Java — Java.lang.Thread.join() Method | Code Factory

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.
  • Wedding card printing thread (t2) has to wait venue fixing thread (t1) completion. Hence t2 has to call t1.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.

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.
package com.example.thread;/**
* @author code.factory
*
*/
public class ThreadJoin {
public static void main(String... args) throws InterruptedException {
Thread.currentThread().join();
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store