gpt4 book ai didi

java - Thread.interrupted() 返回的值不正确 - 为什么?

转载 作者:行者123 更新时间:2023-11-29 08:27:01 25 4
gpt4 key购买 nike

如果执行以下代码,您将看到线程中断,但是,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是一件很棒的事情...

Throws:
InterruptedException - if any thread interrupted the current thread before or while the current thread was waiting. The interrupted status of the current thread is cleared when this exception is thrown.

关于java - Thread.interrupted() 返回的值不正确 - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51889126/

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