Thread isAlive() and join() Methods in Java

Here we will discuss the Thread isalive() and join() method in java

There are two ways to determine whether a thread has finished or not.

  • isAlive()
  •   join()

Read More: Thread Concepts

Thread Life Cycle in Java
Multithreading in Java Thread Creation Method Use Example
Thread Priorities in Java
Main Thread in Java
Thread Yield method in Java
Thread Synchronization in Java
Thread suspend() and resume() method in Java

Thread isAlive() Method

The isAlive() method is used to check whether a thread is in the running or not.

                                                or

In other words, this method determines thread is live or dead. 

isAlive( )method returns true if the thread is still running otherwise it returns false.

Thread join() Method

This method waits for the specified thread to terminate.

import java.lang.*;
public class Demo implements Runnable {
    public void run() {
        Thread t = Thread.currentThread(); // tests if this thread is alive	
        System.out.println("thread is live = " + t.isAlive()); // tests if this thread is alive	
    }
    public static void main(String args[]) throws Exception {
        Thread t = new Thread(new Demo());
        t.start(); // this will call run() function
        t.join(); // waits for this thread to die		
        System.out.println("thread is live = " + t.isAlive()); // tests if this thread is alive	
    }
}

Result

thread is live = true
thread is live = false