Another functionality with threads in Java is the possibility to combine threads together using the join() method.
How to Join Threads in Java?
Java's Thread class provides an overloaded join() method, which allows one Thread to wait/pause/hold until another thread is complete.
This is very helpful when you want to use multi-threading to complete a number of non-sequential tasks but then need to do something in sequential order once those threads are complete.
Three Types of the Join Method
There are three overloaded join functions.
join(): It will put the currentThreadon hold until theThreadon which it is called has been completed.join(long millis): It will put the currentThreadon hold until theThreadon which it is called is complete - but it will only wait for as long as you specify in milliseconds. If theThreadis not completed in that time, the currentThreadwill move on without it.join(long millis, int nanos): It will put the currentThreadon hold until theThreadon which it is called is complete - but it will only wait for as long as you specify in milliseconds + nanoseconds. If theThreadis not completed in that time, the currentThreadwill move on without it.
Example Thread Join in Java
In the example below, you create a Thread object named thrd. After starting that Thread, you invoke the join() method on that object, and this way, the main Thread will wait until thrd completes before moving forward.
Calling thrd.join() will make sure that thrd is terminated before moving forward with the program.
// Java program to explain the concept of joining a thread (author: Nitsdheerendra)
import java.io.*;
// Creating thread by creating the objects of that class
class ThreadJoining extends Thread {
@Override
public void run() {
for (int i = 0; i < 2; i++) {
try {
Thread.sleep(500);
System.out.println("Current Thread: "
+ Thread.currentThread().getName());
} catch(Exception ex) {
System.out.println(
"Exception has been caught" + ex);
}
System.out.println(i);
}
}
}
class GFG {
public static void main (String[] args) {
ThreadJoining thrd0 = new ThreadJoining();
ThreadJoining thrd1 = new ThreadJoining();
ThreadJoining thrd2 = new ThreadJoining();
// thread thrd0 starts
thrd0.start();
// starts second thread after when
// first thread "thrd0" has died.
try {
System.out.println("Current Thread: "
+ Thread.currentThread().getName());
thrd0.join();
} catch(Exception ex) {
System.out.println(
"Exception has been caught" + ex);
}
// thrd1 starts
thrd1.start();
try {
System.out.println("Current Thread: "
+ Thread.currentThread().getName());
thrd1.join();
} catch(Exception ex) {
System.out.println(
"Exception has been caught" + ex);
}
// start thrd2 after when thread thrd1 has died.
thrd2.start();
}
}
/* 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: Thread-2
0
Current Thread: Thread-2
1
Summary: What is a Thread Join?
- Threads can be joined together in Java, putting them in sequential order
- A joined
Threadwill only execute once its previousThreadhas been completed - Threads are joined using the
join()method
Three Types of the Join Method
join()join(long millis)join(long millis, int nanos)