Java Thread isAlive – using isAlive and join methods

Introduction:

In this article we are going to talk about two methods, the Java Thread isAlive method and the Java Thread join method.  We will discuss how to use them, we will see example code and we will learn when to use them.

Example code depicting Java Thread isAlive method together with join method:

package join.isalive;

class ChildThread implements Runnable {

    String runnableName;

    Thread t;

    public void run() {

        try {

            System.out.println(runnableName

                    + ” is entering loop to count from 0 to 3″);

            for (int i = 0; i < 4; i++) {

                System.out.println(runnableName + “: “ + i);

                Thread.sleep(1000);

            }

        } catch (InterruptedException e) {

            System.out.println(runnableName + ” interrupted.”);

        }

        System.out.println(runnableName

                + ” is exiting the loop after counting 0 to 3″);

    }

    ChildThread(String threadname) {

        runnableName = threadname;

        t = new Thread(this, runnableName);

        System.out.println(“New thread name is: “ + t.getName());

    }

}

public class MainClass {

    public static void main(String[] args) {

        ChildThread ct1 = new ChildThread(“ThreadUno”);

        ChildThread ct2 = new ChildThread(“ThreadDos”);

        ChildThread ct3 = new ChildThread(“ThreadTres”);

        ct1.t.start();

        ct2.t.start();

        ct3.t.start();

        System.out.println(“Thread Uno is alive: “ + ct1.t.isAlive());

        System.out.println(“Thread Dos is alive: “ + ct2.t.isAlive());

        System.out.println(“Thread Tres is alive: “ + ct3.t.isAlive());

        try {

            System.out.println(“Waiting for threads to finish.”);

            ct1.t.join();

            ct2.t.join();

            ct3.t.join();

        } catch (InterruptedException e) {

            System.out.println(“Main Thread Interrupted”);

        }

        System.out.println(“Thread Uno is alive: “ + ct1.t.isAlive());

        System.out.println(“Thread Dos is alive: “ + ct2.t.isAlive());

        System.out.println(“Thread Tres is alive: “ + ct3.t.isAlive());

        System.out.println(“Main Thread exiting.”);

    }

}

The “End” of the thread execution:

How do we know when a thread has ended?  The Thread class provides two ways to know when a thread has ended.  You can call the isAlive() method on the thread or you can use the join() method.  Many times you want the main thread to finish last.  It is possible to use sleep() inside the main() method but that is not the best approach.  It is better to use isAlive() or join() or both.  The signatures for the methods are as follows:

boolean isAlive() – Tests if this thread is alive.  The method isAlive() returns true if the thread is still running.  Returns false otherwise.

void join() – Waits for this thread to die.

void join(long millis) – Waits at most millis milliseconds for this thread to die.

void join(long millis, int nanos) – Waits at most millis milliseconds plus nanos nanoseconds for this thread to die.

The join() method waits until the thread on which is called terminates.  The name join means that the calling thread is going to wait until the called thread joins it.  The join() method has several signatures that allow to specify a maximum amount of waiting time for the thread to finish.  The example program below uses a combination of join() and isAlive().

Running the program produces the following output:

New thread name is: ThreadUno

New thread name is: ThreadDos

New thread name is: ThreadTres

ThreadUno is entering loop to count from 0 to 3

ThreadUno: 0

ThreadDos is entering loop to count from 0 to 3

ThreadDos: 0

ThreadTres is entering loop to count from 0 to 3

Thread Uno is alive: true

ThreadTres: 0

Thread Dos is alive: true

Thread Tres is alive: true

Waiting for threads to finish.

ThreadTres: 1

ThreadDos: 1

ThreadUno: 1

ThreadTres: 2

ThreadDos: 2

ThreadUno: 2

ThreadDos: 3

ThreadTres: 3

ThreadUno: 3

ThreadUno is exiting the loop after counting 0 to 3

ThreadDos is exiting the loop after counting 0 to 3

ThreadTres is exiting the loop after counting 0 to 3

Thread Uno is alive: false

Thread Dos is alive: false

Thread Tres is alive: false

Main Thread exiting.

The MainClass example program explained:

In the MainClass program we instantiate ChildThread 3 times, then we start a thread for each instance.  We display messages stating if the thread is alive by using the isAlive() method.  Next we call the join() method on each one of the 3 threads.  By calling the join() methods on each  thread we ensure the children threads will finish before the main() thread.

The ChildThread example program explained:

In the ChildThread program we implement the interface Runnable.  We create 2 varibles runnableName, and t, runnableName is the name given to the thread by the main program.  The variable t contains a  reference to the Thread class.  By having a reference to the Thread class we can call methods on the class.  For example in the following lines we use the reference:

ct1.t.start();  <— start a new threat

ct1.t.isAlive() <— call the Thread class method isAlive()

ct1.t.join()    <— call the Thread class method join()

 

The run() method of the ChildThread class runs a loop that iterates fourth times, from 0 to 3.  The method will display a message when the program enters the loop, then on each iteration it displays the name of the thread together with the iteration number.  Next goes to sleep for 1 second.  Upon finalizing the loop execution it displays a message again.

Conclusion:

We cover two methods from the Java Thread class, the isAlive() method and the join() method.  We talk about when and how to use them, the method signatures and we even looked at some example programs that make use of both methods.

Leave a Comment