Java Thread Interruption Explained: interrupt(), interrupted() & isInterrupted()
This article clarifies the differences among Java’s thread interruption methods—interrupt(), Thread.interrupted(), and isInterrupted()—explaining how each affects a thread’s interrupt status, when exceptions are thrown, and how to properly monitor and handle interruptions in concurrent applications.
In Java, three methods related to thread interruption— interrupt(), Thread.interrupted() and isInterrupted() —have distinct behaviors.
interrupt()
interrupt()is an instance method that sets the interrupt status of the current or specified thread to true. If the thread is blocked in a call such as sleep(), wait() or certain I/O operations, invoking interrupt() causes an InterruptedException to be thrown and clears the interrupt flag. If the thread is not blocked, the flag is simply set to true without throwing an exception.
Note: Interrupting a thread only changes its interrupt flag; it does not stop the thread automatically. The thread must periodically check the flag and respond appropriately.
interrupted()
Thread.interrupted()is a static method that checks whether the current thread has been interrupted and then clears the interrupt status. The first call after an interruption returns true; subsequent calls return false because the flag has been cleared.
isInterrupted()
isInterrupted()checks the interrupt status of a thread without clearing it. It returns true as long as the thread’s interrupt flag remains set, regardless of how many times the thread has been interrupted.
/**
* t1 thread checks interrupt status via isInterrupted().
* Main thread interrupts t1.
* After catching InterruptedException, t1 resets the interrupt status
* to continue checking in the loop.
*/
public class InterruptTest {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
// Loop checking t1's interrupt status
while (!Thread.currentThread().isInterrupted()) {
System.out.println("t1 thread is running...");
try {
// Simulate blocking operation
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("t1 was interrupted...");
// Interrupt status cleared, so reset it
Thread.currentThread().interrupt();
}
}
System.out.println("t1 thread has exited...");
});
t1.start();
// Main thread waits 3 seconds then interrupts t1
Thread.sleep(3000);
t1.interrupt();
}
} t1 thread is running...
t1 thread is running...
t1 thread is running...
t1 was interrupted...
t1 thread has exited...Summary
interrupt(): Sets the thread’s interrupt flag to true, potentially causing an InterruptedException if the thread is blocked. Thread.interrupted(): Checks and clears the current thread’s interrupt status. isInterrupted(): Checks the interrupt status without clearing it.
Xuanwu Backend Tech Stack
Primarily covers fundamental Java concepts, mainstream frameworks, deep dives into underlying principles, and JVM internals.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
