Java- Interrupting a Thread

Interrupting a Thread

An interrupt is an indication to a thread that it should stop what it is doing and do something else. It’s up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate

If any thread is in sleeping or waiting state (i.e. sleep() or wait()), calling the interrupt() method on the thread, breaks out the sleeping or waiting state throwing InterruptedException.

public void interrupt() - Inturpting a Thread

To Test the Status

  • public static boolean interrupted() It is a Static method, tests whether the CURRENTLY running thread is interrupted or not

  • public boolean isInterrupted() : It is a instance method and tests whether the thread instance on which the method is invoked is interrupted or not

If the thread is not in the sleeping or waiting state, calling the interrupt() method performs normal behaviour and doesn’t interrupt the thread but sets the interrupt flag to true


Thread is Interrupted & Stops working

public class InterruptNormal extends Thread {
	public void run() {
 try {
 	Thread.sleep(1000);
 	System.out.println("task");
 } catch (InterruptedException e) {
 	throw new RuntimeException("Thread interrupted..." + e);
 }
 System.out.println("Thread is Running ...");
	}
	public static void main(String args[]) {
 InterruptNormal t1 = new InterruptNormal();
 t1.start();
 try {
 	t1.interrupt();
 } catch (Exception e) {
 	System.out.println("Exception handled " + e);
 }
	}
}
Exception in thread "Thread-0" java.lang.RuntimeException: Thread interrupted...java.lang.InterruptedException: sleep interrupted
	at threads.InterruptNormal.run(InterruptNormal.java:9)

above Example your are re throwing IntereuptException. So thraed is Inturrpeed & also stops its execution


Thread is Interrupted & doesn’t Stops working

public class InterruptHandled extends Thread {
	public void run() {
 try {
 	Thread.sleep(3000);
 	System.out.println(" *** Sleep is Still Running ****");
 } catch (InterruptedException e) {
 	System.out.println("Thread interrupted..." + e);
 }
 System.out.println("Thread is Running ...");
	}
	public static void main(String args[]) {
 InterruptHandled t1 = new InterruptHandled();
 t1.start();
 try {
 	t1.interrupt();
 } catch (Exception e) {
 	System.out.println("Exception handled " + e);
 }
	}
}
Thread interrupted...java.lang.InterruptedException: sleep interrupted
Thread is Running ...

above Example Exception is handled. It is only interruped sleeping thraed.reaming are excuting as normal

If thread is not in sleeping or waiting state, calling the interrupt() method sets the interrupted flag to true that can be used to stop the thread by the java programmer later.

public class InterruptHandled extends Thread {
	public void run() {  
 System.out.println(" *** No Sleep is Here ****");  
 System.out.println("Thread is Running ...");
	}

	public static void main(String args[]) {
 InterruptHandled t1 = new InterruptHandled();
 t1.start();
 try {
 	t1.interrupt();
 } catch (Exception e) {
 	System.out.println("Exception handled " + e);
 }
	}
}
*** No Sleep is Here ****
Thread is Running ...

The interrupt mechanism is implemented using an internal flag known as the interrupt status. Invoking interrupt() sets this flag. When a thread checks for an interrupt by invoking the static method Thread.interrupted, interrupt status is cleared. The non-static isInterruptedmethod, which is used by one thread to query the interrupt status of another, does not change the interrupt status flag.

By convention, any method that exits by throwing an InterruptedException clears interrupt status when it does so. However, it’s always possible that interrupt status will immediately be set again, by another thread invoking interrupt.