What thread’s join method?
- The join method allows one thread to wait for the completion of another thread.
- Suppose we have couple of threads like thread1 & thread2.
- If in thread1, we have putĀ thread2.join.
- Then, thread1 will wait for execution of thread2.
- Once thread2 finishes its execution then thread1 will resume its execution.
- There overload methods of join methods, which allow to specify a waiting period.
- The join method responds to an interrupt by exiting with an InterruptedException.
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
- Create a task by implementing Runnable interface.
- We will demonstrate join method and its impact on execution on multiple threads.
- We will write couple of methods
- demoThreadJoin: Method will create multiple threads ( four threads from 1 to 4).
- Output of a method will demonstrates the execution of join method.
- Threads will execute one after another (i.e. thread-1 to thread-4).
- demoWithoutThreadJoin: Method will demonstrate the execution of multiple threads without join method.
- Threads will execute randomly, there will be no sequence of thread execution.
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