简体   繁体   中英

Active Time of Thread Java

How Can I get active time of Thread for which it was actually in running state. Minus all the Waiting and Sleeping time.

I didn't seem to find anything in Thread API that gives me desired results.

If not exact code any ideas or Pseudo Codes would be great start.

Thanks

Thanks assylias The ThreadMXBean worked for me.. Here is 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 Put up a sleep of 5 seconds just to check if it was added to CPU time. It wasn't added. Also I put up empty for loop so that processing takes some time.

Here's Output
Thread-1 :: 15600200
Thread-4 :: 15700100
Thread-3 :: 15600100
Thread-0 :: 15500100
Thread-2 :: 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM