There is good news and bad news. The bad news is that a software request for CPU usage is not possible using pure Java. There is simply no API for this. A recommended alternative is to use Runtime.exec () to determine the JVM process ID (PID), call an external platform-specific command, such as ps, and analyze its output for the PID of interest. But this approach is fragile at best.
The good news is that a reliable solution can be achieved by going beyond Java and writing a few lines of C code that integrates with a Java application through the Java Native Interface (JNI).
source share