5

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.

mafu
  • 31,798
  • 42
  • 154
  • 247

3 Answers3

8

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.

BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
  • `see the answer by @mafu for example` take a look at OP name :) – Pshemo May 17 '14 at 10:55
  • Heh. Doesn't make it any less valid though, and it saved me from typing up that test myself :) – BeeOnRope May 17 '14 at 10:56
  • 1
    I am not saying that it is less valid, just making OP look at his/her own answer as it was someone else answer is kind of strange. I would probably say something like `as you already shown in your answer` instead. But that is your answer, no pressure :) – Pshemo May 17 '14 at 11:02
  • To be fair, the Javadoc for `sleep` states (as mentioned in the Q): "**if** any thread has interrupted [...]", not "**when** any thread has interrupted [...]" so a good case can be made that it does require to throw when the Thread is in an interrupted state before the sleep method was called. And a +1 for the argument in your last paragraph. – Erwin Bolwidt Mar 30 '15 at 07:22
1

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);
}
mafu
  • 31,798
  • 42
  • 154
  • 247
  • 1
    No need to make it that complicated: `Thread.currentThread().interrupt(); Thread.sleep(1);` will answer your question as well… – Holger May 19 '14 at 11:44
  • @Holger Indeed. The idea of simply interrupting the main thread seemed so unusual it failed to occur to me :) – mafu May 19 '14 at 21:19
-5

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    That bit of documentation is from `Thread.interrupt()`, which only describes what happens when you call that function. You have to look to `Thread.sleep()` to see what it does when the `interrupted` flag is set. `Thread.sleep()` says it throws "InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown", which I'd interpret to to include the case where the `interrupted` flag is set. – Kenster May 17 '14 at 10:53
  • Those are sufficient, but not necessary conditions for an interrupt to be thrown. You left out the last part of the doc: `If none of the previous conditions hold then this thread's interrupt status will be set.` In that case, the interrupted status will cause _subsequent_ calls to interruptible methods to throw an interrupt (per the documentation on those methods - at least if you read between the lines). – BeeOnRope May 17 '14 at 10:54