public static final class CPUUsageSnapshot { private CPUUsageSnapshot (long time, long CPUTime) { m_time = time; m_CPUTime = CPUTime; } public final long m_time, m_CPUTime; } // end of nested class public static CPUUsageSnapshot makeCPUUsageSnapshot () { return new CPUUsageSnapshot (System.currentTimeMillis (), getProcessCPUTime ()); } public static double getProcessCPUUsage (CPUUsageSnapshot start, CPUUsageSnapshot end) { return ((double)(end.m_CPUTime - start.m_CPUTime)) / (end.m_time - start.m_time); }
[PID: 339] CPU usage: 46.8% [PID: 339] CPU usage: 51.4% [PID: 339] CPU usage: 54.8% (while loading, the demo uses nearly 100% of one of the two CPUs on my machine) ... [PID: 339] CPU usage: 46.8% [PID: 339] CPU usage: 0% [PID: 339] CPU usage: 0% (the demo finished loading all of its panels and is mostly idle) ... [PID: 339] CPU usage: 100% [PID: 339] CPU usage: 98.4% [PID: 339] CPU usage: 97% (I switched to the ColorChooserDemo panel which ran a CPU-intensive animation that used both of my CPUs) ... [PID: 339] CPU usage: 81.4% [PID: 339] CPU usage: 50% [PID: 339] CPU usage: 50% (I used Windows NT Task Manager to adjust the CPU affinity for the "java" process to use a single CPU) ...