Answers:
要获得一个可迭代的集合:
Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
获取根的句柄ThreadGroup
,如下所示:
ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
ThreadGroup parentGroup;
while ((parentGroup = rootGroup.getParent()) != null) {
rootGroup = parentGroup;
}
现在,enumerate()
重复在根组上调用该函数。第二个参数让您递归获取所有线程:
Thread[] threads = new Thread[rootGroup.activeCount()];
while (rootGroup.enumerate(threads, true ) == threads.length) {
threads = new Thread[threads.length * 2];
}
注意我们如何重复调用enumerate()直到数组足够大以包含所有条目。
rootGroup
,您都应该使用new Thread[rootGroup.activeCount()+1]
。activeCount()
可能为零,如果为零,您将陷入无限循环。
您可以从ThreadMXBean获得有关线程的大量信息。
调用静态ManagementFactory.getThreadMXBean()方法以获取对MBean的引用。
Apache Commons用户可以使用ThreadUtils
。当前实现使用前面概述的遍历线程组方法。
for (Thread t : ThreadUtils.getAllThreads()) {
System.out.println(t.getName() + ", " + t.isDaemon());
}
在Groovy中,您可以调用私有方法
// Get a snapshot of the list of all threads
Thread[] threads = Thread.getThreads()
在Java中,如果安全管理器允许,则可以使用反射调用该方法。
代码片段获取由主线程启动的线程列表:
import java.util.Set;
public class ThreadSet {
public static void main(String args[]) throws Exception{
Thread.currentThread().setName("ThreadSet");
for ( int i=0; i< 3; i++){
Thread t = new Thread(new MyThread());
t.setName("MyThread:"+i);
t.start();
}
Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
for ( Thread t : threadSet){
if ( t.getThreadGroup() == Thread.currentThread().getThreadGroup()){
System.out.println("Thread :"+t+":"+"state:"+t.getState());
}
}
}
}
class MyThread implements Runnable{
public void run(){
try{
Thread.sleep(5000);
}catch(Exception err){
err.printStackTrace();
}
}
}
输出:
Thread :Thread[MyThread:2,5,main]:state:TIMED_WAITING
Thread :Thread[MyThread:0,5,main]:state:TIMED_WAITING
Thread :Thread[MyThread:1,5,main]:state:TIMED_WAITING
Thread :Thread[ThreadSet,5,main]:state:RUNNABLE
如果需要程序尚未启动的所有线程(包括系统线程),请删除以下条件。
if ( t.getThreadGroup() == Thread.currentThread().getThreadGroup())
现在输出:
Thread :Thread[MyThread:2,5,main]:state:TIMED_WAITING
Thread :Thread[Reference Handler,10,system]:state:WAITING
Thread :Thread[MyThread:1,5,main]:state:TIMED_WAITING
Thread :Thread[ThreadSet,5,main]:state:RUNNABLE
Thread :Thread[MyThread:0,5,main]:state:TIMED_WAITING
Thread :Thread[Finalizer,8,system]:state:WAITING
Thread :Thread[Signal Dispatcher,9,system]:state:RUNNABLE
Thread :Thread[Attach Listener,5,system]:state:RUNNABLE
public static void main(String[] args) {
// Walk up all the way to the root thread group
ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
ThreadGroup parent;
while ((parent = rootGroup.getParent()) != null) {
rootGroup = parent;
}
listThreads(rootGroup, "");
}
// List all threads and recursively list all subgroup
public static void listThreads(ThreadGroup group, String indent) {
System.out.println(indent + "Group[" + group.getName() +
":" + group.getClass()+"]");
int nt = group.activeCount();
Thread[] threads = new Thread[nt*2 + 10]; //nt is not accurate
nt = group.enumerate(threads, false);
// List every thread in the group
for (int i=0; i<nt; i++) {
Thread t = threads[i];
System.out.println(indent + " Thread[" + t.getName()
+ ":" + t.getClass() + "]");
}
// Recursively list all subgroups
int ng = group.activeGroupCount();
ThreadGroup[] groups = new ThreadGroup[ng*2 + 10];
ng = group.enumerate(groups, false);
for (int i=0; i<ng; i++) {
listThreads(groups[i], indent + " ");
}
}
要使用终端获取线程及其完整状态的列表,可以使用以下命令:
jstack -l <PID>
哪个PID是计算机上正在运行的进程的ID。要获取Java进程的进程ID,您只需运行jps
命令即可。
另外,您可以分析jstack在TDA(线程转储分析器)中生成的线程转储,例如fastthread或spotify线程分析器工具。