BackendBench
Coffee

Unravel › Java concepts › Concurrency

Concurrency

Interruption and InterruptedException

interrupt() is a polite request: "please stop what you are doing".

Learn these first

At a glance

Mental model

Tapping a colleague on the shoulder while they wait on hold.

What happens, in order
  1. 1Each thread has an interrupted flagt.interrupt() sets it; if the thread is blocked in sleep, wait or join, that call throws InterruptedException and clears the flag.
  2. 2In a loop that does work, check while (!Thread.currentThread().isInterrupted()) { ..} and exit when asked.
  3. 3If you catch InterruptedException and cannot rethrow, restore the flag: Thread.currentThread().interrupt(); so callers further up still see itNever leave an empty catch block.
  4. 4Executors use it: shutdownNow() and future.cancel(true) interrupt the worker threads
Remember
Watch out
catch (InterruptedException e) {} or only printing itUsing Thread.stop, which is removedTreating interruption as an error to log at ERROR level
Example (highlighted lines explain it)
public class Worker implements Runnable {  private final java.util.concurrent.BlockingQueue<String> queue;  Worker(java.util.concurrent.BlockingQueue<String> queue) { this.queue = queue; }  @Override public void run() {    try {      while (!Thread.currentThread().isInterrupted()) {        String job = queue.take();        System.out.println("job " + job);      }    } catch (InterruptedException e) {      Thread.currentThread().interrupt();    }  }}
  1. line 6 uses currentThread, the core of Interruption and InterruptedException
  2. line 10 uses InterruptedException, the core of Interruption and InterruptedException
  3. line 11 uses currentThread, the core of Interruption and InterruptedException
Read the full explanation (about 1 min)

interrupt() is a polite request: "please stop what you are doing". Blocking methods such as sleep, wait and queue.take() respond by throwing InterruptedException, and your code must either stop or restore the flag.

A mental model

Tapping a colleague on the shoulder while they wait on hold. They are not forced to hang up, but a well-mannered colleague notices the tap, wraps up and leaves.

How it works

Each thread has an interrupted flag. t.interrupt() sets it; if the thread is blocked in sleep, wait or join, that call throws InterruptedException and clears the flag.

In a loop that does work, check while (!Thread.currentThread().isInterrupted()) { ... } and exit when asked.

If you catch InterruptedException and cannot rethrow, restore the flag: Thread.currentThread().interrupt(); so callers further up still see it. Never leave an empty catch block.

Executors use it: shutdownNow() and future.cancel(true) interrupt the worker threads.

Going deeper

Swallowing the interrupt is a classic bug: the pool cannot shut down, the application hangs on exit, and a cancelled request keeps working. Either propagate (throws InterruptedException) or re-set the flag.

Blocking I/O on plain sockets and synchronized waiting are not interruptible; use channels, timeouts, or lockInterruptibly().

Interrupt is cooperative cancellation: design long tasks with check points, and set timeouts on everything that waits.

Under the hood

The flag lives in the JVM thread structure. Thread.interrupted() reads and clears it, isInterrupted() only reads. Blocking primitives such as LockSupport.park check the flag before and while waiting and throw or return when it is set.

Common mistakes

  • catch (InterruptedException e) {} or only printing it.
  • Using Thread.stop, which is removed.
  • Treating interruption as an error to log at ERROR level.

How Unravel recognises it

When you paste code, Unravel looks for these signs of interruption and interruptedexception. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is Interruption and InterruptedException?

interrupt() is a polite request: "please stop what you are doing". Blocking methods such as sleep, wait and queue.take() respond by throwing InterruptedException, and your code must either stop or restore the flag.

What are the most common mistakes with Interruption and InterruptedException?

catch (InterruptedException e) {} or only printing it. Using Thread.stop, which is removed. Treating interruption as an error to log at ERROR level.

How can I see Interruption and InterruptedException in real code?

Paste your own Java into Unravel and it will point out where interruption and interruptedexception appears, explain how it works at the detail level you choose, and draw the flow of the code around it.