18

The Java documentation is not clear on this point. What happens if you call interrupt on a Thread before a call to Thread.sleep():

        //interrupt reaches Thread here
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            return;
        }

Will the InterruptedException be thrown?

Please point to relevant documentation.

Roland
  • 7,525
  • 13
  • 61
  • 124

4 Answers4

16

Yes, it will throw an exception. According to the javadoc for Thread.sleep, the method:

Throws: InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

The 'has' in this case is an informal way of referring to the interrupted status. It's a shame that it is informal - if there's somewhere a spec should be precise and unambiguous, well, it's everywhere, but it's the threading primitives above all.

The way the interrupted status mechanism works in general is if that a thread receives an interruption while it's not interruptible (because it's running), then the interruption is essentially made to wait until the thread is interrupted, at which point it swoops in an causes an InterruptedException. This is an example of that mechanism.

Tom Anderson
  • 46,189
  • 17
  • 92
  • 133
8

A thread can be interrupted at any point in time, but it won't have any effect until that thread specifically checks its interrupted state with Thread.currentThread().isInterrupted() or when it reaches, or is already blocked by a call to Thread.sleep(long), Object.wait(long) or other standard JDK methods which throw InterruptedException such as those in the java.nio package. The thread's interrupt status is reset when you catch an InterruptedException or when you explicitly call Thread.interrupted() (see the documentation for that elusive method).

This JavaSpecialists article should explain a bit more about how thread interrupts work and how to deal with them properly.

BoffinBrain
  • 6,337
  • 6
  • 33
  • 59
  • 2
    *"The thread's interrupt status is reset when you catch an InterruptedException"* - Not quite. According to the javadoc, status is cleared when the exception is *thrown* not when it is caught. (And that really means *before* the `throw` statement ... or in this case, the native code equivalent of the `throw`.) – Stephen C Apr 14 '11 at 23:31
  • Yes, that's right, but of course you won't actually be able to check it or do anything else until it's been caught. I guess the long way of saying it is "The thread's interrupt status *will already be reset by the time* you catch an `InterruptedException`" – BoffinBrain Apr 15 '11 at 01:05
  • This answer completely misses the original question, not sure why OP accepted this. The question is whether calling `Thread.sleep()` when the interrupted flag is already set will result in the method throwing `InterruptedException`, and this doesn't try to answer that at all. – haridsv Nov 28 '14 at 11:42
  • I guess you can lead a horse to water, but you can't make it drink. I supplied more than enough information for the OP to work out the answer to his own question. In case you didn't get it, the answer is Yes. – BoffinBrain Nov 29 '14 at 14:25
  • @BoffinbraiN, sometimes the reason people ask questions here is not because there isn't enough information, but because there is so much information available that it obscures or obfuscates the actual answer to a simple question. Sometimes answering such a question with too much information contributes to information overload, and leaves the person wondering, "so if this paragraph of a response supposedly contains the answer, then why didn't they just say yes or no?" So thanks for adding the follow-up with an actual "yes", because your thorough answer was still leaving a little bit unclear. – Garret Wilson Oct 15 '15 at 21:18
3

You can use the following class to test the behavior. In this case, the loop is not interrupted and the thread dies when it gets to the sleep.

public class TestInterrupt{

public static void main(String[] args) throws InterruptedException {
    Thread t = new Thread(){
        public void run(){
            System.out.println("hello");
            try {
                for (int i = 0 ; i < 1000000; i++){
                    System.out.print(".");
                }
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                System.out.println("interrupted");
                e.printStackTrace();  
            }

        }
    };
    t.start();
    Thread.sleep(100);
    System.out.println("about to interrupt.");
    t.interrupt();

}

}

Zeki
  • 5,107
  • 1
  • 20
  • 27
  • I tried a variation of this program and it threw the InterruptedException, confirming that the answers of Boffin and Tom are correct. – Roland Apr 14 '11 at 22:15
  • Yes. It throws an exception, but the question is when? In the above case, the exception is not thrown until the sleep is called. – Zeki Apr 14 '11 at 22:47
  • 2
    Yes you are right. But that was the whole point: will sleep() throw the exception if the interrupt occurred before the call? And the answer is: yes! – Roland Apr 15 '11 at 12:51
1

The docs of InterruptedException seems to suggest that it can be interrupted at other times

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/InterruptedException.html

Thrown when a thread is waiting, sleeping, or otherwise paused for a long time and another thread interrupts it using the interrupt method in class Thread

Also since it is a checked exception, it will only be thrown by methods that declare it. See

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html#interrupt()

James Scriven
  • 7,784
  • 1
  • 32
  • 36