Ad Space — Top Banner

InterruptedException

Java Programming Language

Severity: Moderate

What Does This Error Mean?

An InterruptedException means a thread that was paused (sleeping, waiting, or blocked) was interrupted by another thread before it finished waiting. It is Java's mechanism for cancelling a thread's current wait operation. This is a checked exception — Java forces you to handle it wherever you call methods like Thread.sleep(), wait(), or take() on a blocking queue. How you handle it matters a lot — ignoring it incorrectly can cause bugs.

Affected Models

  • Java 8
  • Java 11
  • Java 17
  • Java 21
  • All Java versions

Common Causes

  • Another thread called Thread.interrupt() on your thread while it was sleeping or waiting
  • A thread pool or executor service is shutting down and interrupting all its worker threads
  • A blocking operation (like take() on a LinkedBlockingQueue) was interrupted as part of a cancellation
  • A timeout mechanism interrupted a thread that was waiting too long
  • Application shutdown code interrupts background threads to make them stop cleanly

How to Fix It

  1. Understand what interrupted your thread. Is it a shutdown signal? A timeout? A cancellation request? The response depends on the context of your thread's work.

    InterruptedException is a cooperative cancellation signal. Whoever called interrupt() is asking your thread to stop what it is doing.

  2. If your thread should stop when interrupted, catch the exception and exit the thread cleanly. Clean up any resources, then let the method return or throw the exception upward.

    Example: try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return; } — restore the interrupt flag and return.

  3. Always restore the interrupt flag when you catch InterruptedException and do not re-throw it. Call Thread.currentThread().interrupt() inside the catch block. This preserves the interrupt signal for callers.

    When you catch InterruptedException, Java clears the interrupt flag. If you just swallow the exception, the interrupt signal is lost and calling code cannot know the thread was interrupted.

  4. If your method cannot handle the interruption itself, re-throw it or declare 'throws InterruptedException' in your method signature so the caller can decide how to handle it.

    Propagating the exception is often the cleanest approach. Let the calling code decide whether to stop, retry, or log and continue.

  5. Never use an empty catch block for InterruptedException: catch (InterruptedException e) { } — this silently swallows the signal and can cause threads that should stop to keep running, causing hangs on shutdown.

    This is one of the most common threading bugs in Java. Always either restore the interrupt flag or re-throw the exception.

When to Call a Professional

InterruptedException is a normal part of Java's thread management — it is not always a bug. The important thing is to handle it correctly: either restore the interrupt flag or propagate the exception. Never silently swallow InterruptedException with an empty catch block.

Frequently Asked Questions

Why is InterruptedException checked when most Java exceptions are unchecked?

Because interruption is something you are expected to plan for when writing multi-threaded code. The compiler forcing you to handle it is intentional — it makes you think about what your thread should do when asked to stop. If it were unchecked, developers would often forget to handle it, leading to threads that never terminate cleanly.

What happens if I call Thread.sleep() and my thread is already interrupted?

Thread.sleep() immediately throws InterruptedException if the interrupt flag is already set when it is called — it does not actually sleep at all. This ensures that a thread marked for interruption is not able to delay its response by sleeping. Check Thread.currentThread().isInterrupted() if you need to know the flag's current state.

What is the difference between Thread.interrupt() and Thread.stop()?

Thread.interrupt() is a polite request — it sets a flag asking the thread to stop when convenient. The thread decides how to respond. Thread.stop() was a forceful stop that could leave objects in inconsistent states. It is deprecated and should never be used. Always use interrupt() for cooperative thread cancellation.