InterruptedException
JavaWARNINGNotableConcurrency
Thread interrupted while blocked
Quick Answer
Always restore the interrupt flag with Thread.currentThread().interrupt() after catching InterruptedException.
What this means
Thrown when a thread is waiting, sleeping, or blocked and another thread interrupts it via Thread.interrupt().
Why it happens
- 1Thread.sleep() interrupted by shutdown signal
- 2ExecutorService.shutdownNow() interrupts running tasks
Fix
Restore interrupt flag
Restore interrupt flag
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // restore
return;
}Why this works
Restoring the flag allows calling code to detect interruption and clean up.
Code examples
Bad — swallow interruptjava
try { Thread.sleep(1000); }
catch (InterruptedException e) { /* never do this */ }Good — restore and returnjava
try { Thread.sleep(1000); }
catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}Re-throw as uncheckedjava
try { future.get(); }
catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}Same error in other languages
Sources
Official documentation ↗
Java SE Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev