skip to content

Thread Interruption

Interruption is cooperative: interrupt() only sets a flag, blocking methods clear it and throw InterruptedException, and it is your job to either propagate or restore it. Swallowing InterruptedException is the mistake interviewers look for in your code.

part ofJavaread it →
on this pageshow

questions

5

What does it mean to interrupt a thread in Java, and what does calling interrupt() actually do?

level: juniorimportance: must knowfreq 70%

answer

  1. interrupt() just sets a boolean flag
  2. not a kill switch — cooperative
  3. poll the flag OR catch InterruptedException
  4. Thread.stop() was unsafe, and is now gone
  5. thread that never checks ignores it

basics

~20 s

Calling interrupt() on a thread sets its interrupt status, a request to stop, and wakes it with InterruptedException if it is blocked in sleep(), wait() or join(). It never kills the thread: the thread must notice and stop itself.

solid answer

~40 s

Interruption in Java is a cooperative cancellation mechanism, not a kill switch. Calling t.interrupt() sets that thread's internal interrupt-status flag to true. It does not stop the thread, throw anything across threads, or abandon work. The interrupted thread is responsible for noticing the request and winding down gracefully. It usually notices in one of two ways: a long-running loop polls Thread.currentThread().isInterrupted() and returns when it sees true; or, if the thread is blocked in a method like sleep(), wait(), or join(), that method clears the flag and throws InterruptedException immediately. So the contract is: the interrupter politely asks, and the worker cooperates by checking the flag or catching the exception. Threads that ignore interruption simply keep running, which is why writing interruptible code is the programmer's responsibility, not the JVM's.

code

java · 11 lines
java
// Cooperative cancellation: the worker polls the flag.
Thread worker = new Thread(() -> {
    while (!Thread.currentThread().isInterrupted()) {
        // do a chunk of CPU work...
    }
    System.out.println("Noticed interrupt, stopping cleanly.");
});
worker.start();

// From another thread: politely ask it to stop.
worker.interrupt(); // just sets the flag; worker exits its loop next check

go deeper

for a junior

Knows interrupt() sets a flag and is a request, not a forced stop; can describe the two ways a thread notices (polling and InterruptedException).

for a middle

Explains why the cooperative model exists (Thread.stop() was unsafe), and can write a loop that polls isInterrupted() to exit cleanly.

for a senior

Articulates the full contract, knows which methods clear the flag vs leave it set, and designs cancellable tasks that combine polling with exception handling.

for a principal

Frames interruption as the mechanism behind FutureTask.cancel(true) and ThreadPoolExecutor.shutdownNow(), knows CompletableFuture.cancel does not interrupt, and sets team conventions for propagating cancellation through layers and libraries.

## The problem interruption solves Sometimes you want to stop a running thread early: a user cancels a download, a request times out, or an app is shutting down. Java's old `Thread.stop()` method did this forcibly, but it was deprecated in JDK 1.2 because killing a thread mid-operation can leave shared data in a broken, half-updated state (locks released with invariants violated). JDK 20 made it throw `UnsupportedOperationException`, which is still all it does at JDK 25, and JDK 26 removed it, so at JDK 27 a call no longer compiles. Java replaced it with a **cooperative** model called **interruption**: you *ask* a thread to stop, and well-written threads *cooperate* by stopping themselves at a safe point. ## What `interrupt()` actually does Every `Thread` object carries a hidden boolean called the **interrupt status flag** (also "interrupt status"), initially `false`. Calling `someThread.interrupt()` from any thread sets that flag to `true`. **That is all it does in the common case** — it does not pause, kill, or throw anything into the running code. The interrupted thread keeps executing normally until *it* chooses to look at the flag. ```text Thread A: workerThread.interrupt(); // sets workerThread's flag = true, returns instantly Thread B (the worker): ... keeps running until it checks the flag ... ``` ## How a thread "notices" it was interrupted There are two common mechanisms, plus a third for NIO I/O: 1. **Polling.** Code that loops doing CPU work checks the flag itself, e.g. `while (!Thread.currentThread().isInterrupted()) { ... }`. When the flag flips to `true`, the loop exits and the thread returns from its task. 2. **Blocking methods throw.** If the thread is *blocked* inside `Thread.sleep(...)`, `Object.wait(...)`, `Thread.join(...)`, or many `java.util.concurrent` operations (e.g. `BlockingQueue.take()`), and the flag is (or becomes) set, that method **clears the flag** and throws a checked `InterruptedException`. This wakes the thread up immediately instead of making it wait for the timeout. 3. **Interruptible NIO I/O.** A thread blocked in I/O on a `java.nio.channels.InterruptibleChannel` is handled differently: the channel is closed, the flag stays **set**, and the operation throws `ClosedByInterruptException`. A thread blocked in a `Selector` simply returns early from the selection, also with the flag set. So a responsive thread combines the first two: it polls the flag in its CPU loops and catches `InterruptedException` around its blocking calls. ## Why "cooperative" Because the JVM never forcibly stops the thread, a thread that never checks its flag and never blocks will simply ignore the interrupt forever. Interruption is therefore a *convention*: the platform provides the flag and the exception, but **your code must honor it**. This is a deliberate trade for safety — the thread stops only at points it controls, so it can release resources and leave shared state consistent. ## Key terms - **Interrupt status flag:** a per-thread boolean meaning "someone asked me to stop." - **Cooperative cancellation:** stopping by request + voluntary checking, not by force. - **InterruptedException:** the checked exception blocking methods throw to signal a pending interrupt.

  • If interrupt() only sets a flag, how does a thread blocked in sleep() stop almost instantly?
    Blocking methods like sleep()/wait()/join() are wired to monitor the interrupt status; when it is set they abandon the wait, clear the flag, and throw InterruptedException, so the thread wakes immediately rather than waiting out the full duration.
  • Can you interrupt a thread that is stuck in a tight CPU loop with no blocking calls and no flag checks?
    You can call interrupt() and the flag will be set, but the loop will never notice it because it neither blocks nor polls isInterrupted(), so it runs to completion regardless. The loop must be written to poll the flag.

  • Thinking interrupt() forcibly stops or kills the thread
  • Believing interrupt() throws an exception in the target thread from the outside
  • Assuming an interrupted thread stops automatically without checking the flag
  • Confusing interruption with Thread.stop()/suspend()

Assumes JDK 27. Interruption behaves the same at JDK 25; what differs is Thread.stop(), which throws UnsupportedOperationException at JDK 25 and no longer exists at JDK 27 (removed in JDK 26).

context

open as a page

What is the difference between the instance method isInterrupted() and the static method Thread.interrupted()?

level: middleimportance: must knowfreq 75%

basics

~10 s

Both read the interrupt flag. isInterrupted() (instance) just reads it and leaves it unchanged. The static Thread.interrupted() reads it for the current thread AND clears it back to false.

open as a page

You catch an InterruptedException in a method that cannot propagate it. How should you handle it correctly, and why is swallowing it wrong?

level: seniorimportance: must knowfreq 80%

basics

~10 s

Don't ignore it. Either rethrow it, or — if you can't — call Thread.currentThread().interrupt() to set the flag again so callers up the stack still know the thread was asked to stop.

open as a page

Which Java methods throw InterruptedException, and what do they do to the interrupt status flag when they throw?

level: middleimportance: should knowfreq 58%

basics

~10 s

Blocking methods like Thread.sleep(), Object.wait(), Thread.join(), and BlockingQueue.take()/put() throw InterruptedException. When they throw, they clear the interrupt flag back to false.

open as a page

How would you design a long-running worker thread that responds promptly and correctly to cancellation?

level: seniorimportance: should knowfreq 62%

basics

~20 s

Use interruption: have the worker check Thread.currentThread().isInterrupted() in its loop and catch InterruptedException around its blocking calls (restoring the flag). To cancel, call interrupt() — don't invent your own stop flag if interruption already works.

open as a page