gpt4 book ai didi

java - 如果线程已经中断,Thread.sleep 会抛出吗?

转载 作者:搜寻专家 更新时间:2023-10-31 20:11:39 25 4
gpt4 key购买 nike

给定一个在未被阻塞时被中断的线程(即没有抛出 InterruptedException),该线程在稍后尝试 hibernate 时会抛出 InterruptedException 吗?

documentation没有明确说明这一点:

InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

最佳答案

是的,确实如此。

文档在这一点上可能不是很清楚,但这很容易测试(例如,参见下面您自己的答案),也很容易查看规范 (HotSpot) 实现。 Thread.sleep shell 到 os:sleep,它在启动 sleep 进程之前检查中断,如您所见 here (查找 os:sleep)。

如果不是这种情况,中断将或多或少无法使用。如果它们碰巧到达任何 sleep() 调用之外,它们就会丢失,因为随后的 sleep() 调用会忽略它们。您甚至无法重新中断线程,因为它已经被中断了。

关于java - 如果线程已经中断,Thread.sleep 会抛出吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23710025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com