一个HelloWorld 的程序,运行这个应用,Java 至少会创建几个线程呢?
代码语言:javascript复制package com.wuyu.java;
import java.util.Map;
/**
* Created by wuyupku 2019.4.13
*/
public class MemoryMonitor{
public static void main(String[] args)
{
Thread t = Thread.currentThread();
System.out.println("n线程:" t.getName() "n");
System.out.println("hello world!");
for (Map.Entry<Thread,StackTraceElement[]> entry : Thread.getAllStackTraces().entrySet())
{
Thread thread = entry.getKey();
StackTraceElement[] stackTraceElements = entry.getValue();
if (thread.equals(Thread.currentThread()))
{
continue;
}
System.out.println("n线程: " thread.getName() "n");
for (StackTraceElement element : stackTraceElements)
{
System.out.println("t" element "n");
}
}
}
}
代码语言:javascript复制线程:main
hello world!
线程: Attach Listener
线程: Reference Handler
java.lang.Object.wait(Native Method)
java.lang.Object.wait(Unknown Source)
java.lang.ref.Reference.tryHandlePending(Unknown Source)
java.lang.ref.Reference$ReferenceHandler.run(Unknown Source)
线程: Signal Dispatcher
线程: Finalizer
java.lang.Object.wait(Native Method)
java.lang.ref.ReferenceQueue.remove(Unknown Source)
java.lang.ref.ReferenceQueue.remove(Unknown Source)
java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)
在jdk8时,可见创建了5个线程。
main:主线程 Reference Handler:处理引用对象本身的垃圾回收 Finalizer:处理用户的Finalizer方法 Signal Dispatcher:外部jvm命令的转发器 Attach Listener: jvm提供一种jvm进程间通信的能力,能让一个进程传命令给另外一个进程