Java Reference ยท Concurrency
Thread interruption
Reference for Java thread interruption: interrupt(), isInterrupted(), Thread.interrupted(), InterruptedException and how to write cancellable tasks.
Java has no safe way to force a thread to stop. Instead it offers interruption, a cooperative signal: calling thread.interrupt() sets the thread's interrupt flag and, if the thread is blocked in sleep, wait, join, a BlockingQueue operation or similar, wakes it with an InterruptedException. What the thread does next is up to its own code, which should treat the interrupt as a request to stop what it is doing and finish cleanly.
Executors build on this: Future.cancel(true) and ExecutorService.shutdownNow() both interrupt the worker threads. A task that ignores interruption cannot be cancelled.
thread.interrupt(); // ask it to stop Thread.currentThread().isInterrupted(); // is the flag set? (does not clear it) Thread.interrupted(); // is it set? and clear it (static)
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public void interrupt() // instance public boolean isInterrupted() // instance, does not clear public static boolean interrupted() // static, clears the flag // Many blocking methods: throws InterruptedException
| Member | Description |
|---|---|
interrupt() |
Sets the target's interrupt flag. If the thread is blocked in an interruptible method, it wakes and throws InterruptedException (clearing the flag). |
isInterrupted() |
Returns the flag of that thread without changing it. |
Thread.interrupted() |
Returns the current thread's flag and clears it. |
InterruptedException |
Checked exception thrown by blocking methods when the thread is interrupted while waiting (or when it enters them with the flag set). |
Future.cancel(true) |
Interrupts the thread running the task. |
ExecutorService.shutdownNow() |
Interrupts all workers and returns the tasks that never started. |
Usage notes
- Never swallow
InterruptedException. Either let it propagate, or restore the flag withThread.currentThread().interrupt()so the caller can still notice. An emptycatchblock makes the task uncancellable. - A thrown
InterruptedExceptionclears the flag. That is why you restore it if you catch it and carry on. - Not all blocking is interruptible. Plain
InputStream.readon a socket,synchronizedentry and some file I/O ignore interrupts. Close the resource from another thread, uselockInterruptibly(), or use NIO channels (InterruptibleChannel), which close on interrupt. - Poll in long loops. CPU-bound code must check
Thread.currentThread().isInterrupted()periodically. - Cleanup in
finally. Interruption should still release locks and close resources; use try-with-resources. - Do not use
Thread.stop(). It is deprecated for removal and throwsUnsupportedOperationExceptionsince Java 20 because it leaves objects half-updated. - Virtual threads are interrupted the same way as platform threads.
- Interrupt the right thread: in a pool, interrupt through the
Future, not a thread reference you may have captured by accident, or you can cancel an unrelated task. - Timeouts: prefer
future.get(timeout, unit)pluscancel(true)to open-ended waits.