Java Data Type Tutorial - Java Thread.join()








Syntax

Thread.join() has the following syntax.

public final void join()  throws InterruptedException

Example

In the following code shows how to use Thread.join() method.

public class Main {
  public static void main(String args[]) throws Exception {
/*from   w ww  .  jav  a 2  s.c  o  m*/
    Thread t = new Thread(new ThreadDemo());
    t.start();
    // waits for this thread to die
    t.join();
    System.out.print(t.getName());
    // checks if this thread is alive
    System.out.println(", status = " + t.isAlive());
  }

}

class ThreadDemo implements Runnable {

  public void run() {

    Thread t = Thread.currentThread();
    System.out.print(t.getName());

    System.out.println(", status = " + t.isAlive());
  }
}

The code above generates the following result.