Wait the for the completion of a thread : Thread Join « Thread « Java Tutorial






public class Main {
  public static void main(String a[]) throws Exception {
    MyThread tt1 = new MyThread(50);
    MyThread tt2 = new MyThread(75);
    Thread t1 = new Thread(tt1, "Test thread 1");
    Thread t2 = new Thread(tt2, "Test thread 2");
    t1.start();
    t2.start();
    t1.join();
    t2.join();
  }
}

class MyThread implements Runnable {
  int i;

  MyThread(int i) {
    super();
    this.i = i;
  }

  public void run() {
     System.out.println(Thread.currentThread().getName() + " " + i);
  }

}








10.5.Thread Join
10.5.1.Thread Join Demo
10.5.2.Specify the number of milliseconds to wait for the death of a thread
10.5.3.Wait the for the completion of a thread
10.5.4.Wait for the threads to finish
10.5.5.Using join() to wait for threads to finish.