Does ThreadMXBean.getThreadCpuTime () include time spent in all states, or just RUNNABLE?

As stated in this thread, does this include time spent in BLOCKED and WAITING etc., too, or is it just RUNNABLE? The docs just say "cpu time", which is a bit vague ...

+7
source share
3 answers

ThreadMXBean.getThreadCpuTime () includes only time spent in the RUNNABLE state, but note that the way this value is calculated is platform dependent.

Here's a program that shows that getThreadCpuTime () only covers the time when the thread is actually doing something:

import java.lang.management.*; public class Test implements Runnable { public static void main(String[] args) throws Exception { long time = System.nanoTime(); Test test = new Test(); synchronized (test) { new Thread(test).start(); while (test.cpu == -1) { test.wait(); } } System.out.println("time: " + (System.nanoTime() - time)); System.out.println("cpu: " + test.cpu); } private long cpu = -1; public synchronized void run() { try { ThreadMXBean thread = ManagementFactory.getThreadMXBean(); long cpu = thread.getCurrentThreadCpuTime(); Thread.sleep(300); long time = System.nanoTime(); while (System.nanoTime() - time < 700000000); this.cpu = thread.getCurrentThreadCpuTime() - cpu; } catch (InterruptedException _) {} finally { notify(); } } } 
+3
source

Yes, this is only RUNNABLE , you can get additional statistics of time spent in other states through ThreadInfo in the following ways

There is no discrimination between TIMED_WAITING and WAITING in getWaitedTime() .

+1
source

Only performed. This is it, otherwise it would be useless.

0
source

All Articles