Java中的默认线程

时间:2011-04-11 09:23:00

标签: java multithreading

如果代码中没有用户创建的线程,我们调用jvm时会启动多少个默认线程?我已经知道2个线程将开始,那些是

1)主线程(非守护程序线程)

2)垃圾收集器(守护程序线程)

2 个答案:

答案 0 :(得分:7)

答案取决于您的JVM。在我看来,一个无操作的Java程序会产生以下线程:

2011-04-11 10:25:44
Full thread dump Java HotSpot(TM) 64-Bit Server VM (19.1-b02 mixed mode):

"Attach Listener" daemon prio=10 tid=0x0000000040222000 nid=0x606a waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
    - None

"Low Memory Detector" daemon prio=10 tid=0x0000000040211000 nid=0x6046 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
    - None

"CompilerThread1" daemon prio=10 tid=0x00007f80d0004000 nid=0x6045 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
    - None

"CompilerThread0" daemon prio=10 tid=0x00007f80d0001000 nid=0x6044 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
    - None

"Signal Dispatcher" daemon prio=10 tid=0x000000004020d800 nid=0x6043 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
    - None

"Finalizer" daemon prio=10 tid=0x00000000401eb000 nid=0x6042 in Object.wait() [0x00007f80d6053000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000000eb561300> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:118)
    - locked <0x00000000eb561300> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:134)
    at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:159)

   Locked ownable synchronizers:
    - None

"Reference Handler" daemon prio=10 tid=0x00000000401e9000 nid=0x6041 in Object.wait() [0x00007f80d6154000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000000eb5611d8> (a java.lang.ref.Reference$Lock)
    at java.lang.Object.wait(Object.java:485)
    at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:116)
    - locked <0x00000000eb5611d8> (a java.lang.ref.Reference$Lock)

   Locked ownable synchronizers:
    - None

"main" prio=10 tid=0x0000000040184800 nid=0x603b runnable [0x00007f80db081000]
   java.lang.Thread.State: RUNNABLE
    at c.main(c.java:1)

   Locked ownable synchronizers:
    - None

"VM Thread" prio=10 tid=0x00000000401e2000 nid=0x6040 runnable 

"GC task thread#0 (ParallelGC)" prio=10 tid=0x0000000040197800 nid=0x603c runnable 

"GC task thread#1 (ParallelGC)" prio=10 tid=0x0000000040199800 nid=0x603d runnable 

"GC task thread#2 (ParallelGC)" prio=10 tid=0x000000004019b800 nid=0x603e runnable 

"GC task thread#3 (ParallelGC)" prio=10 tid=0x000000004019d000 nid=0x603f runnable 

"VM Periodic Task Thread" prio=10 tid=0x000000004021c000 nid=0x6047 waiting on condition 

您可以通过jstack -l <pid>了解自己。

答案 1 :(得分:2)

如果安装了JDK,则可以使用位于bin(Windows)目录中的jvisualvmjconsole(旧版Java)。

该工具可让您查看所有主题及其详细信息。

附加截图。enter image description here