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
source
share