简体   繁体   中英

Calling Thread.sleep() with *interrupted status* set?

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.

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.

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.

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();

}

}

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 ()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM