What makes Thread.sleep() and Thread.join() throw InterruptedException, and which thread has to be interrupted for each?
answer
- whose interrupt does each method react to?
- a pending interrupt counts too
- throwing clears the caller's status
- joiner interrupted: target keeps running
- propagate, or restore the flag and stop
basics
~20 sThread.sleep() and Thread.join() throw InterruptedException when the calling thread is interrupted while it waits, and clear its interrupt status; for join(), that means the waiter, not the target. Never swallow it: propagate it or restore the flag.
solid answer
~40 sThread.sleep() and Thread.join() are interruptible waits. Each throws InterruptedException if the calling thread is interrupted while it waits, including by an interrupt still pending when the wait begins, and clears that thread's interrupt status as it throws. For sleep() that is the sleeping thread itself. For t.join() it is the joining thread, not t: interrupting t does not make the joiner throw, and interrupting the joiner ends only its wait while t keeps running. Interruption is Java's cooperative cancellation signal — it doesn't forcibly stop a thread. Swallowing the exception discards the request, so either propagate it by declaring throws InterruptedException, or, where you cannot (Runnable.run()), restore the flag with Thread.currentThread().interrupt() and stop work promptly. This is the backbone of graceful shutdown of thread pools and tasks.
code
java · 11 linespublic void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
doChunkOfWork();
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // restore cleared flag
break; // stop promptly
}
}
}go deeper
Know that sleep() and join() throw InterruptedException when the calling thread is interrupted, and never leave the catch block empty.
Explain that the exception concerns the calling thread, fires for a pending interrupt as well as a new one, and clears the flag; then apply the propagate-or-restore rule.
Explain that interrupting a joiner ends only its wait while the target keeps running, connect interruption to ExecutorService.shutdownNow and timeouts, and poll isInterrupted() in non-blocking loops; never swallow it.
Designs cancellation-aware APIs and shutdown semantics across a system, defines team conventions for interruption handling, and reasons about responsiveness vs. resource cleanup on cancel.
## What interruption is Java has no safe way to *forcibly* kill a running thread (the old `Thread.stop()` was unsafe: it throws `UnsupportedOperationException` at JDK 25 and was removed in JDK 26). Instead it uses **cooperative cancellation**: one thread politely **requests** that another stop, and the target thread is expected to **notice and comply**. The request mechanism is **interruption**. Every thread has a boolean **interrupt status** (the "interrupt flag"). Calling `someThread.interrupt()` sets that flag. The thread can check it with `Thread.currentThread().isInterrupted()` (non-clearing) or the static `Thread.interrupted()` (which **clears** the flag). ## Why sleep/join/wait throw InterruptedException Methods that **block** — `Thread.sleep()`, `Thread.join()`, `Object.wait()`, and many `java.util.concurrent` blocking calls — are *responsive* to interruption. If the thread is blocked in one of them and someone interrupts it, the method **wakes up early and throws `InterruptedException`** instead of finishing its wait. This lets a blocked thread react quickly to a cancellation request rather than being stuck. Important subtlety: **throwing `InterruptedException` clears the interrupt flag.** So once you catch it, the thread no longer *looks* interrupted — which is why handling matters. ## When `Thread.sleep()` throws - `sleep` is static and always acts on the **current** thread, so the interrupt that matters is the sleeping thread's own. - It throws when that thread is interrupted **during** the sleep, and also when its flag was **already set** before the call: in the JDK, even `Thread.sleep(0)` with a pending interrupt throws at once instead of sleeping. - Either way the status is cleared when the exception is thrown. - A negative `millis` is a different failure: `sleep(long)` rejects it with `IllegalArgumentException`, while `sleep(Duration)` (JDK 19+) treats a negative duration as a no-op and returns without looking at the flag. ## When `Thread.join()` throws - `t.join()` throws when the **calling (joining) thread** is interrupted, not when `t` is: the Javadoc's condition is "if any thread has interrupted the current thread". - Interrupting `t` is a request to `t` alone. If `t` honours it and finishes, the joiner's `join()` simply returns normally. - Interrupting the joiner ends only the **wait**: it gets `InterruptedException` with its status cleared, and `t` keeps running. - A thread that has already terminated is not waited on. In the JDK, `join()` on it returns at once without looking at the caller's flag, so a pending interrupt stays set rather than being thrown. | | `Thread.sleep(...)` | `t.join(...)` | |---|---|---| | Whose interrupt makes it throw | the sleeping (current) thread's | the joining (current) thread's, not `t`'s | | Interrupt already pending at the call | throws at once (a negative `sleep(Duration)` is a no-op and returns) | throws at once if it would wait on a live `t`; returns normally, flag still set, if `t` has terminated | | Interrupt status after the throw | cleared | cleared | | Effect on another thread | none | `t` keeps running | ## The cardinal sin: swallowing it ```java try { Thread.sleep(1000); } catch (InterruptedException e) { // empty catch: the cancellation request is silently lost } ``` Swallowing the exception **destroys the interruption signal**: the code keeps running as if nothing happened, defeating graceful shutdown and possibly hanging an `ExecutorService` that's trying to stop. ## Correct handling — two good options **(1) Propagate it.** If your method can declare `throws InterruptedException`, just let it bubble up so the caller — who has more context — decides what to do: ```java void doWork() throws InterruptedException { Thread.sleep(1000); } ``` **(2) Catch and restore the flag.** If you *can't* propagate (e.g., you're implementing `Runnable.run()`, which doesn't declare checked exceptions), catch it, **re-set the interrupt flag**, and stop work promptly: ```java try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); // restore the cleared flag return; // stop doing more work } ``` Restoring the flag means higher-level loops checking `isInterrupted()` (or other blocking calls) will still see the cancellation and bail out. ## Why this matters in practice `ThreadPoolExecutor.shutdownNow()` works by **interrupting** the pool's worker threads, and the `ExecutorService` contract says typical implementations do the same, on a best-effort basis. If your tasks swallow `InterruptedException`, a looping task never stops and your shutdown can hang. Honoring interruption is how thread pools, timeouts (`Future.get(timeout)`), and cancellation all work. A long-running CPU loop with no blocking calls should also periodically check `Thread.currentThread().isInterrupted()` and exit when set. ## Quick rules - Don't swallow `InterruptedException`. - Propagate **or** restore-the-flag-and-stop. - Don't catch it only to log and continue as normal. - Treat interruption as "please stop soon," not "hard kill."
- Why call Thread.currentThread().interrupt() in the catch block?Because the method that threw InterruptedException cleared the interrupt flag first. Re-setting it preserves the cancellation signal so outer loops or later blocking calls still detect that the thread was asked to stop.
- If thread A is blocked in t.join() and another thread interrupts t, does A's join() throw?No. join() reacts only to the calling thread's own interrupt. Interrupting t is a request to t alone: if t's code honours it and finishes, A's join() simply returns normally, and if t ignores it, A keeps waiting. To make A stop waiting, interrupt A; its join() then throws InterruptedException while t keeps running.
- Swallowing InterruptedException with an empty catch block.
- Thinking that interrupting t makes another thread's t.join() throw; only the joiner's own interrupt does.
- Believing an interrupt that arrived before sleep() was called is ignored; sleep() throws at once.
- Thinking that interrupting a thread blocked in join() also stops the thread it is waiting for.
- Believing interrupt() force-kills the thread (it's cooperative).
- Forgetting that the flag was cleared when the exception was thrown, so it must be restored if you can't propagate.
Assumes JDK 27. sleep() and join() throw and clear the status the same way at JDK 25; Thread.stop(), named as the unsafe alternative, throws UnsupportedOperationException at 25 and was removed in JDK 26.
context
- Thread (Java SE 27 & JDK 27)docs.oracle.com
- Chapter 17. Threads and Locksdocs.oracle.com
- Thread.java at jdk-27-gagithub.com