Listing processes
Prior to Java 9, we did not have the means to obtain a list of active processes. With Java 9 it is now possible to get the processes in a stream. There are three methods that return a Stream<ProcessHandle>
. One lists the child processes. The other lists all the descendants; the children and the children of those recursively. The third lists all the processes.
Listing children
To get the stream of process handles that can be used to control the children, the static method processHandle.children()
should be used. This will create a snapshot of the children processes of the process represented by processHandle
and create the Stream
. Since processes are dynamic there is no guarantee that during the code execution, while our program attends to the handles, that all children processes are still active. Some of them may terminate and our process may spawn new children, perhaps from a different thread. Thus the code should not assume that each of the ProcessHandle
elements...