Thursday, September 15, 2016

Features of the ProcessAPI Java9

The primary goal of the developers is, to make it easier for you to manage processes on your operating system. They start with giving you the ability to enumerate the system processes via the updated API (no hacky solutions anymore). That means, you can get different properties for each process, e.g. the PID, the process name, the user who started it, the path of the process, resource usage, etc. It is also planned, to make it easier to deal with process trees, especially destructing entire process trees and managing processes with over 100 sub-processes. To do so, it is planned to multiplex their output streams into one output stream, so you do no longer have one thread per monitored process listening for its output stream
How would it look like
We have two examples for you. In the first example, one tries to get the process-id of the current process. In the second example, one tries to get a list of all processes.
So let’s see, how retrieving the process-ID of the current process looks like, before the ProcessAPI-Update.
private static int getProcessOwnID() {
    // pName is equal to PID@COMPUTER_NAME
    String pName = ManagementFactory.getRuntimeMXBean().getName();
    return Integer.parseInt(pName.split("@")[0]);
}