2
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.

insanely_sin
  • 986
  • 1
  • 14
  • 22
  • 2
    What happens if you run the code EXACTLY as you show it (i.e. with nothing where you indicate `// Some Java Code (No Network/Database calls) `? Please try some different tests before asking here, you might be able to figure it out on your own. Also, what are the results from `isThreadCpuTimeSupported()` and `isThreadCpuTimeEnabled()`? – Jim Garrison Nov 22 '17 at 20:55
  • @JimGarrison I saw that both isThreadCpuTimeSupported() and isThreadCpuTimeEnabled() are true. If I run the code exactly shown, there is never a huge difference between the two latencies. – insanely_sin Dec 04 '17 at 19:57

0 Answers0