Java 中的默认线程

发布于 2024-10-31 10:07:21 字数 108 浏览 0 评论 0原文

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

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

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

How many default Threads are started when ever we call jvm if there is no user created threads in the code ? I already Know 2 threads are going to start, those are

1) Main thread (Non Daemon Thread)

2) Garbage Collector (Daemon Thread)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦中楼上月下 2024-11-07 10:07:21

答案取决于您的 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> 亲自查看。

The answer depends on your JVM. On mine, a no-op Java program results in the following threads:

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 

You can see for yourself with jstack -l <pid>.

套路撩心 2024-11-07 10:07:21

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

该工具将允许您查看所有线程及其详细信息。

附截图。在此处输入图像描述

If you have the JDK installed you can use jvisualvm or jconsole (older versions of Java) located in the bin (Windows) directory.

The tool will allow you to see all the threads and their details.

Screenshot attached.enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文