Thread Interruption in Java : Free Java Tutorials

Thread Interruption is an important concept in Java. A thread can interrupt another thread which is in the sleeping state or waiting state by using the interrupt method of thread class.

The prototype of interrupt method of Thread class: public void interrupt()

If thread t1 calls interrupt method on the object of thread t2 and thread t2 is in waiting or sleeping state, then thread t2 will get interrupted. And in case of interruption, we must handle Interrupted Exception on the thread which is going to be interrupted while in waiting or sleeping state.

Example of Thread Interruption

We will see following two different cases of thread interruption in detail.

Case 1: Interrupt call on a thread which is in the sleeping state

 // We create our own class named MyThread that extends Thread class
class MyThread extends Thread
{	
	// overridden run method
	public void run()
	{
		System.out.println("Child thread is going in the sleeping state");
		try 
		{
			// child thread is sleeping for 5 seconds
			Thread.sleep(5000);	
		}
		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 thread = new MyThread();
		thread.start();
		
		// Main thread sending an interrupt call on child thread
		thread.interrupt();
		
		for(int i=0;i<10;i++)
		{
			System.out.println("Main thread is running");
		}
	}
}

In this case, main thread calls interrupt method on the child thread object. Hence, an interrupted exception occurs and main thread interrupts child thread which is in the sleeping state. Look at the output of the above program for better understanding.

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 going in the sleeping state
Child thread has been interrupted

If we don’t call interrupt method on child thread, then main thread will not interrupt child thread. In this case, child thread will execute successfully without any interrupt.

Case 2: Interrupt call on a thread which is under execution

If we call interrupt method on a thread which is neither in the sleeping state nor in the waiting state but rather is under execution, then there is no impact of interrupt call immediately. The interrupt call will not be wasted and there will not be any exception.

Here, interrupt call will wait for the target thread to come into sleeping or waiting state. Once the target thread enters into any of the two states, target thread will immediately get interrupted by interrupt call.

So, the conclusion is that interrupt call never goes in vain and JVM always takes care of the interrupt call.

// We create our own class named MyThread that extends Thread class
class MyThread extends Thread
{	
	// overridden run method
	public void run()
	{
		int sum = 0;
		for(int i=0;i<1000000;i++)
		{
			// in every iteration, value of sum is incremented by 1
			sum = sum + 1;
		}
		
		System.out.println("Child thread has calculated total sum = " + sum);
		
		System.out.println("Now, child thread is going in the sleeping state");
		try 
		{
			// child thread is sleeping for 5 seconds
			Thread.sleep(5000);	
		}
		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 thread = new MyThread();
		thread.start();
		
		// Main thread sending an interrupt call on child thread
		thread.interrupt();
		
		for(int i=0;i<10;i++)
		{
			System.out.println("Main thread is running");
		}
	}
}

In above program, JVM will wait until child thread calculates the value of sum. When child thread enters into sleeping state, JVM will immediately make an interrupt call on child thread and it will be interrupted.

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 has calculated total sum = 1000000
Now, child thread is going in the sleeping state
Child thread has been interrupted

However, if the target thread never enters into sleeping or waiting state in its lifetime, then there is no impact of interrupt call. No interrupted exception will be generated in this case. This is the only case where interrupt call will be wasted.

This is all about Thread Interruption in Java. Hope you find this tutorial helpful.

 

Leave a Comment

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