Java Data Type Tutorial - Java Thread.join(long millis)








Syntax

Thread.join(long millis) has the following syntax.

public final void join(long millis)  throws InterruptedException

Example

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

//  www  .  j  av  a 2s . co  m

public class Main {

  public static void main(String args[]) throws Exception {

    Thread t = new Thread(new ThreadDemo());
    t.start();
    // waits at most 2000 milliseconds for this thread to die.
    t.join(2000);

    // after waiting for 2000 milliseconds...
    System.out.print(t.getName());

    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.