Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 812
java_geek Avatar asked Feb 18 '10 15:02

java_geek


2 Answers

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.

like image 86
Thomas Pornin Avatar answered Nov 15 '22 19:11

Thomas Pornin


The Javadoc for that method explains what happens in what situation.

like image 26
Thomas Lötzer Avatar answered Nov 15 '22 19:11

Thomas Lötzer