是否有任何方法可以获得当前JVM中所有正在运行的线程的列表(包括不是由我的类启动的线程)?
是否也可以获得列表中所有线程的Thread和Class对象?
我希望能够通过代码来实现。
是否有任何方法可以获得当前JVM中所有正在运行的线程的列表(包括不是由我的类启动的线程)?
是否也可以获得列表中所有线程的Thread和Class对象?
我希望能够通过代码来实现。
当前回答
获取主线程启动的线程列表的代码片段:
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 + " ");
}
}
是的,看一下如何获取线程列表。那页上有很多例子。
这是通过编程来实现的。如果你只是想在Linux上得到一个列表,至少你可以使用这个命令:
kill -3 processid
虚拟机将做一个线程转储到标准输出。
你可以尝试这样做:
Thread.getAllStackTraces().keySet().forEach((t) -> System.out.println(t.getName() + "\nIs Daemon " + t.isDaemon() + "\nIs Alive " + t.isAlive()));
如果你需要,你可以得到更多的线程特性。
您可以使用返回所有活动线程id的getAllThreadIds。当此方法返回时,返回数组中包含的某些线程可能已经终止。
ManagementFactory.getThreadMXBean().getAllThreadIds()
获取一个可迭代集:
Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
性能:0毫秒,12个线程(Azul JVM 16.0.1, Windows 10, Ryzen 5600X)。