how to run a thread program in java multi threading

hai i have complete my multi threading java program .compiling section 2 errors
class ThreadA extends Thread
{
public void run()
{
try
{
for(int i=0; i< 5; i++)
{
Thread.sleep(1000);
System.out.println("Executing first thread");
}
}
catch (InterruptedExpection ex)
{
System.out.println("Thread Interrupted");
}
}
}
class ThreadB extends Thread
{
public void run()
{
try
{
for(int i=0; i< 5; i++)
{
Thread.sleep(2000);
System.out.println("Executing second thread");
}}
catch (InterruptedExpection ex)
{
System.out.println("Thread Interrupted");
}
}
}
class demo3
{
public static void main(String args[])
{
ThreadA t1=new ThreadA();
t1.start();
ThreadB t2=new ThreadB();
t2.start();
}
}


this is my program please give the correct solution

Replies

  • sookie
    sookie
    Hi manikandanams,

    In your program , InterruptedException is misspelled as InterruptedExpection. Correct it, then your program will look like following and will compile as well as run successfully.

    class ThreadA extends Thread
    {
    public void run()
    {
    try
    {
    for(int i=0; i< 5; i++)
    {
    Thread.sleep(1000);
    System.out.println("Executing first thread");
    }
    }
    catch (InterruptedException ex)
    {
    System.out.println("Thread Interrupted");
    }
    }
    }
    class ThreadB extends Thread
    {
    public void run()
    {
    try
    {
    for(int i=0; i< 5; i++)
    {
    Thread.sleep(2000);
    System.out.println("Executing second thread");
    }}
    catch (InterruptedException ex)
    {
    System.out.println("Thread Interrupted");
    }
    }
    }
    class demo3 
    {
    public static void main(String args[])
    {
    ThreadA t1=new ThreadA();
    t1.start();
    ThreadB t2=new ThreadB();
    t2.start();
    }
    }
    
    Output: In my machine, it showed following output
    Executing first thread
    Executing first thread
    Executing second thread
    Executing first thread
    Executing first thread
    Executing second thread
    Executing first thread
    Executing second thread
    Executing second thread
    Executing second thread
    Thanks !

You are reading an archived discussion.

Related Posts

I've been wondering whether I need to install a desktop mail client to manage all my email. To tell you frankly, I'm thinking I should stick to all web-mail instead...
hey frnz. m in final year of electrical engineering, nd i need to submit my major project as soon possible. so plz, if any1 among u hav any idea, plz...
Original Article Link: https://www.crazyengineers.com/know-your-cean-–-ms_cs/ Your questions, comments in this thread 😀
CEans, This thread is dedicated to the most memorable college life stories. We only want the original ones. Share yours 😀 ! PS: We'll pick up the best stories and...
Hi all programmers here! Problem Statement : A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right...