Java – call thread Sleep () and * interrupt status * settings?
Java documentation is unclear at this point After calling thread What happens if an interrupt is called on a thread before sleep():
//interrupt reaches Thread here try { Thread.sleep(3000); } catch (InterruptedException e) { return; }
Will interruptedexception be thrown?
Editor: if you know the answer, can you ask me to point out the relevant documents?
Solution
Yes, it throws an exception According to thread Javadoc of sleep, method:
In this case, 'has' is an informal way to refer to interrupt status It is a pity that if a specification should be precise and explicit, it is ubiquitous, but it is the thread primitive first
The working mode of interrupt state mechanism is that if a thread receives an interrupt without interruption (because it is running), the interrupt basically waits until the thread is interrupted. At this time, it will lead to a cause of interruptedexception This is an example of this mechanism