java - Thread.interrupted() 返回的值不正确 - 为什么?
问题描述
如果您执行以下代码,您将看到线程 is interrupted
,但是,Thread.interrupted()
将返回false
public class Test {
public static void main(String args[]) {
Thread t = new Thread() {
public void run() {
try {
synchronized (this) {
wait();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(Thread.interrupted());
}
}
};
t.start();
t.interrupt();
for (;;) {
}
}
}
请帮助我理解为什么会这样false
,而应该是true
解决方案
JavaDocs是一个很棒的东西......
抛出:
InterruptedException - 如果任何线程在当前线程等待之前或期间中断了当前线程。抛出该异常时清除当前线程的中断状态。
推荐阅读
- c - 如何强制野牛去某一行并从该行解析?
- oracle - 格式字符串不匹配
- swiftui - 如何像 SwiftUI 中的 WhatsApp 一样在滑动时关闭键盘
- node.js - Apollo-server-express 返回“无法获取 /”而不是 Graphql 游乐场
- python - 如何解决 ValueError: Expected 2D array, got scalar array instead of error in python?
- powershell - 格式化字符串命令在控制台中有效,但在脚本中无效。[POWERSHELL]
- pycharm - 在 Pycharm 中达到任意程序状态后如何测试任意代码?
- jboss - 禁用 TLS V1.1,在 WildFly 20.0.1 上仅启用 TLSv1.2
- ruby-on-rails - Rails 6 纱线库不会在生产中加载
- selenium-webdriver - OKTA 应用程序的 API 自动化