Aim:- Write a program to show
usage of isAlive() and join() method.
JAVA Code :-
import java.lang.*;
class NewThread extends Thread
{
String str;
NewThread(String name) {
super(name);
str=name;
System.out.println(name +this);
start();
}
public void run() {
try {
for(int i=5;i>0;i--)
{
System.out.println(str +": " +i);
sleep(1000);
}
}
catch(InterruptedException e) {
System.out.println("Chlid
interrupted ");
}
System.out.println("Exiting "
+str +" thread ");
}
}
class Testjoin
{
public static void main(String args[])
{
NewThread one=new
NewThread("One");
NewThread two=new
NewThread("Two");
System.out.println("One Thread is
alive: " +one.isAlive());
System.out.println("Two Thread is
alive: " +two.isAlive());
try {
one.join();
two.join();
}
catch(InterruptedException e) {
System.out.println("Interrupted
found");
}
System.out.println("One Thread is
alive: " +one.isAlive());
System.out.println("Two Thread is
alive: " +two.isAlive());
System.out.println(" Main thraed
exiting");
}
}
Output:-
No comments:
Post a Comment