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

前端 未结 4 1685
面向向阳花
面向向阳花 2020-12-16 14:16

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

        //interrup         


        
4条回答
  •  萌比男神i
    2020-12-16 15:02

    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.

提交回复
热议问题