Java join() Method: Synchronize Thread Execution in Java

Understand the Java join() method, a crucial function in multithreading that enables one thread to wait until another completes. Provided by the java.lang.Thread class, join() helps synchronize thread execution, ensuring sequential completion when needed. Explore the three overloaded versions of join() and learn how they enhance control over concurrent operations.



Java join() Method

The join() method in Java, provided by the java.lang.Thread class, allows one thread to wait for the completion of another thread. If th is a thread object currently executing, the statement th.join() ensures that th finishes before the next statement executes. The join() method has three overloaded versions:

Overloaded join() Methods

The following are the three overloaded join() methods:

  • join(): Waits indefinitely until the thread dies.
  • join(long mls): Waits for the specified number of milliseconds.
  • join(long mls, int nanos): Waits for the specified milliseconds plus nanoseconds.

join() Method

Syntax

public final void join() throws InterruptedException
public final synchronized void join(long mls) throws InterruptedException
public final synchronized void join(long mls, int nanos) throws InterruptedException

Example of join() Method in Java

The following program demonstrates the usage of the join() method:

Syntax

class ThreadJoin extends Thread {
public void run() {
for (int j = 0; j < 2; j++) {
    try {
        Thread.sleep(300);
        System.out.println("The current thread name is: " + Thread.currentThread().getName());
    } catch (Exception e) {
        System.out.println("The exception has been caught: " + e);
    }
    System.out.println(j);
}
}
}
public class ThreadJoinExample {
public static void main(String[] argvs) {
ThreadJoin th1 = new ThreadJoin();
ThreadJoin th2 = new ThreadJoin();
ThreadJoin th3 = new ThreadJoin();
th1.start();
try {
    System.out.println("The current thread name is: " + Thread.currentThread().getName());
    th1.join();
} catch (Exception e) {
    System.out.println("The exception has been caught: " + e);
}
th2.start();
try {
    System.out.println("The current thread name is: " + Thread.currentThread().getName());
    th2.join();
} catch (Exception e) {
    System.out.println("The exception has been caught: " + e);
}
th3.start();
}
}
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

join() Method: InterruptedException

The join() method throws InterruptedException if the thread is interrupted while waiting. Here's an example:

Syntax

class ABC extends Thread {
Thread threadToInterrupt;
public void run() {
threadToInterrupt.interrupt();
}
}
public class ThreadJoinExample1 {
public static void main(String[] argvs) {
try {
    ABC th1 = new ABC();
    th1.threadToInterrupt = Thread.currentThread();
    th1.start();
    th1.join();
} catch (InterruptedException ex) {
    System.out.println("The exception has been caught: " + ex);
}
}
}
Output

The exception has been caught: java.lang.InterruptedException

Other Examples of join() Method

Example 1:

Syntax

class TestJoinMethod1 extends Thread {
public void run() {
    for (int i = 1; i <= 5; i++) {
        try { Thread.sleep(500); } catch (Exception e) { System.out.println(e); }
        System.out.println(i);
    }
}
}
public class TestJoinMethod1 {
public static void main(String args[]) {
    TestJoinMethod1 t1 = new TestJoinMethod1();
    TestJoinMethod1 t2 = new TestJoinMethod1();
    TestJoinMethod1 t3 = new TestJoinMethod1();
    t1.start();
    try { t1.join(); } catch (Exception e) { System.out.println(e); }
    t2.start();
    t3.start();
}
}
Output

1
2
3
4
5
1
1
2
2
3
3
4
4
5
5

Example 2: join(long milliseconds) Method

Syntax

class TestJoinMethod2 extends Thread {
public void run() {
    for (int i = 1; i <= 5; i++) {
        try { Thread.sleep(500); } catch (Exception e) { System.out.println(e); }
        System.out.println(i);
    }
}
}
public class TestJoinMethod2 {
public static void main(String args[]) {
    TestJoinMethod2 t1 = new TestJoinMethod2();
    TestJoinMethod2 t2 = new TestJoinMethod2();
    TestJoinMethod2 t3 = new TestJoinMethod2();
    t1.start();
    try { t1.join(1500); } catch (Exception e) { System.out.println(e); }
    t2.start();
    t3.start();
}
}
Output

1
2
3
1
4
1
2
5
2
3
3
4
4
5
5