Java-IsAlive and Join Methods

IsAlive and Join Methods

As mentioned, often you will want the main thread to finish last. In the following program, 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.

Program

final0

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. 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( ). 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.

The following program is an improved version of the previous program that uses join( ) to ensure that the main thread is the last to stop. It also demonstrates the isAlive( ) method. Note that the call to join( ) method is enclosed in a try block and followed by a catch block. This is necessary because the join( ) method throws an exception, which should be caught. If we fail to catch the exception, program will not compile.

Progarm


final0

Program Source

class NewThread extends Thread{

    NewThread(String name)
    {
        setName(name);
        start();
    }
   
    public void run()
    {
        try{
            for(int i=10;i<=50;i+=10)
            {
                Thread.sleep(1000);
                System.out.println(getName()+" : "+i);
            }
        }catch(InterruptedException ie)
        {
            System.out.println(getName()+" Interrupted");
        }
        System.out.println("Exiting "+getName()+" Thread");
    }
}

public class Javaapp {
  
    public static void main(String[] args) {
       
        NewThread th1 = new NewThread("Child1");
        NewThread th2 = new NewThread("Child2");
        try{
            Thread.sleep(10000);
        }catch(InterruptedException ie)
        {
            System.out.println("ThreadMain Interrupted");
        }        
        System.out.println("Exiting MainThread");
    }
}

Program Source

class NewThread extends Thread{

    NewThread(String name)
    {
        setName(name);
        start();
    }
   
    public void run()
    {
        try{
            for(int i=10;i<=50;i+=10)
            {
                Thread.sleep(1000);
                System.out.println(getName()+" : "+i);
            }
        }catch(InterruptedException ie)
        {
            System.out.println(getName()+" Interrupted");
        }
        System.out.println("Exiting "+getName()+" Thread");
    }
}

public class Javaapp {
  
    public static void main(String[] args) {
       
        NewThread th1 = new NewThread("Child1");
        NewThread th2 = new NewThread("Child2");
        System.out.println("Thread th1 is alive : "+th1.isAlive());
        System.out.println("Thread th2 is alive : "+th2.isAlive());
        try{
            th1.join();
            th2.join();
        }catch(InterruptedException ie)
        {
            System.out.println("ThreadMain Interrupted");
        }   
        System.out.println("Thread th1 is alive : "+th1.isAlive());
        System.out.println("Thread th2 is alive : "+th2.isAlive());
        System.out.println("Exiting MainThread");
    }
}

Leave a Comment