如果执行以下代码,您将看到线程中断
,但是,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/