Linux java thread count Vs ThreadMxBean.getThreadCount()

141 Views Asked by At

I have a simple java program running to display mxbean threads in Centos.
When I do

cat /proc/<pid>/status|grep Threads

I get 17 threads but the program outputs very less threads.

Could you help me understand why mxbean returns lesser count?

Note: Use case is: prometheus jmx exporter uses threadmxbean interface to show thread count (which is lesser) whereas graphite returns the same value from linux command (which is higher).

import java.lang.management.*;
public class threadmxbean {

public static void main(String[] args) {
    // Get the ThreadMXBean instance
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
   // Print the number of threads currently running
   System.out.println("Number of threads-getThreadCount(): " + threadMXBean.getThreadCount());
   System.out.println("Number of threads-getDaemonThreadCount(): " + threadMXBean.getDaemonThreadCount());
   System.out.println("Number of threads-getTotalStartedThreadCount(): " + threadMXBean.getTotalStartedThreadCount());
try{Thread.sleep(500000);}catch(InterruptedException e){System.out.println(e);}
}
}

Output:

Number of threads-getThreadCount(): 5
Number of threads-getDaemonThreadCount(): 4
Number of threads-getTotalStartedThreadCount(): 5
0

There are 0 best solutions below