Monday, 4 July 2011

Concept of isAlive and join in java

isAlive : This method is used to check whether a thread is in the running state or not .
Join : This method is use to specify the time you want to wait for the specified thread to terminate .

Example :

class Demo
{
String arr[]={"Here comes the I'st child ",
                    "Here comes the II'nd child",
                    "Here comes the III'rd child"};
public void run()
{
try
{
for(int i=0;i<3;i++)
{
Thread.sleep(3000);
System.out.println(arr[i]);
}
}
catch(InterruptedException e)
{
}
}
}
class Demojoin
{
public static void main(String args[])
{
System.out.println("The School Bus is waiting for the three children at the Bus Stop");
Demo ob1 = new Demo();
Thread t = new Thread(ob1);
t.start();
while(t.isAlive()) //Here you can make out the use of isAlive concept
{
try
{
System.out.println("Still waiting ........ ");
join(1000);
}
catch(InterrutptedException e)
{
}
}
System.out.println("Finally!!");
}
}

OUTPUT :

AbeBooks Logo Canada

No comments:

Post a Comment