Java — Java.lang.Thread.join() Method | Code Factory
3 min readMay 29, 2020
Index Page : Link
Donate : Link
WordPress Blog : Link
- If a thread wants to wait until completing some other thread then we should go for join() method.
- For example if a thread t1 wants to wait until completing t2 then t1 has to call t2.join().
- If t1 executes t2.join() then immediatly t1 will be entered into waiting state until t2 completes.
- Once t2 completes then t1 can continue it’s execution.
- Wedding card printing thread (t2) has to wait venue fixing thread (t1) completion. Hence t2 has to call t1.join().
- Wedding card distribution thread (t3) has to wait until wedding card printing thread (t2) completion. Hence t3 has to call t2.join().
/* Waits for this thread to die. */
public final void join() throws InterruptedException
/* Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances. */
public final void join(long millis) throws InterruptedException
/* Waits at most millis milliseconds plus nanos nanoseconds for this thread to die.This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances. */
public final void join(long millis, int nanos) throws InterruptedException
- Every join method throws
InterruptedException
which is checked exception, compulsory we should handle this exception either by using try catch or by throws keyword otherwise we will get compile time error.
package com.example.thread;/**
* @author code.factory
*
*/
public class ThreadJoin {
public static void main(String... args) throws InterruptedException {
MyThread t = new MyThread();
t.start();
t.join(); // #1
for(int i=0; i<5; i++) {
System.out.println("Main Thread");
}
}
}class MyThread extends Thread {
public void run() {
for(int i=0; i<5; i++) {
System.out.println("Child Thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Output :
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
- Waiting of main thread until completing child thread, you can see in above program.
- If we comment line #1 then both main and child executed simeltaneously we can’t expect exact output.
- If we are not commenting line #1 then main thread calls join method on child thread object. Hence main thread will wait until completing child thread. In this case output is child thread 10 times and then main thread 10 times.
Waiting of child thread until completing main thread :
package com.example.thread;/**
* @author code.factory
*
*/
public class ThreadJoin {
public static void main(String... args) throws InterruptedException {
MyThread.mt = Thread.currentThread();
MyThread t = new MyThread();
t.start();
// t.join(); // It will create deadlock
for(int i=0; i<5; i++) {
System.out.println("Main Thread");
}
}
}class MyThread extends Thread {
static Thread mt;
public void run() {
try {
mt.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i=0; i<5; i++) {
System.out.println("Child Thread");
}
}
}
Output :
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
- In the above example child thread calls join() method on main thread object. Hence child thread has to wait until completing main thread.
- If main thread calls join() method on child thread object and child thread calls join() method on main thread object then both the thread will wait forever and the program will be stucked (this is something like deadlock).
- If a thread calls join() method on the same thread itself then the program will be stucked (this is something like deadlock). In this case thread has to wait infinite amount of time.
package com.example.thread;/**
* @author code.factory
*
*/
public class ThreadJoin {
public static void main(String... args) throws InterruptedException {
Thread.currentThread().join();
}
}