Skip to content
Tutorials Center
My learning

Topics

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.

Java
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

Java Cancel a worker that is sleeping

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.

Java Flag, restore and polling in a loop

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

Java
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 with Thread.currentThread().interrupt() so the caller can still notice. An empty catch block makes the task uncancellable.
  • A thrown InterruptedException clears the flag. That is why you restore it if you catch it and carry on.
  • Not all blocking is interruptible. Plain InputStream.read on a socket, synchronized entry and some file I/O ignore interrupts. Close the resource from another thread, use lockInterruptibly(), 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 throws UnsupportedOperationException since 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) plus cancel(true) to open-ended waits.