Thread join() method in Java

In this post, we will see Thread join() method in Java. Let’s see some points related to the join() method.

  • Method declaration – public final void join() throws InterruptedException.
  • Thread’s join() method have three overloaded versions, all throws InterruptedException(Checked Exception)

join() throws InterruptedException
join(long millis) throws InterruptedException
public final void join(long millis, int nanos) throws InterruptedException

  • What it does – The join() method allows one thread to wait for the completion of another.
  • If a thread wants to wait for the completion of other thread then we can use join() method. For example, there are two threads i.e main thread and thread1 and if a thread (main thread) wants to complete thread1 then the main thread has to call thread.join().
  • Let’s see an example. First, we will see an example without the join() method, then later we will use the join() method.

Example without join() method –

package multithreading;

class MyThread1 extends Thread {
	public void run() {
		for (int i = 0; i < 5; i++) {
			System.out.println("thread1");
		}

	}
}

public class ThreadExample1 {
	public static void main(String[] args) throws InterruptedException {
		MyThread1 myThread1 = new MyThread1();
		myThread1.start();
		for (int i = 0; i < 5; i++) {
			System.out.println("main thread");
		}

	}
}

Output –

main thread
main thread
main thread
main thread
main thread
thread1
thread1
thread1
thread1
thread1

Note – Since both threads will execute simultaneously, the output may vary. It depends on the Thread scheduler, which thread will get picked first. To get the different outputs we need to execute the program multiple times.

another possible output –

thread1
thread1
thread1
thread1
thread1
main thread
main thread
main thread
main thread
main thread

Example2 – In this example, we are going to use join() method with thread1.

package multithreading;

class MyThread1 extends Thread {
	public void run() {
		for (int i = 0; i < 5; i++) {
			System.out.println("thread1");
		}

	}
}

public class ThreadExampleJoin1 {
	public static void main(String[] args) throws InterruptedException {
		MyThread1 thread1 = new MyThread1();
		thread1.start();
		thread1.join();
		for (int i = 0; i < 5; i++) {
			System.out.println("main thread");
		}

	}
}

output is –

thread1
thread1
thread1
thread1
thread1
main thread
main thread
main thread
main thread
main thread

Now we have a fixed output. What’s going on here? Once again just recall what the join() method does. The join() method allows one thread to wait for the completion of another thread.  In the above program, when we say one thread that means the main thread, and another thread that means thread1. 

Did you notice, In the above program line 16(i.e. thread1.join()) is executed by the main thread? So the main thread will wait for the completion of another thread(i.e. thread1) that’s why in output we have thread1 first.

Let’s see another example –

package multithreading;

class MyThread1 extends Thread {
	public void run() {
		for (int i = 0; i < 5; i++) {
			System.out.println("thread1");
		}

	}
}

class MyThread2 extends Thread {
	public void run() {
		for (int i = 0; i < 5; i++) {
			System.out.println("thread2");
		}

	}
}

public class ThreadExampleJoin2 {
	public static void main(String[] args) throws InterruptedException {
		MyThread1 thread1 = new MyThread1();
		MyThread2 thread2 = new MyThread2();
		thread1.start();
		thread1.join();
		thread2.start();

	}
}

Output is –

thread1
thread1
thread1
thread1
thread1
thread2
thread2
thread2
thread2
thread2

Note – In the above program we have three threads i.e. thread1,thread2 and the main thread. Line number 26 will be executed by the main thread which means the main thread will wait to complete thread1. Once thread1 execution will complete the main thread will execute thread2.

That’s all about Thread join() method in Java.

join() method docs.