gpt4 book ai didi

java - 如果线程未被阻塞,Thread.interrupt() 会做什么?

转载 作者:搜寻专家 更新时间:2023-11-01 07:59:06 25 4
gpt4 key购买 nike

Thread 的 JavaDoc 说 Thread.interrupt() 中断线程如下:

  • Threads blocked in one of Object's wait() methods or one of Thread's join() or sleep() methods will be woken up, their interrupt status will be cleared, and they receive an InterruptedException.
  • Threads blocked in an I/O operation of an InterruptibleChannel will have their interrupt status set and receive an ClosedByInterruptException. Also, the channel will be closed.
  • Threads blocked in a Selector will have their interrupt status set and return immediately. They don't receive an exception in this case.

如果线程不满足上述任何条件怎么办?它是被杀死还是继续运行还是什么?

提前致谢...

最佳答案

如果被中断的线程不检查 Thread.isInterrupted() 并对其进行处理,那么在未阻塞的线程上调用 Thread.interrupt() 会有效除了简单地设置标志之外别无他法。线程实现者的工作是使用 Thread.isInterrupted() 正确检查线程的状态并采取适当的操作。

关于java - 如果线程未被阻塞,Thread.interrupt() 会做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23837847/

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