Java - Thread Joining

A thread invokes the join() method on another thread in order to wait for the other thread to complete its execution.
Consider a thread t1 invokes the method join() on a thread t2. The join() call has no effect if thread t2 has already completed. If thread t2 is still alive, then thread t1 transits to the Blocked-for-join-completion state.

Below is a program showing how threads invoke the overloaded thread join method.

public class ThreadJoinDemo {

public static void main(String[] args) {
Thread t1 = new Thread("T1");
Thread t2 = new Thread("T2");
try {
System.out.println("Wait for the child threads to finish.");
if (!t1.isAlive())
System.out.println("Thread T1 is not alive.");
if (!t2.isAlive())
System.out.println("Thread T2 is not alive.");
} catch (InterruptedException e) {
System.out.println("Main Thread interrupted.");
System.out.println("Exit from Main Thread.");

Wait for the child threads to finish.
Thread T1 is not alive.
Thread T2 is not alive.
Exit from Main Thread.

People who read this post also read :


Post a Comment


Twitter Delicious Facebook Digg Stumbleupon Favorites More