Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

The InterruptedException class

Reference for java.lang.InterruptedException: how thread interruption works, which methods throw it, and the rule to restore the interrupt flag.

java.lang.InterruptedException is a checked exception thrown when a thread that is waiting, sleeping or otherwise blocked is interrupted by another thread calling Thread.interrupt() on it. It is Java's cooperative cancellation mechanism: interruption is a request to stop what you are doing, and the blocked method reports it by throwing this exception.

Methods that declare it include Thread.sleep(), Thread.join(), Object.wait(), BlockingQueue.put()/take(), Future.get(), Lock.lockInterruptibly(), CountDownLatch.await() and ExecutorService.awaitTermination(). When the exception is thrown, the thread's interrupt status flag is cleared, so code that catches it must decide what to do with that information.

Java
public class InterruptedException extends Exception

Try it

Java Interrupting a sleeping worker

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Restoring the flag and stopping a loop cleanly

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public InterruptedException()
public InterruptedException(String s)

Interruption API on Thread:

Member Description
void interrupt() Sets the target's interrupt flag; wakes it with InterruptedException if it is blocked.
boolean isInterrupted() Reads the flag of this thread object without clearing it.
static boolean interrupted() Reads and clears the flag of the current thread.

Usage notes

  • Never swallow it. catch (InterruptedException e) {} loses the cancellation request and makes the thread impossible to stop. Either propagate (throws InterruptedException) or restore the flag with Thread.currentThread().interrupt() and return.
  • The flag is cleared on throw. After catching, isInterrupted() is false unless you set it again, which is why restoring matters for code further up the call stack.
  • Interruption does not stop a thread by itself. A busy loop that never blocks or checks isInterrupted() ignores it. Check the flag in long computations.
  • Executors: ExecutorService.shutdownNow() and Future.cancel(true) interrupt worker threads. Tasks must cooperate by reacting to interruption.
  • Plain synchronized blocks are not interruptible; a thread waiting to enter one keeps waiting. Use Lock.lockInterruptibly() if you need it.
  • Blocking I/O on classic streams generally does not respond to interrupts either; NIO channels (InterruptibleChannel) do, by closing.
  • Virtual threads support interruption the same way as platform threads.
  • Do not use Thread.stop(); since Java 20 it always throws UnsupportedOperationException. Interruption is the supported mechanism.
  • Tests and small demos usually declare throws InterruptedException on main for simplicity.