Advertisement



< Prev
Next >



Interrupting a running Thread



Each and every thread in Java has a flag associated with it which tells us 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 the interrupt() method, it is set to true.




How a thread is interrupted?

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




When a running thread is interrupted:


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




Note:


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




Interrupting a running thread.


In this program, we are going to interrupt a running thread by calling the interrupt() method on it. Thread after being interrupted, will not throw an InterruptedException, therefore, we will call the interruped() method to check the status of its interrupt flag.

//Java - Interrupting a running 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);

		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.
						
	//Starts the thread
	th.start();	  
	
	//Interrupts the thread while it is running
	th.interrupt();   
}

} 


Output-


0
Thread was interrupted
1
2
3
4


Program Analysis





Advertisement




Manually throwing InterruptedException from a running thread.


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

//Java - Manually throwing InterruptedException from a running 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

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement