Java 文档在这一点上并不清楚。如果在调用 Thread.sleep() 之前调用 Thread 中断会发生什么情况? :
//interrupt reaches Thread here
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
return;
}
请问InterruptedException被扔?
请指出相关文档。
最佳答案
是的,它会抛出异常。根据 Thread.sleep 的 javadoc , 方法:
Throws: InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
在这种情况下,“有”是指代中断状态的非正式方式。遗憾的是它是非正式的 - 如果有某个地方的规范应该是精确和明确的,那么它无处不在,但它首先是线程原语。
中断状态机制的一般工作方式是,如果线程在不可中断(因为它正在运行)时接收到中断,那么中断实际上是等待线程被中断,此时它突然进入导致 InterruptedException。这是该机制的一个示例。
关于java - 在设置了*中断状态*的情况下调用 Thread.sleep()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5669760/