Site icon

Java Concurrency – Thread join example with Runnable interface a

What thread’s join method?

Join methods of thread class in java:

Method Name Description
void join() Waits for this thread to die.
void join(long millis) Waits at most millis milliseconds for this thread to die.
void join(long millis, int nanos) Waits at most millis milliseconds plus nanos nanoseconds for this thread to die.

Algorithm: Demo of Join methods in multi-thread application

Program – thread join example with Runnable interface in java

package org.learn;


class MyTask implements Runnable {
    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        try {
            System.out.printf("Start of %s\n",name);
            Thread.sleep(1500);
            System.out.printf("End of %s\n",name);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public class ThreadJoinExample {
    public static void main(String[] args) throws InterruptedException {
        MyTask task = new MyTask();
        System.out.println("1. Working with multiple threads using thread join:");
        demoThreadJoin(task);
        System.out.println("2. Working with multiple threads WITHOUT thread join:");
        demoWithoutThreadJoin(task);

    }

    private static void demoWithoutThreadJoin(MyTask task) throws InterruptedException {
        Thread thread1 = new Thread(task, "Thread-1 without join");
        Thread thread2 = new Thread(task, "Thread-2 without join");
        Thread thread3 = new Thread(task, "Thread-3 without join");
        Thread thread4 = new Thread(task, "Thread-4 without join");

        //Start thread 1
        thread1.start();

        //Start thread 2
        thread2.start();

        //Start thread 3
        thread3.start();

        //Start thread 4
        thread4.start();
    }

    private static void demoThreadJoin(MyTask task) throws InterruptedException {
        Thread thread1 = new Thread(task, "Thread-1 using join");
        Thread thread2 = new Thread(task, "Thread-2 using join");
        Thread thread3 = new Thread(task, "Thread-3 using join");
        Thread thread4 = new Thread(task, "Thread-4 using join");

        //Start thread 1
        thread1.start();
        thread1.join();

        //start thread 2
        thread2.start();
        thread2.join();

        //Start thread 3
        thread3.start();
        thread3.join();

        //start thread 4
        thread4.start();
        thread4.join();
    }
}

Output – thread join example with Runnable interface in java

1. Working with multiple threads using thread join:
Start of Thread-1 using join
End of Thread-1 using join
Start of Thread-2 using join
End of Thread-2 using join
Start of Thread-3 using join
End of Thread-3 using join
Start of Thread-4 using join
End of Thread-4 using join
2. Working with multiple threads WITHOUT thread join:
Start of Thread-1 without join
Start of Thread-2 without join
Start of Thread-4 without join
Start of Thread-3 without join
End of Thread-1 without join
End of Thread-2 without join
End of Thread-4 without join
End of Thread-3 without join
Exit mobile version