Home  >  Article  >  Java  >  What are the improvements to Process API in Java 9?

What are the improvements to Process API in Java 9?

WBOY
WBOYforward
2023-08-20 22:37:34830browse

在Java 9中,Process API有哪些改进?

Java has improved the Process API in the Java 9 version, which can help manage and control operating system processes. In earlier versions, it was difficult to manage and control operating system processes using Java. Now, in Java 9 new classes and interfaces have been added to perform this task. ProcessHandle The interface is used to identify and control the local process, and provides methods to check process survivability and destroy the process. ProcessHandle.Info The interface provides an information snapshot of the process.

Process API Provides more information, such as:

  • The native process ID of the process
  • Cumulative CPU time
  • Parent process
  • Methods to destroy a process
  • Descendants of a process, etc.

Example

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

Output

<strong>PID: 6004
totalCpuDuration: Optional[PT0.421875S]
user: Optional[Tutorialspoint\User]</strong>

The above is the detailed content of What are the improvements to Process API in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete