Active Time Thread Java

How can I get Thread active time, for which it was actually in working condition. Minus all the time waiting and sleeping.

It seems that I did not find anything in the Thread API, which gave me the desired results.

If not accurate code, any ideas or pseudo codes would be a great start.

thank

+4
source share
1 answer

Thanks assylias ThreadMXBean worked for me. Here is a sample code of what I did.

@Override
public void run() {
   System.out.println(Thread.currentThread().getName());
   try {
    for(int i = 999999999; i > 0; i--){}
    Thread.sleep(5000);
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    long threadCpuTime = threadBean.getCurrentThreadCpuTime();
    System.out.println(Thread.currentThread().getName() + " :: " + threadCpuTime);
   } catch (InterruptedException e) {
    e.printStackTrace();
   return;
   }
}

I will put it to sleep for 5 seconds to check if it is added to the processor time. It has not been added. In addition, I run an empty loop, so the processing takes some time.

Here Output
Thread-1 :: 15600200
Thread-4 :: 15700100
Thread-3 :: 15600100
Thread-0 :: 15500100
Thread-2 :: 0
+3
source

All Articles