|
Thread Demo
class
NewThread implements Runnable
{
String name;
Thread t;
NewThread(String threadName)
name = threadName;
t = new Thread(this,name);
System.out.println("New Thread : " + t);
}
public void run ( )
{
try
{
for(int i = 5; i > 0; i++)
{
System.out.println(name + " : " + i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println(name + " Interrupted.");
}
System.out.println(name + " End of the
Thread.");
}
}
public class MultiThreadDemo
{
public static void main(String arg[])
{
NewThread ram, seetha, lakshman;
ram = new NewThread ("Mr. Ram") ;
seetha = new NewThread("Mrs. Seetha");
lakshman = new NewThread("Mr. Lakshman");
System.out.println("Is the thread" + ram.name
+ "Alive," + ram.t.isAlive());
System.out.println("Is the thread "+seetha.name+"Alive,"+seetha.t.isAlive());
System.out.println("Is the thread "+lakshman.name+" Alive, "+
lakshman.t.isAlive());
try
{
ram.t.join();
seetha.t.run();
lakshman.t.run();
catch(InterruptedException e)
{
System.out.println("Is the thread " + ram.name
+ " Alive, " + ram.t.isAlive());
System.out.println("Is the thread " +
seetha.name + " Alive, " + seetha.t.isAlive());
System.out.println("Is the thread " +
lakshman.name + " Alive, " +
lakshman.t.isAlive());
}
}
}
Output
:
New Thread : Thread[Mr.Ram,5,main]
New Thread : Thread[Mrs.Seetha,5,main]
New Thread : Thread[Mr.Laxman,5,main]
Is the thread Mr.Ram Alive, false
Is the thread Mrs.Seetha Alive, false
Is the thread Mr.Laxman Alive, false
Mrs.Seetha : 5
Mrs.Seetha : 4
Mrs.Seetha : 3
Mrs.Seetha : 2
Mrs.Seetha : 1
Mrs. Seetha End of the Thread.
Mr.Laxman : 5
Mr.Laxman : 4
Mr.Laxman : 3
Mr.Laxman : 2
Mr.Laxman : 1
Mr.Laxman End of the Thread.
|