Java Thread join example and detailed explanation

The Java Thread join method is used to suspend the current thread until the thread on the join operation ends. There are three overloaded join methods in java:

public final void join(): This method will turn the current thread into wait until the thread performing the join operation ends. If the thread is interrupted during execution, an InterruptedException will be thrown.

public final synchronized void join(long millis): This method will turn the current thread into wait until the thread performing the join operation ends or waits for millis time after the join operation. Because thread scheduling depends on the implementation of the operating system, because this does not guarantee that the current thread will become RUNnable at millis time.

public final synchroinzed void join(long millis, int nanos): This method will turn the current thread into wait until the thread performing the join operation ends or waits for millis+nanos time after join.

The following sample program shows the usage of the Thread join method. In this program, ensure that the main thread is the last thread to complete, and ensure that the third thread (third thread) starts executing after the first thread (first thread) ends.

package com.journaldev.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 output of the program is as follows:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325471907&siteId=291194637