gpt4 book ai didi

java - 获取每个线程的线程组信息

转载 作者:行者123 更新时间:2023-12-03 18:28:33 24 4
gpt4 key购买 nike

我有一个线程转储,在这个线程转储中,每个线程都以如下格式显示:

"isThreadCpuTimeSupported": "true",
"getLockOwnerName": null,
"isSuspended": "false",
"stacktrace": "java.base@11.0.5/java.net.PlainSocketImpl.accept0(Native
"cpuTime": "36359.375",
"userTime": "25828.125",
"threadState": "RUNNABLE",
"id": "1",
"lockName": null,
"threadName": "main",
"isNative": "true"

我的需求是获取每个线程的threadGroup,如何获取?我必须先将它们添加到线程组中吗?我怎么知道它们是否已经是线程组的一部分?在网站上Fastthread.io当我上传线程转储时,它会自动找到线程组,它是如何做到的?

在服务器端我正在做这样的事情来获取线程转储:

ThreadMXBean tmbean = ManagementFactory.getThreadMXBean();
List<HashMap> list = new ArrayList<>();
long[] tids;
ThreadInfo[] tinfos;
tids = tmbean.getAllThreadIds();
tinfos = tmbean.getThreadInfo(tids, Integer.MAX_VALUE);

for (ThreadInfo ti : tinfos) {
try {
list.add( printThreadInfo(ti,tmbean,response ));
} catch (IOException ex) {
_logger.error("could not call printThreadInfo",ex);
}
}
...
...

对于每个ti

map.put("threadName", ti.getThreadName());

最佳答案

正如 Sarel Foyerlicht 所提到的,ThreadInfo 没有关于 ThreadGroup 的信息。我们可以利用Thread#getThreadGroup()获取组信息,步骤如下:

  1. 获取top ThreadGroup,引用引用答案[1 ]
  2. 递归迭代Thread和子ThreadGroup,得到Thread和对应的ThreadGroup的映射,见引用 [ 2 ] ThreadThreadGroup
  3. 之间的关系

下面的程序演示了上述步骤。


import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.HashMap;
import java.util.Map;

public class ThreadDumpWithGroup {
public static void main(String args[]) {
ThreadGroup top = getTopThreadGroup();
Map<Long, String> threadIdToThreadGroupNameMap = new HashMap<>();
putThreadIdToThreadGroupNameMap(top, threadIdToThreadGroupNameMap);
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
long[] threadIds;
ThreadInfo[] threadInfos;
threadIds = threadBean.getAllThreadIds();
threadInfos = threadBean.getThreadInfo(threadIds, Integer.MAX_VALUE);
for (ThreadInfo threadInfo : threadInfos) {
// May still be null as there may be new thread after constructing the map.
String threadGroupName = threadIdToThreadGroupNameMap.get(threadInfo.getThreadId());
String threadName = threadInfo.getThreadName();
long threadId = threadInfo.getThreadId();
System.out.println(
String.format("Group:%s, Thread Name:%s, Thread Id:%d", threadGroupName, threadName, threadId));
}
}

/**
* Result maybe inaccurate, see
* {@link java.lang.ThreadGroup#enumerate(Thread[], boolean) enumerate}
* {@link java.lang.ThreadGroup#enumerate(ThreadGroup[], boolean) enumerate}
**/
private static void putThreadIdToThreadGroupNameMap(ThreadGroup group,
Map<Long, String> threadIdToThreadGroupNameMap) {
Thread[] threads = new Thread[group.activeCount()];
group.enumerate(threads, false);
for (Thread thread : threads) {
if (thread == null) {
continue;
}
threadIdToThreadGroupNameMap.put(thread.getId(), group.getName());
}

ThreadGroup[] subGroups = new ThreadGroup[group.activeGroupCount()];
group.enumerate(subGroups, false);
for (ThreadGroup subGroup : subGroups) {
putThreadIdToThreadGroupNameMap(subGroup, threadIdToThreadGroupNameMap);
}
}

private static ThreadGroup getTopThreadGroup() {
ThreadGroup top = Thread.currentThread().getThreadGroup();
ThreadGroup parent;
do {
parent = top.getParent();
if (parent != null) {
top = parent;
}
} while (parent != null);
return top;
}
}

引用文献
Get a list of all threads currently running in Java
Java 101: Understanding Java threads, Part 4: Thread groups, volatility, and thread-local variables
Java Doc Thread Group

关于java - 获取每个线程的线程组信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62041280/

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