So, I have a program with multiples threads, and for some reason, I don't know why, the CPU usage increases randomly. I need to know which class or thread is using the CPU.
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
for(Long threadID : threadMXBean.getAllThreadIds()) {
ThreadInfo info = threadMXBean.getThreadInfo(threadID);
logging.info("Thread name: " + info.getThreadName());
logging.info("Thread state: " + info.getThreadState());
long cpuTime= threadMXBean.getThreadCpuTime(threadID);
logging.info(String.format("CPU time: %.2f s",cpuTime==0?cpuTime/1000000000d));
}
I need to print something like this
2019-10-08 20:47:41 INFO - thread name: xxxx
2019-10-08 20:47:41 INFO - thread cpu percent: xx,x%