Login    New User    Forgot Password    Help      

IsAlive( ) and Join( ) in Java

Java Tutorials

The main thread must be the last thread to finish. Sometimes this is accomplished by calling sleep() within main( ), with a long enough delay to ensure that all child threads terminate prior to the main thread. However, this is hardly a satisfactory solution, and it also raises a larger question: How can one thread know when another thread has ended? Fortunately, Thread provides a means by which you can answer this question.
Two ways exist to determine whether a thread has finished. First, you can call isAlive( ) on the thread. This method is defined by Thread, and its general form is shown here:

final boolean isAlive( )

The isAlive( ) method returns true if the thread upon which it is called is still running. It returns false otherwise. While isAlive( ) is occasionally useful, the method that you will more commonly use to wait for a thread to finish is called join( ), shown here:

final void join( ) throws InterruptedException

This method waits until the thread on which it is called terminates. Its name comes from the concept of the calling thread waiting until the specified thread joins it. Additional forms of join( ) allow you to specify a maximum amount of time that you want to wait for the specified thread to terminate. Here is an improved version of the preceding example that uses join( ) to ensure that the main thread is the last to stop. It also demonstrates the isAlive( ) method.

Example

// Using isalive and join
/*
isAlive() method returns true if the thread upon which it is called is still running
it returns false other wise
while isAlive() is occasionally useful , the method that you will more commonly use to wait for a thread to finish is called join()
*/

class newtread implements Runnable
{
Thread t;
        // creates a second thread
        newtread()
        {
        // creates other thread
        t= new Thread(this , "New tread");
        System.out.println("Child Thread "+t);
        t.start();// Start the thread
        }

        // entry point for the second thread
        public void run()
        {
        try{
           for(int i=5;i>0;i--)
                {
                   System.out.println("Child Thread "+i+"  ");
                   Thread.sleep(1000);
                }
           }
       catch(InterruptedException e)
           {
           System.out.println("Child interruptedd ");
           }
        System.out.print("Exiting child thread  ");
        }

 

}

class tread4
{
public static void main(String args[])
{
// start multiple thread
newtread ob1 =new newtread();
newtread ob2 =new newtread();
newtread ob3 =new newtread();

System.out.println("Thread First is alive "  + ob1.t.isAlive());
System.out.println("Thread Second is alive " + ob2.t.isAlive());
System.out.println("Thread Third is alive "  + ob3.t.isAlive());

// wait for thread to finish
     try
        {
        System.out.println("Wating to finish ");
        ob1.t.join();
        System.out.println();
        ob2.t.join();
        System.out.println();
        ob3.t.join();
        }
     catch(InterruptedException e)
        {
        System.out.println("Main thread interrupted ");
        }

System.out.println("Thread First is alive "  + ob1.t.isAlive());
System.out.println("Thread Second is alive " + ob2.t.isAlive());
System.out.println("Thread Third is alive "  + ob3.t.isAlive());

 

}
}

Output

Java Tutorials