CrazyEngineers
  • how to run a thread program in java multi threading

    manikandanams

    manikandanams

    @manikandanams-bew8Eg
    Updated: Oct 21, 2024
    Views: 956
    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
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • sookie

    MemberAug 24, 2009

    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 !
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register