When an InterruptException is thrown, the interrupt is considered to be handled and the interrupted flag is cleared.
The Thread.interrupted() static method is used in code that should be interruptable but does not normally wait() or sleep() - for instance, I/O or a lengthy calculation. For instance, you could use
at a suitable point in your loop, and handle the exception in the usual way. The interrupted() method, too, clears the interrupt flag because the interrupt is considered to be handled.
Finally, there is the Thread.isInterrupted() instance method. This is primarily a way for other threads to see if the thread has an unhandled interrupt, but it can also be used by a thread to look at its own interrupted status without clearing the flag (Thread.currentThread().isInterrupted()).
We had
a religious discussion a while ago debating whether it is good practice to make code that does not wait() or sleep() interruptable. You may find it interesting if passionate discourse about the finer points of OO and encapsulation are your thing
- Peter
[ March 27, 2002: Message edited by: Peter den Haan ]