Thread Prevention using join Method in Java : Free Java Tutorials and Online Training

Join method allows us to do thread prevention. If a thread wants to wait until the completion of some other thread, then we should go for join() method. For example, if a thread t1 wants to wait until completion of thread t2, then t1 has to call t2.join().

Thread’s join method is mainly used where there are dependencies between threads. If one thread’s input depends on other thread’s output, then we can use join method in that case.

Join method is always called by the thread who wants to wait for the completion of other thread. Join method is always called on the instance of the thread which is currently running. For example, if t1 executes t2.join(), then immediately t1 will enter into waiting state until t2 completes. Once t2 completes, then t1 can continue its execution.

Every Join method throws Interrupted Exception as a waiting method can be interrupted by some other thread. Since this is a checked exception, we should compulsorily handle it either by using try catch or by using throws keyword. otherwise, we will get compile time error.

Thread join() method has following 3 prototypes:

  1. public final void join() – wait until the thread t2 finishes its execution.
  2. public final void join(long milliseconds) – wait for the specified number of milliseconds and allow thread t2 to execute during that time.
  3. public final void join(long milliseconds, int nanoseconds) – wait for specified and accurate milliseconds and nanoseconds and allow thread t2 to execute during that time.

Impact of join method on the Lifecycle of a thread

Thread Prevention

Example of Thread Prevention using join() method

// We create our own class named MyThread that extends Thread class
class MyThread extends Thread
{	
	// overridden run method
	public void run()
	{
		for(int i=0;i<10;i++)
		{
			System.out.println("Child thread is running");
			try 
			{
				Thread.sleep(1000);	
			}
			catch(InterruptedException exp)
			{
				System.out.println("Child thread has been interrupted");	
			}
		}
	}
}

// Main class
public class Demo
{
	public static void main(String args[]) throws InterruptedException
	{
		// Creating a thread object of our class named MyThread and starting the thread.
		MyThread t = new MyThread();
		t.start();
		t.join();
		
		for(int i=0;i<10;i++)
		{
			System.out.println("Main thread is running");
		}
	}
}

Here, since we have called t.join(), the main thread will wait for the child thread to finish its execution entirely. So, child thread will print one statement in a second and finish its execution in 10 seconds. Only after these 10 seconds, the main thread will continue its execution.

Output:

Child thread is running
Child thread is running
Child thread is running
Child thread is running
Child thread is running
Child thread is running
Child thread is running
Child thread is running
Child thread is running
Child thread is running
Main thread is running
Main thread is running
Main thread is running
Main thread is running
Main thread is running
Main thread is running
Main thread is running
Main thread is running
Main thread is running
Main thread is running

We can also call join method with waiting time in milliseconds as an argument. In above example, if we call join method and pass 5000 milliseconds as an argument, the main thread will wait for 5 seconds and allow child thread to execute. After these 5 seconds, the main thread will continue its execution followed again by child thread’s execution.

// We create our own class named MyThread that extends Thread class
class MyThread extends Thread
{	
	// overridden run method
	public void run()
	{
		for(int i=0;i<10;i++)
		{
			System.out.println("Child thread is running");
			try 
			{
				Thread.sleep(1000);	
			}
			catch(InterruptedException exp)
			{
				System.out.println("Child thread has been interrupted");	
			}
		}
	}
}

// Main class
public class Demo
{
	public static void main(String args[]) throws InterruptedException
	{
		// Creating a thread object of our class named MyThread and starting the thread.
		MyThread t = new MyThread();
		t.start();
		t.join(5000);
		
		for(int i=0;i<10;i++)
		{
			System.out.println("Main thread is running");
		}
	}
}

Output:

Child thread is running
Child thread is running
Child thread is running
Child thread is running
Child thread is running
Main thread is running
Main thread is running
Main thread is running
Main thread is running
Main thread is running
Main thread is running
Main thread is running
Main thread is running
Main thread is running
Main thread is running
Child thread is running
Child thread is running
Child thread is running
Child thread is running
Child thread is running

Hope you like this article.

Leave a Comment

Your email address will not be published. Required fields are marked *