views:

472

answers:

5
+3  Q: 

Thread Interrupt

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

A: 

it is interrupted where it just is. This could lead to some half-update objects and because of that, you shouldn't use it. Use a signaling var, e.g. a boolean to tell the thread to stop.

ZeissS
That's `Thread.stop` (deprecated). Not that I think `Thread.interrupt` is a good idea.
Tom Hawtin - tackline
@ZeissS as Tom noted to, interrupting a thread is only as useful as that thread is responsive to interruption. There is no guarantee that if you interrupt a thread it will in fact ever stop whatever that thread is doing. Using j.u.c classes that throws InterruptedException are pretty reliable to interrupt accordingly.
John V.
@Tom Hawtin can you explain why you dont think Thread.interrupt is a good idea?
John V.
You are right, I just reread the `interrupt()` stuff. Thomas Pronin answer is totaly right.
ZeissS
+2  A: 

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

Thomas Lötzer
+1  A: 

Here is the JDK 1.6 Javadoc:

Interrupts this thread.

Unless the current thread is interrupting itself, which is always permitted, the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.

If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.

If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException.

If this thread is blocked in a Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.

If none of the previous conditions hold then this thread's interrupt status will be set.

Interrupting a thread that is not alive need not have any effect.

Adamski
+6  A: 

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.

Thomas Pornin
`InterruptedIOException` is unlikely is practice. Interruptible I/O was enabled in Solaris briefly, but it just isn't worth it. Of course the implementation is implementation-specific. Typically (in recent times) the OS will have something to do with threads.
Tom Hawtin - tackline
+1 for the explanation by yourself and the shoting comparison.
helios
A: 

ZeissS is correct, signaling is the cleanest way to do it. you can also catch the interrupt exception and clean up that way.

avirtuos