Thread class’s join method can be used to stop current execution of thread until thread it joins, completes its task. So basically , it waits for the thread on which join method is getting called, to die.
There are three variant of join method:
public final void join() throws InterruptedException :
Thread on which join method is getting called to die.
public final void join(long millis) throws InterruptedException:
This method when called on the thread, it waits for either of following:
- Thread on which join method is getting called, to die.
- Specified milliseconds
This method when called on the thread, it waits for either of following:
- Thread on which join method is getting called, to die.
- Specified milliseconds + nano seconds
Example:
Lets take simple example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.arpit.java2blog.thread; public class MyRunnable implements Runnable{ public void run() { try { System.out.println(Thread.currentThread().getName()+" Start"); // thread sleeps for 4 secs Thread.sleep(4000); System.out.println(Thread.currentThread().getName()+" end"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
Create ThreadExampleMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package org.arpit.java2blog.thread; public class ThreadExampleMain { public static void main(String args[]) { System.out.println("Main thread execution starts"); MyRunnable mr=new MyRunnable(); Thread t1=new Thread(mr,"Thread 1"); Thread t2=new Thread(mr,"Thread 2"); Thread t3=new Thread(mr,"Thread 3"); t1.start(); // lets waits for t1 to die try { t1.join(); } catch (InterruptedException e) { e.printStackTrace(); } t2.start(); try { // lets waits for 1 sec or t2 to die which ever occurs first t2.join(1000); } catch (InterruptedException e1) { e1.printStackTrace(); } t3.start() ; // complete all threads before completing main thread try { t2.join(); t3.join(); } catch (InterruptedException e1) { e1.printStackTrace(); } System.out.println("Main thread execution ends"); } } |
When you run above program, you will get following output.
1 2 3 4 5 6 7 8 9 10 |
Main thread execution starts Thread 1 Start Thread 1 end Thread 2 Start Thread 3 Start Thread 2 end Thread 3 end Main thread execution ends |
Lets analysis output now.
- Main thread execution starts
- Thread 1 starts(Thread 1 start) and as we have put t1.join() , it will wait for t1 to die(Thread 1 end).
- Thread 2 starts(Thread 2 start) and waits for either 1 seconds or die but as we have put sleep for 4 seconds in run method, it will not die in 1 second. so main thread resumes and Thread 3 starts(Thread 3 start)
- As we have put t2.join() and t3.join(). These 2 threads will get completed before exiting main thread.So Thread 2 will end(Thread 2 end ) and then thread 3 will end(Thread 3 end).
- Main thread execution ends.
Join Our News Letter – Stay Updated
Subscribe to Awesome Java Content.