Tuesday, 29 August 2017

how to generates 2 threads in java program

Java Program to generates 2 threads

WAP that generates 2 threads 1st thread print ‘a’ after 1000ms & 2nd print ‘b’ after 2000ms.

Create threads by using extend thread & implements runnable.

Program

class Multi1 extends Thread{
public void run()
{
try
{Thread.sleep(1000);}
catch(InterruptedException e)
{System.out.println(e);}
System.out.println("A");
}
}
class Multi2 extends Thread{
public void run()
{
try
{Thread.sleep(2000);}
catch(InterruptedException e)
{System.out.println(e);}
System.out.println("B");
}
}
class MultiDemo2
{
public static void main(String args[]){
Multi1 t1=new Multi1();
Multi2 t2=new Multi2();
t1.start();
t2.start();
}

}

Output

generates 2 threads core java

0 comments:

Post a Comment