Java Thread Join

In some conditions, we might need to pause or halt the execution of a current thread until a different thread completes its execution. Java thread join API gives a solution to this requirement and this can be easily achieved by a simple call.

We have an in-build option to accomplish this in the Thread class which is join() method.

Thread Join Example

public class JoinOfThread extends Thread {

	public static void main(String[] args) throws InterruptedException {
		Thread thread=new JoinOfThread();
		thread.start();
		thread.join();
//Main thread will wait until the completion of a thread execution.
		System.out.println("Main thread completing the execution");
	}
	
	public void run()
	{
	System.out.println("Thread entered the run method");
	System.out.println("Thread completing  the execution of run method");
	}

}
In the above example, we see main thread will wait until complete the execution of a newly created thread of JoinOfThread.