Interrupts

for (int i = 0; i < importantInfo.length; i++) {
    // Pause for 4 seconds
    try {
        Thread.sleep(4000);
    } catch (InterruptedException e) {
        // We've been interrupted: no more messages.
        return;
    }
    // Print a message
    System.out.println(importantInfo[i]);
}
  • An interrupt is an indication to a thread that it should stop what it is doing and do something else.
  • It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate.
  • A thread sends an interrupt by invoking interrupt on the Thread object for the thread to be interrupted. For the interrupt mechanism to work correctly, the interrupted thread must support its own interruption.
  • 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.

Related concepts

Interrupts

Interrupts — Structure map

Clickable & Draggable!

Interrupts — Related pages: