加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 05:02:11 所属栏目:Java 来源:网络整理
导读:Java文档在这一点上还不清楚.在调用Thread.sleep()之前,如果在线程上调用中断,会发生什么: //interrupt reaches Thread here try { Thread.sleep(3000); } catch (InterruptedException e) { return; } 是否会抛出InterruptedException? 编辑:如果你知道
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的.这是该机制的一个例子.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读