Java Thread join(), isAlive() Method

Category: Java   Tags: Java, Java Thread, Java MultiThread, Thread Synchronization

join() and isAlive() are two methods to determine whether a thread has finished.

The isAlive() method returns true if the thread upon which it is called is still running. It returns false otherwise. Its general form is:

                          final boolean isAlive()
                        

The join() method waits until the thread on which it is called terminates. Its general form is:

                          final void join() throws InterruptedException
                        

Here is a simple example:

JavaThreadJoinExample.java

                            package com.tutorial.javabasic;

                            class MyThread implements Runnable {
                                String threadName;
                                Thread thread;

                                MyThread(String name) {
                                    threadName = name;
                                    thread = new Thread(this, name);
                                    thread.start();
                                }

                                @Override
                                public void run() {
                                    for(int i = 1; i<=5; i++) {
                                        try {
                                            System.out.println("[ " + threadName + " ] : " + i);
                                            Thread.sleep(1000);
                                        } catch (InterruptedException ex) {
                                            System.out.println("[ " + threadName + " ] Interrupted!");
                                        }
                                    }
                                }
                            }

                            public class JavaThreadJoinExample {
                                public static void main(String s[]) {
                                    System.out.println("Main thread");
                                    MyThread thread1 = new MyThread("Thread-1");
                                    MyThread thread2 = new MyThread("Thread-2");
                                    MyThread thread3 = new MyThread("Thread-3");

                                    System.out.println("Thread1 alive: " + thread1.thread.isAlive());
                                    System.out.println("Thread2 alive: " + thread2.thread.isAlive());
                                    System.out.println("Thread3 alive: " + thread3.thread.isAlive());

                                    System.out.println("Waiting for termination...");
                                    try {
                                        thread1.thread.join();
                                        thread2.thread.join();
                                        thread3.thread.join();
                                    } catch (InterruptedException ex) {
                                        System.out.println("Thread operation interrupted.");
                                    }

                                    System.out.println("Thread1 alive: " + thread1.thread.isAlive());
                                    System.out.println("Thread2 alive: " + thread2.thread.isAlive());
                                    System.out.println("Thread3 alive: " + thread3.thread.isAlive());
                                }
                            }
                        

Output:

                            Main thread
                            Thread1 alive: true
                            [ Thread-2 ] : 1
                            [ Thread-1 ] : 1
                            [ Thread-3 ] : 1
                            Thread2 alive: true
                            Thread3 alive: true
                            Waiting for termination...
                            [ Thread-1 ] : 2
                            [ Thread-3 ] : 2
                            [ Thread-2 ] : 2
                            [ Thread-1 ] : 3
                            [ Thread-3 ] : 3
                            [ Thread-2 ] : 3
                            [ Thread-1 ] : 4
                            [ Thread-2 ] : 4
                            [ Thread-3 ] : 4
                            [ Thread-2 ] : 5
                            [ Thread-3 ] : 5
                            [ Thread-1 ] : 5
                            Thread1 alive: false
                            Thread2 alive: false
                            Thread3 alive: false
                          

Here you can see that before using join() all isAlive() returns true but after join() all isAlive() returns false. This indicate that join() make sure that calling thread is terminated.