Java - (Thread) Interrupt

> Procedural Languages > Java > Java - Concurrency (Parallel Work)

1 - About

3 - Java

Many methods that throw InterruptedException, such as sleep, are designed to cancel their current operation and return immediately when an interrupt is received.

A thread sends an interrupt by invoking interrupt on the Thread object for the thread to be interrupted.

Advertising

3.1 - The Interrupt Status Flag

The interrupt mechanism is implemented using an internal flag known as the interrupt status. Invoking Thread.interrupt sets this flag. When a thread checks for an interrupt by invoking the static method Thread.interrupted, interrupt status is cleared. The non-static isInterrupted method, which is used by one thread to query the interrupt status of another, does not change the interrupt status flag.

By convention, any method that exits by throwing an InterruptedException clears interrupt status when it does so. However, it's always possible that interrupt status will immediately be set again, by another thread invoking interrupt.

4 - Documentation / Reference