< Prev
Next >



Interrupting a running Thread



Each and every thread in Java has a flag associated with it which tells us the its interrupt status. This flag can be set to true or false. By default, this flag is set to false but when a thread is interrupted by interrupt() method, it is set to true.




How a thread is interrupted?

A running thread can be interrupted by calling interrupt() method on it.




When a running thread is interrupted :


When a running thread is interrupted by interrupt() method, its interrupt flag of thread is set to true, the thread continues its work without throwing any InterruptedException, unless you check the status of its interrupt flag by interrupted() method and throw an InterruptedException manually.




Note


interrupted() method is a static method of Thread class.




Interrupting a running thread.


In this program, we are going to interrupt a running thread by calling interrupt() method on it. Thread after being interrupted, will not throw an InterruptedException. Hence, we will use interruped() method of thread to check the status of its interrupt flag.
class Intr implements Runnable  	//Implementing the Runnable interface
{

public void run()		//Entry point of new thread
{

	for(int i=0;i<5;i++)
	{
		System.out.println(i);

		if (Thread.interrupted())  // Checking interrupt status
   		   System.out.println("Thread was interrupted");
	
	}			

}


public static void main(String... ar)
{
Intr newTh= new Intr();
Thread th= new Thread(newTh,"Thread2");     //Calling Thread's constructor & passing the object
					    //of class that  implemented  Runnable interface
					    //& the name of new thread.
						
th.start();	  //Starts the thread.
th.interrupt();   //Interrupts the thread while it is running
}

}
 


Output-


0
Thread was interrupted
1
2
3
4


Program Analysis







Manually throwing InterruptedException from a running thread.


As you have seen in the previous program how a running thread doesn't throw an InterruptedException even after interrupt() method is called on it. In this program we will manually throw InterruptedException after confirming the status of interrupt flag of a thread.

class Intr implements Runnable	//Implementing the Runnable interface
{

public void run()		//Entry point of new thread	
{

	for(int i=0;i<5;i++)
	{
		System.out.println(i);
	try
	{
		if (Thread.interrupted())  	     // Checking interrupt status of Thread2
   		   throw new InterruptedException(); //throwing an InterruptedException manually.
	}
	catch(Exception e)
	{
		System.out.println(e);
	}
	}			

}


public static void main(String... ar)
{
Intr newTh= new Intr();
Thread th= new Thread(newTh,"Thread2");     //Calling Thread's constructor & passing the object
					    //of class that  implemented  Runnable interface
					    //& the name of new thread.

//Starts the thread.
th.start();	

//Interrupts the thread while it is running, raising InterruptedException	
th.interrupt();		
}

}


Output is - :

0
java.lang.InterruptedException
1
2
3
4


Program Analysis


Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2