Does Thread.sleep throw if the thread was already

2019-05-03 09:16发布

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.

3条回答
来,给爷笑一个
2楼-- · 2019-05-03 09:47

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);
}
查看更多
Rolldiameter
3楼-- · 2019-05-03 10:04

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.

查看更多
Fickle 薄情
4楼-- · 2019-05-03 10:09

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.

查看更多
登录 后发表回答