一个普通Java程序包含哪些线程??
package com.java.threads; import java.lang.management.ManagementFactory; import java.lang.management.ThreadInfo; import java.lang.management.ThreadMXBean; import java.util.Arrays; public class Demo01 { public static void main(String[] args) { // 获取Java线程管理MXBean ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); // 获取运行的线程 ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false, false); Arrays.stream(threadInfos).forEach(threadInfo -> { System.out.println("thread-id="+threadInfo.getThreadId() + ", thread-name="+threadInfo.getThreadName()); }); } }
运行结果如下:
thread-id=6, thread-name=Monitor Ctrl-Break thread-id=5, thread-name=Attach Listener thread-id=4, thread-name=Signal Dispatcher thread-id=3, thread-name=Finalizer thread-id=2, thread-name=Reference Handler thread-id=1, thread-name=main
- Monitor Ctrl-Break:这是IDEA特有的监控线程
- Attach Listener:接收外部命令,返回处理结果
- Signal Dispatcher:分发处理发送信号给JVM的线程
- Finalizer:调用对象finalize方法的线程
- Reference Handler:清除引用的线程
- main:主线程,用户程序入口