When an Thread.interrupt() is called on some thread, what happens?

java_geek picture java_geek · Feb 18, 2010 · Viewed 13.6k times · Source

When an Thread.interrupt() is called on some thread, what happens to that thread?

Answer

Thomas Pornin picture Thomas Pornin · Feb 18, 2010

The target thread is "interrupted". Mostly, a flag is set in that thread, which the thread can look at (with Thread.interrupted()). If the target thread was currently blocked on some I/O or Object.wait(), then it is awakened with, respectively, an InterruptedIOException or an InterruptedException.

Thread interruption is a gentle way to nudge a thread. It is used to give threads a chance to exit cleanly, as opposed to Thread.stop(), which is more like shooting the thread with an assault rifle.