Java Thread Join method

The Java Thread join method allows the current thread to pause its execution until the specified thread finishes. There are three different versions of the join function.

Wait for a Java thread to finish execution

The join() method in Java is used to make the current thread wait until the thread on which it is called is finished. If the thread is interrupted, it will throw an InterruptedException. There are two additional variants of the join() method. The join(long millis) method can be used to make the current thread wait for the specified number of milliseconds or until the target thread is finished. However, due to differences in OS implementation, it cannot guarantee that the current thread will wait only for the given time. The join(long millis, int nanos) method can be used to make the current thread wait for the specified number of milliseconds and nanoseconds or until the target thread is finished. Here is a simple example that demonstrates the usage of the Thread join methods. The objective of the program is to ensure that the main thread is the last one to finish and that the third thread starts only when the first one is finished.

package com.scdev.threads;

public class ThreadJoinExample {

    public static void main(String[] args) {
        Thread t1 = new Thread(new MyRunnable(), "t1");
        Thread t2 = new Thread(new MyRunnable(), "t2");
        Thread t3 = new Thread(new MyRunnable(), "t3");
        
        t1.start();
        
        //start second thread after waiting for 2 seconds or if it's dead
        try {
            t1.join(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        t2.start();
        
        //start third thread only when first thread is dead
        try {
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        t3.start();
        
        //let all threads finish execution before finishing main thread
        try {
            t1.join();
            t2.join();
            t3.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        System.out.println("All threads are dead, exiting main thread");
    }

}

class MyRunnable implements Runnable{

    @Override
    public void run() {
        System.out.println("Thread started:::"+Thread.currentThread().getName());
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Thread ended:::"+Thread.currentThread().getName());
    }
    
}

The program above will produce the following result:

Thread started:::t1
Thread started:::t2
Thread ended:::t1
Thread started:::t3
Thread ended:::t2
Thread ended:::t3
All threads are dead, exiting main thread

That’s it for a brief overview of an example involving joining Java threads.

 

more tutorials

multithreading in Java that you need to know(Opens in a new browser tab)

Java thread ensuring Java code is thread-safe(Opens in a new browser tab)

Adding a string to a Python variable(Opens in a new browser tab)

Ensuring thread safety in Java Singleton Classes(Opens in a new browser tab)

Java String substring() method(Opens in a new browser tab)

Leave a Reply 0

Your email address will not be published. Required fields are marked *