java – 调用Thread.sleep()与*中断状态*设置?

前端之家收集整理的这篇文章主要介绍了java – 调用Thread.sleep()与*中断状态*设置?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Java文档在这一点上还不清楚.在调用Thread.sleep()之前,如果在线程上调用中断,会发生什么:
//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.

在这种情况下,’has’是一种非正式的方式来提及中断状态.这是非常可惜的,如果某个规范应该是精确和明确的,那么它是无处不在的,但它是线程原语首先.

中断状态机制的工作方式一般是如果一个线程在不中断的情况下接收到中断(因为它正在运行),那么中断基本上是等到线程被中断,此时它会导致一个原因InterruptedException的.这是该机制的一个例子.

原文链接:https://www.f2er.com/java/123690.html

猜你在找的Java相关文章