Thread Prevention and Deadlock in Java : Free Java Tutorials

Thread Prevention using join() method sometimes causes an unintended deadlock situation to arise. A deadlock situation occurs when there is a circular chain of threads each waiting for the other thread to finish its execution.

Join() method can also be used to prevent child thread from executing before main thread. To do this, we have to call join method from child thread on the instance of the main thread. The static keyword of java is useful in implementing this functionality.

Look at the Java program below for better understanding.

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

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

In the above Example, child thread calls join method on the main thread object. Hence, child thread has to wait until the completion of the main thread.

Since main thread is printing one statement every second, it will take 10 seconds to complete its execution. So, child thread will wait for 10 seconds and then only it will continue its execution. Look at the output below for better understanding of the above program.

Output:

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
Child thread is running
Child thread is running
Child thread is running
Child thread is running
Child thread is running

If we want child thread to wait only for a specific amount of time, then we can call join method on main thread object and pass time in milliseconds as an argument in the join method. In such case, child thread will wait for the specified amount of time and allow main thread to execute during that time.

Join() method deadlock: Scenario 1

If the main thread calls join method on child thread object and child thread calls join method on main thread object, then both the threads will wait forever and the program will be paused forever. Since both threads are waiting for each other, a deadlock situation arises and the program will freeze without execution of any threads.

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

// Main class
public class Demo
{
	public static void main(String args[]) throws InterruptedException
	{
		// passing main thread reference
		MyThread.main_thread = Thread.currentThread();
		
		// 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");
			Thread.sleep(1000);
		}
	}
}

Output for the above program will be empty as there is a deadlock situation.

Join() method deadlock: Scenario 2

If a thread calls join method on the same thread itself, then the program will freeze and this is also considered a deadlock situation. In this case, the thread has to wait for an infinite amount of time.

// Main class
public class Demo
{
	public static void main(String args[]) throws InterruptedException
	{
		Thread.currentThread().join();
		
		for(int i=0;i<10;i++)
		{
			System.out.println("Main thread is running");
		}
	}
}

Output in this case will also be empty as there is a deadlock situation.

This is all about the deadlock in the java in multi-threading. Hope you find this tutorial useful.

Leave a Comment

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