What are the improvements in Process API in Java 9?

JavaObject Oriented ProgrammingProgramming

Java has improved Process API in Java 9 version that helps to manage and control operating system processes. In earlier versions, it's difficult to manage and control operating system processes by using Java. Now, new classes and interfaces have added in Java 9 to perform this task. The ProcessHandle interface identifies and provides control of native processes and also provides a method to check processes liveness and destroy the processes. The ProcessHandle.Info interface gives an Information snapshot of the process.

The Process API provides more information like:

  • Process's native process ID
  • Accumulated CPU time
  • Parent process
  • Method to destroy a process
  • Process’s Descendants, etc

Example

public class ProcessTest {
   public static void main(String args[]) {
      ProcessHandle currentProcess = ProcessHandle.current();
      System.out.println("PID: " + currentProcess.pid());
      ProcessHandle.Info currentProcessInfo = currentProcess.info();
      System.out.println("totalCpuDuration: " + currentProcessInfo.totalCpuDuration());
      System.out.println("user: " + currentProcessInfo.user());
   }
}

Output

PID: 6004
totalCpuDuration: Optional[PT0.421875S]
user: Optional[Tutorialspoint\User]
raja
Published on 17-Feb-2020 09:45:01
Advertisements