Ad

Friday 11 July 2014

Joining Threads using join method in java

By using join method in Thread, we can make one thread to wait until the other thread completes its execution.There are two overloaded methods of join:

t.join()
    - The current executing thread which invokes the above join method,will make it to wait until thread "t" completes its execution.In other words the current thread will resumes its execution only after thread "t" completes.

t.join(10000)
    - The current executing thread which invokes the above join method,will make it to wait  until thread "t" completes its execution.But one exception to this  is, the current thread can wait only up to a maximum of 10 seconds i.e if thread "t" doesn't complete in 10 seconds,current thread will resumes its execution even though thread "t" has not completed.

The below sample program will explain this behavior


public class ThreadJoinSample {                                                                                                                

public static void main(String[] args) {
System.out.println("Thread started::"+Thread.currentThread().getName());
ChildThread childThread= new ChildThread();
Thread thread=new Thread(childThread, "ChildThread");
thread.start();
try {
thread.join();
//thread.join(10000);
          System.out.println("Thread Resumed::"+Thread.currentThread().getName());
}
   catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread completed::"+Thread.currentThread().getName());
}

}

class ChildThread implements  Runnable {
public void run() {
System.out.println("Thread started::"+Thread.currentThread().getName());
try {
Thread.sleep(20000);//Sleep for 20 seconds
}
    catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread completed::"+Thread.currentThread().getName());
}

}

The Main thread(ie main() method) creates and starts a new ChildThread and invokes  the join method on the Childthread.As a result the main thread will wait until the childThread completes its execution.When we run the above program,we will get the output as shown below:

Thread started::main
Thread started::ChildThread                                                                                                                      
Thread completed::ChildThread
Thread Resumed::main
Thread completed::main

As we can see in the output,the Main thread has resumes its execution only after the Child Thread has completed.

Now use the other overloaded join method in the above program i.e replace join() with join(10000).
Now the output will be:

Thread started::main
Thread started::ChildThread                                                                                                                      
Thread Resumed::main
Thread completed::main
Thread completed::ChildThread

Since the Main Thread can wait only maximum of 10 seconds and the ChildThread will take 20 seconds to completes its execution,the main thread has resumed its execution even though the ChildThread has not completed.

No comments:

Post a Comment