1

In Linux, many Java threads states are running, but their threads' parent Java process is sleeping. Why?

For example, the Java process with pid is 5197:

[root@ov7-ops-test-99 tmp]# ps -eo pid,s,lwp,command|grep activity
 5197 S  5197 java -jar /opt/work/jenkins/workspace/.......

but threads with pid 5197 state is:

[root@ov7-ops-test-99 tmp]# ps  -eLo  pid,lwp,s,%cpu,%mem,command|awk '/activity/ {if(index($3,"R")>0){print $1,$2,$3,$4,$8}}'
5197 5303 R 0.4 /opt/work/jenkins/workspace/....
5197 5563 R 0.5 /opt/work/jenkins/workspace/....
5197 7326 R 1.4 /opt/work/jenkins/workspace/....
5197 7330 R 1.4 /opt/work/jenkins/workspace/....
5197 7334 R 1.4 /opt/work/jenkins/workspace/....
5197 7338 R 1.4 /opt/work/jenkins/workspace/....
5197 7339 R 1.4 /opt/work/jenkins/workspace/....
5197 7340 R 1.4 /opt/work/jenkins/workspace/....
5197 7345 R 1.4 /opt/work/jenkins/workspace/....
5197 7346 R 1.4 /opt/work/jenkins/workspace/....
5197 7349 R 1.5 /opt/work/jenkins/workspace/....
5197 7357 R 1.4 /opt/work/jenkins/workspace/....
5197 7360 R 1.4 /opt/work/jenkins/workspace/....
5197 7365 R 1.4 /opt/work/jenkins/workspace/....
5197 7368 R 1.4 /opt/work/jenkins/workspace/....
5197 7369 R 1.4 /opt/work/jenkins/workspace/....
5197 7370 R 1.4 /opt/work/jenkins/workspace/....
glglgl
  • 89,107
  • 13
  • 149
  • 217

1 Answers1

1

Either the parent thread started worker threads and then went to sleep or you are observing background tasks doing the work. Most likely 5197 is the main thread, as per this answer you can confirm it by running jps -v.

There is not enough details in your example. You would have to look at the Java thread names and reconcile them with Jenkins source code. It looks like you are dealing with builds inside the Jenkins workspace in which case the main could be the agent startup code and running threads could be the workers.

The below code causes the same process state that you observed:

public static void main(String[] args) throws Exception {
    Thread t1 = new Thread(() -> {
        int i = 0;
        while (true) {
            i++;
        }
    });
    t1.start();
    t1.join();
}
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • I wouldn't be surprised if the thread in question is blocking on an `ExecutorService`'s `awaitTermination` call. – Powerlord Dec 24 '18 at 09:58
  • the threads are executed by ThreadPool,this java process is a spring boot application – xianshuang zhang Dec 24 '18 at 11:59
  • the java process is a master process,and threads(LWP= Light weight Process) are created by master using CLONE_THREAD(linux kernel).so why the master is sleeping when many lwp is running? – xianshuang zhang Dec 24 '18 at 12:02