3

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
markalex
  • 8,623
  • 2
  • 7
  • 32
J Jena
  • 51
  • 3
  • 1
    I guess one reports the total number of threads used by the process (including JVM internal native threads that have no Java `java.lang.Thread` instance), and the other reports the number of `java.lang.Thread` instances. – Mark Rotteveel Apr 28 '23 at 10:58

0 Answers0