Thread InterruptedException explain
I'm a reasonably experienced java programmer who has, until now, always been able to ignore thecatch (InterruptedException) { } necessary on aThread.wait(), Thread.sleep() etc. I've done a thorough read of the javadocs, searched the java.sun.com site and not found ANY really detailed description of what a Thread being interrupted means.
Established Facts:
1. a Thread can throw an InterruptedException if it is sleeping/waiting or otherwise paused and another Thread calls the interrupt() method.
2. interrupt() checks whether a Thread has been interrupted and clears the info.
Questions:
1. Does the JVM ever cause an InterruptedException to be thrown as it schedules Threads for execution (someone here thinks it might) - or is it only ever called by programmer-defined code (ie. if I never call Thread.interrupt(), can I ignore the Exception).
2. What is the purpose behindinterrupted() clearing the interrupted flag? Why would you wish to do this?
3. After a Thread has been interrupted, can it carry on as if nothing has happened, or are there mysterious forces at work detailing what should happen afterwards?
I'd be grateful of some mentoring here...
>
> 1. Does the JVM ever cause an InterruptedException to
> be thrown as it schedules Threads for execution
> (someone here thinks it might) - or is it only ever
> called by programmer-defined code (ie. if I never call
> Thread.interrupt(), can I ignore the Exception).
>
I am pretty sure that on unix, at least some of them, that a thread can be killed by an external process. Probably not a good idea of course, but hopefully that would cause the exception.
A1. As far as I know, the JVM never interrupts a thread. It has to be programmer defined.
A2. Calling interrupt() on a thread is not pre-emptive. The thread is not killed or stopped. All that happens is an the interrupt flag for the thread is set to true. If the thread is in a wait or sleep, the wait or sleep throw a InterruptedException and the flag is cleared. If the thread is not in a wait or sleep, the thread continues to execute. The next time the thread reaches a wait or sleep, the InterruptedException is thrown and the flag is cleared.
The Thread.isInterrupted method checks the flag without resetting it. The Thread.interrupted method checks the flag and resets it. In some cases you may want to check for interuption (say at the top of a long running loop) when you don't do waits or sleeps.
A3. Interrupting is a signal from one thread to another to stop processing. The interrupted thread can ignore the interruption and keep processing. If a thread never wait's or sleep's it is unlikely to ever be aware that it has been interrupted. Threads can finish normally with their interrupt flag set to true.
You have to be careful when interrupting complex logic that calls other class libraries. You don't always know if other code has waits or sleeps and how they will react to an InterruptedException.I once saw an interrupt of a thread that made JDBC calls and the JDBC driver did not expect interrupts of its waits. The JDBC connection had a most gruesome death. :)