I wrote a Java program that sleeps for a while:

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 {
        } catch (InterruptedException e) {

        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:

$ pstree -pau -l -G -s 10172
systemd,1 splash
          └─java,10172 -cp target com.mycompany.app.Main
  • What are those threads (i.e. lightweight processes) named {java} created for?
  • Is it possible to find out what programs they run from shell using some commands?
  • Which processes (and LWPs) are running JVM?
  • Which processes (and LWPs) are running my Java program?

2 Answers

apangin On Best Solutions

All of these threads belong to JVM.
Run jstack <pid> to get the thread list.

"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([email protected]/Native Method)
        at java.lang.Thread.sleep([email protected]/Thread.java:339)
        at java.util.concurrent.TimeUnit.sleep([email protected]/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([email protected]/Native Method)
        at java.lang.ref.Reference.processPendingReferences([email protected]/Reference.java:241)
        at java.lang.ref.Reference$ReferenceHandler.run([email protected]/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([email protected]/Native Method)
        - waiting on <0x0000000712108f80> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove([email protected]/ReferenceQueue.java:155)
        - waiting to re-lock in wait() <0x0000000712108f80> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove([email protected]/ReferenceQueue.java:176)
        at java.lang.ref.Finalizer$FinalizerThread.run([email protected]/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([email protected]/Native Method)
        - waiting on <0x0000000712002df0> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove([email protected]/ReferenceQueue.java:155)
        - waiting to re-lock in wait() <0x0000000712002df0> (a java.lang.ref.ReferenceQueue$Lock)
        at jdk.internal.ref.CleanerImpl.run([email protected]/CleanerImpl.java:148)
        at java.lang.Thread.run([email protected]/Thread.java:834)
        at jdk.internal.misc.InnocuousThread.run([email protected]/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.

  • The first thread named main is the thread executing your code.

  • Reference Handler thread is responsible for adding Weak, Soft and Phantom references discovered by Garbage Collector into their registered ReferenceQueues.

  • Finalizer thread runs finalize method of the objects ready to be finalized.

  • Signal Dispatcher waits for specific OS signals and handles them. In particular, it makes thread dump on SIGQUIT, and also initiates VM shutdown process on SIGTERM, SIGINT and SIGHUP.

  • CompilerThreads perform JIT compilation of the bytecode.

  • Sweeper thread cleans up obsolete compiled methods.

  • 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.

  • Common-Cleaner runs cleaning actions of java.lang.ref.Cleaner instances.

  • Attach Listener thread supports Dynamic Attach mechanism. It listens for incoming Dynamic Attach connections and executes VM commands. For example, it is used by jstack, jmap and jcmd utilities.

  • VM Thread runs internal VM operations that require a safepoint. The examples of such operations are deoptimization, class redifinition, biased lock revocation, thread dump, heap inspection etc.

  • G1 threads are involved in Garbage Collection.

  • VM Periodic Task Thread is used to simulate timer interrupts.

David Woods On

Here is the best I can find, but I am not sure what the commands listed under CMD mean

$ ps -eL -q 10172
  PID   LWP TTY          TIME CMD
10172 10172 pts/8    00:00:00 java
10172 10173 pts/8    00:00:00 java
10172 10174 pts/8    00:00:00 GC Thread#0
10172 10175 pts/8    00:00:00 GC Thread#1
10172 10176 pts/8    00:00:00 G1 Main Marker
10172 10177 pts/8    00:00:00 G1 Conc#0
10172 10178 pts/8    00:00:00 G1 Refine#0
10172 10179 pts/8    00:00:00 G1 Refine#1
10172 10180 pts/8    00:00:00 G1 Young RemSet
10172 10181 pts/8    00:00:00 VM Thread
10172 10182 pts/8    00:00:00 Reference Handl
10172 10183 pts/8    00:00:00 Finalizer
10172 10184 pts/8    00:00:00 Signal Dispatch
10172 10185 pts/8    00:00:00 C2 CompilerThre
10172 10186 pts/8    00:00:00 C1 CompilerThre
10172 10187 pts/8    00:00:00 Sweeper thread
10172 10188 pts/8    00:00:00 Service Thread
10172 10189 pts/8    00:00:00 VM Periodic Tas
10172 10190 pts/8    00:00:00 Common-Cleaner