import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;class Solution {public static void main(String[] args) {// 获取java线程管理的MXBeanThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();// 仅获取线程和线程堆栈信息ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false, false);for(ThreadInfo threadInfo: threadInfos){System.out.println("[" + threadInfo.getThreadId() + "] " + threadInfo. getThreadName());}}
}
输出:
- [5] Ctrl-Break线程是在idea中才有的,而且还是要用run启动方式
- [4] Signal Dispatcher:分发处理发送给JVM信号的线程
- [3] Finalizer :调用对象finalize方法的线程
- [2] Reference Handler:清除Reference的线程
- [1] main:main线程,用户程序入口