Given a thread which was interrupted while it was not blocked (i.e. no InterruptedException was thrown), does that thread throw an InterruptedException when it later attempts to sleep?
The documentation does not state this clearly:
InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
While I found no document stating this is mandatory, I found that on my system (32 bit client VM 1.7), attempting to sleep when the interrupted flat is set does throw. Test code:
Yes, it does.
The documentation perhaps isn't crystal clear on that point, but this is easy to both test (see the your own answer below, for example), and to see the canonical (HotSpot) implementation. Thread.sleep shells out to
os:sleep
which checks interruption before staring the sleep process as you can see here (look foros:sleep
).If this wasn't the case, interrupts would be more or less impossible to use. If they happened to arrive outside of any
sleep()
call they would be lost as subsequentsleep()
calls would ignore them. You couldn't even re-interrupt the thread because it is already interrupted.No.
The documentation also says "If this thread is blocked [my emphasis] 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."
That excludes the case you mention.