简体   繁体   English

创建的名为``java}`的线程(即轻量级进程)是什么?

[英]What are threads (i.e. lightweight processes) named `{java}` created for?

I wrote a Java program that sleeps for a while: 我写了一个睡眠一段时间的Java程序:

package com.mycompany.app;

import java.lang.System;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) {
    System.out.println("the current process's pid is " + ProcessHandle.current().pid());
    try {
        TimeUnit.SECONDS.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Hello World!"); // Prints the string to the console.

    }
}

I run the program with: 我运行程序:

$ java -cp target com.mycompany.app.Main 
the current process's pid is 10172

I inspect the processes that Ubuntu creates to run it: 我检查Ubuntu创建的进程来运行它:

$ pstree -pau -l -G -s 10172
systemd,1 splash
  └─lxterminal,3194,t
      └─bash,12150
          └─java,10172 -cp target com.mycompany.app.Main
              ├─{java},10173
              ├─{java},10174
              ├─{java},10175
              ├─{java},10176
              ├─{java},10177
              ├─{java},10178
              ├─{java},10179
              ├─{java},10180
              ├─{java},10181
              ├─{java},10182
              ├─{java},10183
              ├─{java},10184
              ├─{java},10185
              ├─{java},10186
              ├─{java},10187
              ├─{java},10188
              ├─{java},10189
              └─{java},10190
  • What are those threads (ie lightweight processes) named {java} created for? 名为{java}那些线程(即轻量级进程)创建的是什么?
  • Is it possible to find out what programs they run from shell using some commands? 是否有可能使用某些命令找出它们从shell运行的程序?
  • Which processes (and LWPs) are running JVM? 哪些进程(和LWP)正在运行JVM?
  • Which processes (and LWPs) are running my Java program? 哪些进程(和LWP)正在运行我的Java程序?

All of these threads belong to JVM. 所有这些线程都属于JVM。
Run jstack <pid> to get the thread list. 运行jstack <pid>以获取线程列表。

"main" #1 prio=5 os_prio=0 cpu=150.00ms elapsed=8.04s tid=0x00007f9f90011000 nid=0x107 waiting on condition  [0x00007f9f99f9f000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(java.base@11.0.1/Native Method)
        at java.lang.Thread.sleep(java.base@11.0.1/Thread.java:339)
        at java.util.concurrent.TimeUnit.sleep(java.base@11.0.1/TimeUnit.java:446)
        at com.mycompany.app.Main.main(Main.java:10)

"Reference Handler" #2 daemon prio=10 os_prio=0 cpu=0.00ms elapsed=7.95s tid=0x00007f9f901f9000 nid=0x10e waiting on condition  [0x00007f9f6c10f000]
   java.lang.Thread.State: RUNNABLE
        at java.lang.ref.Reference.waitForReferencePendingList(java.base@11.0.1/Native Method)
        at java.lang.ref.Reference.processPendingReferences(java.base@11.0.1/Reference.java:241)
        at java.lang.ref.Reference$ReferenceHandler.run(java.base@11.0.1/Reference.java:213)

"Finalizer" #3 daemon prio=8 os_prio=0 cpu=0.00ms elapsed=7.95s tid=0x00007f9f901fd800 nid=0x10f in Object.wait()  [0x00007f9f65fef000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(java.base@11.0.1/Native Method)
        - waiting on <0x0000000712108f80> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(java.base@11.0.1/ReferenceQueue.java:155)
        - waiting to re-lock in wait() <0x0000000712108f80> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(java.base@11.0.1/ReferenceQueue.java:176)
        at java.lang.ref.Finalizer$FinalizerThread.run(java.base@11.0.1/Finalizer.java:170)

"Signal Dispatcher" #4 daemon prio=9 os_prio=0 cpu=0.00ms elapsed=7.93s tid=0x00007f9f90210000 nid=0x110 runnable  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 cpu=40.00ms elapsed=7.93s tid=0x00007f9f90212000 nid=0x111 waiting on condition  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   No compile task

"C1 CompilerThread0" #7 daemon prio=9 os_prio=0 cpu=40.00ms elapsed=7.93s tid=0x00007f9f90214000 nid=0x112 waiting on condition  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   No compile task

"Sweeper thread" #8 daemon prio=9 os_prio=0 cpu=10.00ms elapsed=7.93s tid=0x00007f9f90216000 nid=0x113 runnable  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Service Thread" #9 daemon prio=9 os_prio=0 cpu=0.00ms elapsed=7.90s tid=0x00007f9f902d3800 nid=0x114 runnable  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Common-Cleaner" #10 daemon prio=8 os_prio=0 cpu=0.00ms elapsed=7.89s tid=0x00007f9f902df800 nid=0x116 in Object.wait()  [0x00007f9f656ef000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
        at java.lang.Object.wait(java.base@11.0.1/Native Method)
        - waiting on <0x0000000712002df0> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(java.base@11.0.1/ReferenceQueue.java:155)
        - waiting to re-lock in wait() <0x0000000712002df0> (a java.lang.ref.ReferenceQueue$Lock)
        at jdk.internal.ref.CleanerImpl.run(java.base@11.0.1/CleanerImpl.java:148)
        at java.lang.Thread.run(java.base@11.0.1/Thread.java:834)
        at jdk.internal.misc.InnocuousThread.run(java.base@11.0.1/InnocuousThread.java:134)

"Attach Listener" #11 daemon prio=9 os_prio=0 cpu=0.00ms elapsed=0.21s tid=0x00007f9f44001000 nid=0x126 waiting on condition  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"VM Thread" os_prio=0 cpu=0.00ms elapsed=7.95s tid=0x00007f9f901f1000 nid=0x10d runnable

"GC Thread#0" os_prio=0 cpu=0.00ms elapsed=8.01s tid=0x00007f9f90038800 nid=0x108 runnable

"G1 Main Marker" os_prio=0 cpu=0.00ms elapsed=8.00s tid=0x00007f9f90097800 nid=0x109 runnable

"G1 Conc#0" os_prio=0 cpu=0.00ms elapsed=8.00s tid=0x00007f9f90099800 nid=0x10a runnable

"G1 Refine#0" os_prio=0 cpu=0.00ms elapsed=8.00s tid=0x00007f9f9018d000 nid=0x10b runnable

"G1 Young RemSet Sampling" os_prio=0 cpu=0.00ms elapsed=8.00s tid=0x00007f9f9018f000 nid=0x10c runnable
"VM Periodic Task Thread" os_prio=0 cpu=0.00ms elapsed=7.90s tid=0x00007f9f902d6000 nid=0x115 waiting on condition

Here nid is the hexadecimal ID of a thread in the OS - you may match it to the output of pstree . 这里nid是OS中线程的十六进制ID - 您可以将它与pstree的输出匹配。

  • The first thread named main is the thread executing your code. 名为main的第一个线程是执行代码的线程。

  • Reference Handler thread is responsible for adding Weak, Soft and Phantom references discovered by Garbage Collector into their registered ReferenceQueues . Reference Handler线程负责将垃圾收集器发现的Weak,Soft和Phantom引用添加到其注册的ReferenceQueues中

  • Finalizer thread runs finalize method of the objects ready to be finalized. Finalizer线程运行准备完成的对象的finalize方法。

  • Signal Dispatcher waits for specific OS signals and handles them. Signal Dispatcher等待特定的OS信号并处理它们。 In particular, it makes thread dump on SIGQUIT , and also initiates VM shutdown process on SIGTERM , SIGINT and SIGHUP . 特别是,它在SIGQUIT上进行线程转储,并在SIGTERMSIGINTSIGHUP上启动VM关闭过程。

  • CompilerThreads perform JIT compilation of the bytecode. CompilerThreads执行字节码的JIT编译。

  • Sweeper thread cleans up obsolete compiled methods. Sweeper thread清理过时的编译方法。

  • Service Thread runs several background JVM tasks: detects low memory condition, cleans up StringTable and SymbolTable, sends deferred JVMTI events and GC notifications and so on. Service Thread运行多个后台JVM任务:检测低内存条件,清除StringTable和SymbolTable,发送延迟的JVMTI事件和GC通知等。

  • Common-Cleaner runs cleaning actions of java.lang.ref.Cleaner instances. Common-Cleaner运行java.lang.ref.Cleaner实例的清理操作。

  • Attach Listener thread supports Dynamic Attach mechanism. Attach Listener线程支持动态附加机制。 It listens for incoming Dynamic Attach connections and executes VM commands. 它侦听传入的动态连接连接并执行VM命令。 For example, it is used by jstack , jmap and jcmd utilities. 例如,它由jstackjmapjcmd实用程序使用。

  • VM Thread runs internal VM operations that require a safepoint . VM Thread运行需要一个内部VM操作还原点 The examples of such operations are deoptimization, class redifinition, biased lock revocation, thread dump, heap inspection etc. 这种操作的例子是去优化,类重定义,偏向锁撤销,线程转储,堆检查等。

  • G1 threads are involved in Garbage Collection. 垃圾收集涉及G1线程。

  • VM Periodic Task Thread is used to simulate timer interrupts. VM Periodic Task Thread用于模拟定时器中断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM