April 15, 2024

isAlive() And join() Methods in Java

In your application you may have a scenario where you spawn a bunch of threads to execute some logic and you want to start any further processing only after all the threads have finished the execution. Which means you need some way to know whether a thread is terminated or not. For that purpose Thread class provides isAlive() and join() methods in Java to check if a thread has finished executing or not.

isAlive() method in Java

This method tests if this thread is alive. A thread is alive if it has been started and has not yet died. Method returns true if thread is alive otherwise it returns false.

isAlive() method syntax
public final boolean isAlive()

join() method in Java

This method waits until the thread on which it is called terminates. There are three overloaded versions of join() method in Java Thread class.

  • public final void join() throws InterruptedException- Waits indefinitely for this thread to die.
  • public final void join(long millis) throws InterruptedException- Waits at most the time in milliseconds for this thread to die.
  • public final void join(long millis, int nanos) throws InterruptedException- Waits at most the time in milliseconds plus additional time in nanoseconds for this thread to die.

isAlive() and join() method Java example

Here is an example where five threads are created and you want to ensure that any further processing is done only after all these five threads finish executing the run() method and all these five threads are terminated.

First we’ll see what happens if join() method is not used.

class MyRunnable implements Runnable{
  @Override
  public void run() {
    System.out.println("In run method --" + Thread.currentThread().getName());	
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }	
}

public class ThreadDemo {

  public static void main(String[] args) {
    Thread t1 = new Thread(new MyRunnable());
    Thread t2 = new Thread(new MyRunnable());
    Thread t3 = new Thread(new MyRunnable());
    Thread t4 = new Thread(new MyRunnable());
    Thread t5 = new Thread(new MyRunnable());

    t1.start();
    t2.start();
    t3.start();
    t4.start();
    t5.start();

    System.out.println("Is t1 Alive " + t1.isAlive());
    System.out.println("Is t2 Alive " + t2.isAlive());
    System.out.println("Is t3 Alive " + t3.isAlive());
    System.out.println("Is t4 Alive " + t4.isAlive());
    System.out.println("Is t5 Alive " + t5.isAlive());

    System.out.println("Now start further processing");
  }
}
Output
Is t1 Alive true
Is t2 Alive true
Is t3 Alive true
Is t4 Alive true
Is t5 Alive true
Now start further processing
In run method --Thread-1
In run method --Thread-0
In run method --Thread-2
In run method --Thread-3
In run method –Thread-4

As you can see from the output “Now start further processing” message is printed even before the threads started executing. But that is not what you want so let’s see how join() method can help in this scenario.

class MyRunnable implements Runnable{
  @Override
  public void run() {
    System.out.println("In run method --" + Thread.currentThread().getName());	
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }	
}

public class ThreadDemo {

  public static void main(String[] args) {
    Thread t1 = new Thread(new MyRunnable());
    Thread t2 = new Thread(new MyRunnable());
    Thread t3 = new Thread(new MyRunnable());
    Thread t4 = new Thread(new MyRunnable());
    Thread t5 = new Thread(new MyRunnable());
    
    t1.start();
    t2.start();
    t3.start();
    t4.start();
    t5.start();
        
    System.out.println("Is t1 Alive " + t1.isAlive());
    System.out.println("Is t2 Alive " + t2.isAlive());
    System.out.println("Is t3 Alive " + t3.isAlive());	
    System.out.println("Is t4 Alive " + t4.isAlive());
    System.out.println("Is t5 Alive " + t5.isAlive());
    try {
      t1.join();
      t2.join();
      t3.join();
      t4.join();
      t5.join();
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
        
    System.out.println("Is t1 Alive " + t1.isAlive());
    System.out.println("Is t2 Alive " + t2.isAlive());
    System.out.println("Is t3 Alive " + t3.isAlive());
    System.out.println("Is t4 Alive " + t4.isAlive());
    System.out.println("Is t5 Alive " + t5.isAlive());

    System.out.println("Now start further processing");
  }
}
Output
Is t1 Alive true
In run method --Thread-2
In run method --Thread-0
In run method --Thread-3
Is t2 Alive true
Is t3 Alive true
Is t4 Alive true
Is t5 Alive true
In run method --Thread-4
In run method --Thread-1
Is t1 Alive false
Is t2 Alive false
Is t3 Alive false
Is t4 Alive false
Is t5 Alive false
Now start further processing

As you can see “Now start further processing” message is now displayed only after all the five threads have finished executing. With isAlive() methods after the join() methods it can be verified that the threads are terminated.

That's all for the topic isAlive() And join() Methods in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment