A catch block that logs InterruptedException and continues makes shutdownNow wait forever. Measured with four workers, plus the propagate-or-restore rule.
An executor that will not stop
A Java service at the fictional Belgavi's Book Store polls for new help-desk cases on a worker thread. At shutdown it calls shutdownNow() on the executor and waits with awaitTermination. The wait times out, the process manager kills the JVM, and the logs show the same line every deploy: interrupted while polling, retrying.
The worker was written defensively. Thread.sleep throws a checked InterruptedException, the compiler insists that something handle it, and the handler logs it and carries on. That handler is the bug. This guide shows what the interrupt mechanism actually does, measures the effect on executor shutdown, and gives the two correct responses: propagate the exception, or restore the interrupt status before returning.
What an interrupt is
An interrupt is a request, not a forced stop. Thread.interrupt() sets the target thread's interrupt status. Code running on that thread decides what to do about it. The Java SE 25 documentation for Thread.interrupt describes the case that matters most: if the thread is blocked in Object.wait, Thread.join or Thread.sleep, its interrupt status is cleared and it receives an InterruptedException. Blocking queue methods and many other library calls follow the same convention.
So when the exception arrives, the status that carried the request has already been consumed. If the handler neither rethrows nor restores it, the request is gone, and nothing later in the thread can see that anyone asked it to stop.
The executor side is documented too. The ExecutorService.shutdownNow documentation says there are no guarantees beyond best-effort attempts to stop actively executing tasks, that typical implementations cancel through Thread.interrupt(), and that any task that fails to respond to interrupts may never terminate. Future.cancel(true) relies on the same mechanism.
The measurement
The companion example submits four tasks, each to its own single-thread executor, lets each run for 60 milliseconds, calls shutdownNow(), and then waits up to 500 milliseconds with awaitTermination:
- A polling loop that catches
InterruptedExceptionaroundThread.sleep(20)and continues: not stopped within 500 ms. It caught exactly one interrupt; nothing interrupted it again. - A queue loop that catches the exception around
poll(20, MILLISECONDS)and continues: not stopped, also exactly one interrupt caught. - A loop that restores the status in the handler and leaves: stopped.
- A loop that calls a helper which catches the exception, restores the status and returns false, while the loop condition checks
Thread.currentThread().isInterrupted(): stopped.
The two failing tasks are not hung in the usual sense. They are running normally, polling every 20 milliseconds, and would keep doing so until the process dies. The "exactly one" count is the important detail: shutdownNow interrupts once. There is no second chance for code that discards the first.
The two correct responses
If the method can declare the exception, let it propagate. That keeps the decision with the caller that understands the task's life cycle:
void pollForever(BlockingQueue<CaseEvent> queue) throws InterruptedException {
while (true) {
CaseEvent e = queue.take(); // InterruptedException ends the loop
handle(e);
}
}If it cannot, for example inside Runnable.run, a callback interface or a library helper with a fixed signature, restore the status before returning, and make the caller's loop check it:
static boolean pauseQuietly(long ms) {
try {
Thread.sleep(ms);
return true;
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // keep the request for the caller
return false;
}
}
while (!Thread.currentThread().isInterrupted()) {
pauseQuietly(20);
pollOnce();
}Restoring is not the same as stopping. It passes the request on; the loop condition is what actually honors it. A helper that restores the flag inside a loop that never checks it has fixed nothing.
The test for a clear status
Two calls look alike and behave differently. Thread.currentThread().isInterrupted() reads the status. The static Thread.interrupted() reads it and clears it. The example interrupts the current thread and then calls Thread.interrupted() twice: the results are true, then false. Using the static method as a loop condition therefore consumes the request on the first check, the same mistake as an empty catch block, only harder to see.
Where swallowed interrupts hide
- Retry helpers that sleep between attempts and catch every exception, including
InterruptedException, as a generic failure. - Wrapping in an unchecked exception:
catch (InterruptedException e) { throw new RuntimeException(e); }keeps the stack trace but clears the status; restore it before throwing. - Futures:
future.get()throwsInterruptedExceptionwhen the waiting thread is interrupted, which is about the waiter, not the task. Restore and stop waiting. - Thread pools inside libraries that call your code: if your callback swallows the interrupt, the library's own shutdown hangs.
- Virtual threads: interrupts work the same way. A swallowed interrupt on a virtual thread hangs a structured shutdown just as it hangs a platform thread.
Blocking I/O is a separate case. The Thread.interrupt documentation describes interruptible channels, which close and throw ClosedByInterruptException, and selectors, which return. Classic socket stream I/O on a platform thread may simply not respond, so a shutdown that must be prompt also needs socket timeouts or closing the socket from another thread.
Shutting down a service that has this bug
Fixing every handler is the real cure, but a service can be made to shut down predictably meanwhile. Give each loop an explicit stop flag that the shutdown hook sets in addition to calling shutdownNow. Keep the awaitTermination timeout short and log the stack traces of threads still running when it expires, so the remaining offenders identify themselves. Static analysis helps: several linters flag catch blocks for InterruptedException that neither rethrow nor call interrupt().
Auditing an existing codebase
A search for catch (InterruptedException finds the candidates quickly; the review is deciding which of four shapes each one has:
- Rethrows, or the method declares the exception and does not catch it. Correct.
- Calls
Thread.currentThread().interrupt()and then returns or breaks out of its loop. Correct. - Calls
interrupt()but stays in a loop that does not check the status. The next blocking call throws again immediately, so the loop spins at full speed instead of sleeping. Restore and leave. - Logs, ignores or converts to another exception without restoring. The bug in this guide.
Shape 3 deserves its own warning, because it looks like a fix. A loop that restores the status and then calls Thread.sleep again gets an immediate InterruptedException, restores, sleeps, and repeats, burning a core while making no progress. Catch blocks generalized to catch (Exception e) hide all four shapes; search for those around blocking calls too.
A shutdown test that catches regressions
Unit tests rarely exercise shutdown, which is why this bug survives. A test of the real shutdown path is short: build the component with its production executor, start it, wait until it is idle in its blocking call, call the same method the shutdown hook calls, and assert that awaitTermination returns true within a bound. Run it in continuous integration. It fails the day someone adds a retry helper that swallows the interrupt, which is much cheaper than discovering it from a deploy that hangs.
When ignoring the interrupt is right
Very rarely, a task must finish a short critical step even if interrupted, such as writing the last record of a batch so the file is not left half-written. The pattern is to remember the interrupt, finish the step, then restore the status:
boolean interrupted = false;
while (!done) {
try { done = writeNext(); }
catch (InterruptedException e) { interrupted = true; }
}
if (interrupted) Thread.currentThread().interrupt();The request is delayed, not lost. Anything else should treat an interrupt as "stop soon".
Verification
The companion example runs seven plain-Java checks on JDK 23.0.2, compiled with --release 17: both swallowing tasks survive shutdownNow, each catches one interrupt, both correct tasks stop, and Thread.interrupted() returns true then false. The checks are timing-based on one machine; on a loaded machine lengthen the waits rather than shortening them. Add the same shape of test to your own service: start the worker, call the real shutdown path, and assert that it completes within the timeout.
Run the example yourself
The four workers, the shutdown measurement and seven plain-Java checks. Companion code on GitHub; its README lists the prerequisites, exact commands, expected output and limits.