gpt4 book ai didi

java - 调用 Thread.getAllStackTraces() 时发生挂起

转载 作者:行者123 更新时间:2023-12-03 13:08:45 27 4
gpt4 key购买 nike

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

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 中,在这两种情况下都是间歇性的,软件作为独立的桌面应用程序运行

    先感谢您

    最佳答案

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

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

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

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

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

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

    备注 :如果您以前从未经历过这种情况,当您尝试读取不存在的 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() 内容的快照以读取不可变对象(immutable对象)

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

    关于java - 调用 Thread.getAllStackTraces() 时发生挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46436543/

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