Java's thread interruption mechanism relies on three distinct methods: interrupt(), isInterrupted(), and interrupted(). These methods operate around a boolean interruption flag maintained within each thread, but differ significantly in functionality and usage.
The Interruption Flag
Every Java thread maintains an internal interrupsion flag:
- Setting this flag to
truesuggests the thread should stop execution (cooperative approach) - Blocking methods like
sleep(),wait(), andjoin()throwInterruptedExceptionwhen interrupted and clear the interruption flag - The three methods differ primarily in how they manipulate this flag: setting, checking, or checking-and-clearing
Method Breakdown
1. interrupt(): Set the Flag
public void interrupt()
- Function: Sets the target thread's interruption flag to
true - Behavior:
- Called via thread instance (e.g.,
thread.interrupt()) - If target thread is blocked, triggers
InterruptedExceptionand clears flag - No return value
- Called via thread instance (e.g.,
Example:
class InterruptExample {
public static void main(String[] args) throws InterruptedException {
Thread worker = new Thread(() -> {
while (!Thread.currentThread().isInterrupted()) {
System.out.println("Working...");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println("Interrupted during sleep. Flag reset to: "
+ Thread.currentThread().isInterrupted());
Thread.currentThread().interrupt(); // Re-set flag
}
}
System.out.println("Exiting. Final flag state: "
+ Thread.currentThread().isInterrupted());
});
worker.start();
Thread.sleep(2000);
worker.interrupt();
}
}
Output:
Working...
Working...
Working...
Interrupted during sleep. Flag reset to: false
Exiting. Final flag state: true
2. isInterrupted(): Check Flag State
public boolean isInterrupted()
- Functon: Checks interruption status without modifying flag
- Behavior:
- Called via thread instance
- Returns
trueif flag is set,falseotherwise - Non-destructive: flag remains unchanged
Example:
class StatusCheckExample {
public static void main(String[] args) {
Thread task = new Thread(() -> {
for (int i = 0; i < 5; i++) {
System.out.println("Task executing. Interrupted? "
+ Thread.currentThread().isInterrupted());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
task.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
task.interrupt();
}
}
Output:
Task executing. Interrupted? false
Task executing. Interrupted? false
Task executing. Interrupted? true
Task executing. Interrupted? true
Task executing. Interrupted? true
3. interrupted(): Check and Clear Flag
public static boolean interrupted()
- Function: Checks and clears interruption status of current thread
- Behavior:
- Static method:
Thread.interrupted() - Returns previous flag state before clearing
- Always resets flag to
falseafter checking
- Static method:
Example:
class ClearFlagExample {
public static void main(String[] args) {
Thread runner = new Thread(() -> {
Thread.currentThread().interrupt();
System.out.println("First check: " + Thread.interrupted());
System.out.println("Second check: " + Thread.interrupted());
System.out.println("isInterrupted(): "
+ Thread.currentThread().isInterrupted());
});
runner.start();
}
}
Output:
First check: true
Second check: false
isInterrupted(): false
Method Comparison
| Method | Type | Primary Action | Clears Flag? | Target | Returns |
|---|---|---|---|---|---|
interrupt() |
Instance | Sets flag to true |
No¹ | Specified thread | void |
isInterrupted() |
Instance | Checks flag state | No | Specified thread | boolean |
interrupted() |
Static | Checks and clears flag | Yes | Current thread | boolean |
¹: Blocking methods clear flag when throwing InterruptedException
Practical Applications
Graceful Thread Termination
class GracefulStop {
public static void main(String[] args) throws InterruptedException {
Thread service = new Thread(() -> {
while (!Thread.currentThread().isInterrupted()) {
System.out.println("Processing task...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Interruption received");
Thread.currentThread().interrupt();
}
}
System.out.println("Shutdown complete");
});
service.start();
Thread.sleep(3000);
service.interrupt();
}
}
Avoiding Method Misuse
// Incorrect: Using static method on another thread
Thread t = new Thread(() -> {});
t.interrupt();
System.out.println(Thread.interrupted()); // Checks current (main) thread
System.out.println(t.isInterrupted()); // Correctly checks t's status
Key Takeaways
interrupt()triggers interruption without stopping threadisInterrupted()safely checks status without side effectsinterrupted()clears flag and operates on current thread only- Blocking methods clear interruption flag; manual re-interruption often needed