Course contentsShow
← Java
Lesson 2599 of 2,87048. Concurrency FundamentalsFree lesson

What Is Thread Interruption?

Understanding interruption as a cooperative mechanism for signaling a thread to stop its current work.

What Is Thread Interruption?

You'll learn: How Java's interruption mechanism allows one thread to politely signal another thread to stop what it's doing.

The Cooperative Signal

Thread interruption is Java's cooperative mechanism for requesting that a thread stop its current work. It's cooperative because the interrupted thread must actively check for and respond to the interruption signal—the JVM doesn't forcibly terminate the thread.

Think of interruption like a tap on someone's shoulder while they're focused on a task. You're signaling "Hey, you should stop what you're doing," but they choose whether to acknowledge it and how to respond.

How Interruption Works

When you call interrupt() on a thread, Java sets an internal interrupt status flag to true for that thread. The thread being interrupted doesn't immediately stop—it continues executing until it:

  1. Checks the flag using methods like Thread.interrupted() or isInterrupted()
  2. Calls a blocking method that responds to interruption (like Thread.sleep(), wait(), or blocking I/O)

If a thread is sleeping or waiting when interrupted, it wakes up immediately and throws InterruptedException. This is why you've been handling that exception—it's the interruption mechanism in action!

Why Cooperative?

Java chose cooperation over forced termination because abruptly killing a thread can leave shared resources in inconsistent states (files half-written, locks not released, etc.). By making interruption cooperative, the interrupted thread gets a chance to clean up gracefully, close resources, and exit safely.

The interrupted thread decides what "stopping work" means: it might exit entirely, finish its current task first, or even ignore the interruption in rare cases where continuing is critical.

Key Takeaway:
Thread interruption is a cooperative signaling mechanism where one thread requests another to stop, but the interrupted thread must actively check for and respond to the interruption flag.