Wait the for the completion of a thread : Wait « Threads « Java






Wait the for the completion of a thread

 
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);
  }

}

   
  








Related examples in the same category

1.Wait for the threads to finish
2.Demonstrate join().
3.A simple demonstration of wait() and notify().
4.Suspend, resume, and stop a thread.
5.Waiting on an object
6.Launch many programs using Thread and use join() to wait for the completion.