how to run a thread program in java multi threading

manikandanams
@manikandanams-bew8Eg โ€ข Oct 21, 2024

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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • sookie

    @sookie-T06sFW • Aug 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 !