import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
public class GetThreads {
public static void main(String[] args) {
// 获取java线程的管理MXBean
ThreadMXBean tmxb = ManagementFactory.getThreadMXBean();
// 不需要获取同步的Monitor和synchronizer信息,仅获取线程和线程堆栈信息
ThreadInfo[] threadInfos = tmxb.dumpAllThreads(false, false);
// 遍历线程信息,打印出ID和名称
for (ThreadInfo info : threadInfos) {
System.out.println(\”[\” + info.getThreadId() + \”] \” + info.getThreadName());
}
}
}
遍历打印出来的信息,(可能每个电脑打印出来有些差别)
[5] Attach Listener
[4] Signal Dispatcher
[3] Finalizer
[2] Reference Handler
[1] main