Unravel › Java concepts › Concurrency
Interruption and InterruptedException
interrupt() is a polite request: "please stop what you are doing".
Learn these first
At a glance
Tapping a colleague on the shoulder while they wait on hold.
- 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.
- 2In a loop that does work, check while (!Thread.currentThread().isInterrupted()) { ..} and exit when asked.
- 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.
- 4Executors use it: shutdownNow() and future.cancel(true) interrupt the worker threads
- Swallowing the interrupt is a classic bug: the pool cannot shut down, the application hangs on exit, and a cancelled request keeps working
- Blocking I/O on plain sockets and synchronized waiting are not interruptible; use channels, timeouts, or lockInterruptibly()
- The flag lives in the JVM thread structure
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(); } }}- line 6 uses currentThread, the core of Interruption and InterruptedException
- line 10 uses InterruptedException, the core of Interruption and InterruptedException
- 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:
- Types:
InterruptedException - Method calls:
interrupted()isInterrupted()Thread.interrupted()Thread.currentThread()interrupt()sleep()join()
Part of a learning path
- Concurrency beyond the basics · previous: Runnable, Callable and Future · next: Latches, barriers and semaphores
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.