【问题标题】:Hang occurring when Thread.getAllStackTraces() is called调用 Thread.getAllStackTraces() 时发生挂起
【发布时间】:2018-03-08 06:32:57
【问题描述】:

我正在运行下面的代码来显示我的程序正在运行的线程

public void printTasks() {
    System.out.println("get threads start");
    for (Thread thread : Thread.getAllStackTraces().keySet()) {
        //This block filters the non-java threads running
        if (!thread.getThreadGroup().getName().equals("system")) {
            System.out.println("hello");
        }
    }
    System.out.println("get threads end");
}

问题是有时代码在打印“获取线程启动”之后就挂起,我怀疑挂起发生在“Thread.getAllStackTraces()”这一行

注意:我的程序使用线程执行一组任务,因此,它创建了大约 70 个线程并且挂起是间歇性的,我调用此方法的每 6 或 7 次中只有 1 次出现此问题

我的问题是:

  • 这是一个已知问题吗?
  • 有没有办法防止这种行为?
  • 是否有更安全的方法来列出正在运行的线程?

编辑:我使用的是 java 1.8,问题发生在 Linux OEL 和 Windows Server 中,在这两种情况下都是间歇性的,软件作为独立桌面应用程序运行

提前谢谢你

【问题讨论】:

  • 您运行的是什么版本的 Java,在什么操作系统上运行?这是一个独立的应用程序还是某种应用程序服务器?

标签: java multithreading performance freeze


【解决方案1】:

我刚刚发现了问题所在(至少这是我的假设)。

用于检索正在运行的线程的代码遇到了竞争条件。

我的程序创建的线程在不断变化,有些开始有些在很短的时间内结束(1 秒或更短)

函数Thread.getAllStackTraces() 返回线程和堆栈跟踪(a 行)的 HashMap,然后在下一行(b 行)我试图获取线程的组名

 for (Thread thread : Thread.getAllStackTraces().keySet()) { <--- a
     if (!thread.getThreadGroup().getName().equals("system")) { <--- b

但线程持续时间太短以至于在到达第二行之前它就消失了,因此我最终尝试使用无效键从映射中获取值(这是竞争条件)

注意:如果您以前从未遇到过这种情况,当您尝试读取不存在的 HashMap 的值时,您可能最终会永远等待结果

解决方案

在尝试读取其属性之前确保线程仍然存在

public void printTasks() {
    System.out.println("get threads start");
    for (Thread thread : Thread.getAllStackTraces().keySet()) {
        //This block filters the non-java threads running
        if (Thread.getAllStackTraces().containsKey(thread) && //<--new line here
            thread.getThreadGroup().getName().equals("system")) {
            System.out.println("hello");
        }
    }
    System.out.println("get threads end");
}

第二种方法是尝试获取 getAllStackTraces() 内容的快照以读取不可变对象

欢迎提供更好的解决方案,希望这对某人有所帮助

【讨论】:

    猜你喜欢
    • 2011-10-05
    • 2017-04-26
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 2021-07-07
    • 2017-12-05
    • 1970-01-01
    相关资源
    最近更新 更多