Home โบ Java exceptions โบ InterruptedException
InterruptedException: causes and fixes
A thread blocked in sleep, wait or a blocking queue was interrupted by another thread.
What it looks like
java.lang.InterruptedException: sleep interruptedExtends Exception in java.lang.
What it means
Interruption is Java's cooperative way to ask a thread to stop. Blocking methods such as Thread.sleep, Object.wait, BlockingQueue.take and Future.get throw InterruptedException when the thread's interrupt flag is set, and they clear the flag when they throw. Swallowing the exception silently breaks shutdown and cancellation. The correct response is to stop what you are doing or to restore the flag.
Common causes
- An executor shutdown (
shutdownNow), a timeout, or a cancel on a Future. - The application is stopping and the container sends signals to worker threads.
- Another thread called
thread.interrupt()to cancel work. - A framework cancelling a task that exceeded its time limit.
How to fix it
- Either propagate the exception or call
Thread.currentThread().interrupt()before returning, so callers see the flag. - Never catch it and ignore it with an empty block.
- Make long loops check
Thread.interrupted()orisInterrupted()and exit cleanly. - Close resources in finally or try-with-resources so an interrupt does not leak them.
Broken and fixed
try { Thread.sleep(1000); } catch (InterruptedException e) { /* ignored */ }try { Thread.sleep(1000); }
catch (InterruptedException e) {
Thread.currentThread().interrupt(); // keep the signal
return;
}๐ Unravel the failing code ๐ Unravel the fix
In Spring and enterprise code
In Spring, scheduled and async tasks are interrupted on application shutdown. Handling interruption properly makes graceful shutdown (server.shutdown=graceful) work.
Debug it faster
Paste the whole stack trace into the stack trace beautifier to hide framework frames, jump to the root cause and find the first line of your own code. It runs in your browser, so the trace never leaves your machine.
Frequently asked questions
Is InterruptedException a checked or unchecked exception?
InterruptedException is a checked exception (it extends Exception), so the compiler forces you to catch it or declare it with throws.
Which package is InterruptedException in?
It lives in java.lang, so the import is import java.lang.InterruptedException; (java.lang needs no import).
What is the first thing to check when I see InterruptedException?
Read the message and the first stack trace line in your own code. The usual causes are: An executor shutdown (shutdownNow), a timeout, or a cancel on a Future; and The application is stopping and the container sends signals to worker threads.