Huge Difference in magnitude of getCurrentThreadCpuTime diff and System.nanoTime() diff for a code snippet in Java

Viewed 357
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
long startCPUTime = threadBean.getCurrentThreadCpuTime();
long startTime = System.nanoTime();

// Some Java Code (No Network/Database calls) .....

long endCPUTime = threadBean.getCurrentThreadCpuTime();
long endTime = System.nanoTime();

long timeElapsedCPU = endCPUTime - startCPUTime
long latency = endTime - startTime

The JVM was launced with verbose GC logs switch enabled and there was no GC logs while the above code was running. Then can we conclude that if there is a huge difference between actual latency and CPU time elapsed, then the thread was preempted by the OS ?

I have observed huge difference: latency more than 1 second and timeElpasedCPU a few milliseconds.

0 Answers
Related