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

标签 java multithreading

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

相关文章:

java - 使用 Deployer 进行 JMX 调用时出现 InstanceNotFoundException

java - 使用 JPA 插件找不到 SeedStack "persistence.xml"

java - SQL 查询使用大量资源但没有结果

c - 设置共享变量时应该使用临界区还是内存屏障?

multithreading - Delphi 多线程消息循环

java - AsyncTask 一段时间后停止

java - 我可以使用 native 编译作为 Java 混淆吗

java - 如何将java监听接口(interface)从localhost更改为IP地址?

.net - 编码/设计通用线程安全限制器(即每秒将 X() 的执行限制为 Y 多次)

c# - 回避异常和 "a function evaluation has timed out"