In Java multithreading, threads often depend on the results of other threads. Java provides a simple way to handle such dependencies using the join() method. The join() method allows one thread to pause its execution until another thread has completed.
In this chapter, you will understand what the join() method is, why it is used, and how its different overloaded forms work in Java.
The join() method is provided by the java.lang.Thread class. It allows the current executing thread to wait until the thread on which join() is called finishes its execution.
In simple terms: One thread waits for another thread to complete before moving forward.
The join() method is mainly used when:
Java provides three overloaded versions of the join() method. These allow threads to wait either indefinitely or for a specific amount of time.
When join() is invoked without parameters, the current thread stops execution and enters the waiting state. It remains there until the target thread completes its execution.
If the waiting thread is interrupted, an InterruptedException is thrown.
Syntax
It has the following syntax:
This version of the join () method allows the current thread to wait for a specified time (in milliseconds). The waiting ends when:
Just like sleep(), the actual waiting time depends on the operating system, so exact timing is not guaranteed.
Syntax
It has the following syntax:
Here, millis represents time in milliseconds.
This version of join() method allows even more precise waiting by specifying:
The current thread waits until:
Again, timing accuracy depends on the operating system.
Syntax:
It has the following syntax:
Here, millis is milliseconds and nanos is additional nanoseconds.
The following example demonstrates how the join() method is used to make the main thread wait until a child thread completes its execution.
Output:
The current thread name is: main The current thread name is: Thread - 0 0 The current thread name is: Thread - 0 1 The current thread name is: main The current thread name is: Thread - 1 0 The current thread name is: Thread - 1 1 The current thread name is: Thread - 2 0 The current thread name is: Thread - 2 1
Explanation:
The above program shows that the second thread th2 begins after the first thread th1 has ended, and the thread th3 starts its work after the second thread th2 has ended or died.
As discussed earlier, when a thread waiting using the join() method is interrupted, Java throws an InterruptedException. This happens when another thread interrupts the currently waiting thread.
The following example demonstrates using join() method with InterruptedException:
Output:
The exception has been caught. java.lang.InterruptedException
Let us now see some additional examples to understand how join() affects thread execution order.
The following example demonstrates how the join() method without any time limit makes one thread wait until another thread completes its execution.
Output:
1
2
3
4
5
1
1
2
2
3
3
4
4
5
5
Here, t1 completes its execution first. Only after t1 finishes do t2 and t3 start executing.
The following example demonstrates how the join(long milliseconds) method allows a thread to wait for a specified time period.
Output:
1
2
3
1
4
1
2
5
2
3
3
4
4
5
5
In this case, t1 is allowed to execute for 1500 milliseconds (approximately 3 iterations). After that time expires, t2 and t3 begin execution even though t1 has not yet finished.
We request you to subscribe our newsletter for upcoming updates.