问题
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.
回答1:
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 for os: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 subsequent sleep()
calls would ignore them. You couldn't even re-interrupt the thread because it is already interrupted.
回答2:
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:
static volatile boolean ready = false;
public static void main (final String... args) throws InterruptedException {
Thread t = new Thread () {
public void run () {
while (!ready) {
}
try {
Thread.sleep (1);
System.out.println ("Not thrown!");
}
catch (InterruptedException e) {
System.out.println ("Thrown!");
}
}
};
t.start ();
t.interrupt (); // remove this line to change the output
ready = true;
Thread.sleep (100);
}
回答3:
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.
来源:https://stackoverflow.com/questions/23710025/does-thread-sleep-throw-if-the-thread-was-already-interrupted