![]() |
VOOZH | about |
In Java, threads allow concurrent execution of multiple tasks, improving performance and responsiveness. Sometimes, one thread needs to wait for another thread to finish its execution. java.lang. Thread class provides the join() method which allows one thread to wait until another thread completes its execution.
join() method of the Thread class.join(), its overloaded versions allow specifying a waiting timeThe join() method is used to pause the execution of the current thread until the thread on which it is called completes.
Syntax:
public final void join()
The join() method is used to make the current thread wait until the target thread completely finishes its execution. Waits until the thread completely finishes
Syntax:
public final void join()
It will put the current thread on wait until the thread on which it is called is dead or wait for the specified time (milliseconds).Waits for a specified time or until the thread finishes
Syntax:
public final synchronized void join(long millis)
It will put the current thread on wait until the thread on which it is called is dead or wait for the specified time (milliseconds + nanos). Waits for a specified time (milliseconds + nanoseconds) or until completion
Syntax:
public final synchronized void join(long millis, int nanos)
Example: Java program to explain the concept of joining a thread.
Output:
Current Thread: main
Current Thread: Thread-0
0
Current Thread: Thread-0
1
Current Thread: main
Current Thread: Thread-1
0
Current Thread: Thread-1
1
Current Thread: main
Current Thread: Thread-2
0
Current Thread: Thread-2
1
In the above example we can see clearly second thread t2 starts after first thread t1 has died and t3 will start its execution after second thread t2 has died.