📜  Java 9 Process API

📅  最后修改于: 2020-10-14 00:42:52             🧑  作者: Mango

Java 9 Process API改进

Java改进了Java 9版本的流程API,该API有助于管理和控制操作系统流程。

在早期版本中,使用Java编程来管理和控制操作系统进程很复杂。现在,添加了新的类和接口来执行此任务。

下表中将新方法添加到java.lang.Process类。

Modifier and Type Method Description
boolean supportsNormalTermination() It returns true if the implementation of destroy() is to normally terminate the process, else returns false.
ProcessHandle toHandle() It returns a ProcessHandle for the Process.
long pid() It returns the native process ID of the process.
Stream children() It returns a snapshot of the direct children of the process.
Stream descendants() It returns a snapshot of the descendants of the process.
ProcessHandle.Info info() It returns a snapshot of information about the process.
CompletableFuture onExit() It returns a CompletableFuture for the termination of the Process.

添加了新的接口ProcessHandle和ProcessHandle.Info。

Java ProcessHandle接口

ProcessHandle帮助处理和控制过程。我们可以监控流程,列出其子级,获取信息等。

此接口包含静态工厂方法,这些方法返回基于值,不可变且线程安全的实例。

Java ProcessHandle接口Signature(签名)

public interface ProcessHandle extends Comparable

此接口包含以下方法。

Modifier and Type Method Description
static Stream allProcesses() It returns a snapshot of all processes visible to the current process.
Stream children() It returns a snapshot of the current direct children of the process.
int compareTo(ProcessHandle other) It compares this ProcessHandle with the specified ProcessHandle for order.
static ProcessHandle current() It returns a ProcessHandle for the current process.
Stream descendants() It returns a snapshot of the descendants of the process.
boolean destroy() It requests the process to be killed.
boolean destroyForcibly() It requests the process to be killed forcibly.
boolean equals(Object other) It returns true if other object is non-null, is of the same implementation, and represents the same system process; otherwise it It returns false.
int hashCode() It returns a hash code value for this ProcessHandle.
ProcessHandle.Info info() It returns a snapshot of information about the process.
boolean isAlive() It tests whether the process represented by this ProcessHandle is alive.
static Optional of(long pid) It returns an Optional for an existing native process.
CompletableFuture onExit() It returns a CompletableFuture for the termination of the process.
Optional parent() It returns an Optional for the parent process.
long pid() It returns the native process ID of the process.
boolean supportsNormalTermination() It returns true if the implementation of destroy() normally terminates the process.

Java ProcessHandle.Info接口

它被添加到Java 9中,并用于提供有关该过程的信息。它是ProcessHandle接口的嵌套接口。

Java ProcessHandle.Info接口签名

public static interface ProcessHandle.Info
Modifier and Type Method Description
Optional arguments() It returns an array of Strings of the arguments of the process.
Optional command() It returns the executable pathname of the process.
Optional commandLine() It returns the command line of the process.
Optional startInstant() It returns the start time of the process.
Optional totalCpuDuration() It returns the total cputime accumulated of the process.
Optional user() It returns the user of the process.

Java 9 Process API示例

public class ProcessApiExample {
public static void main(String[] args) {
ProcessHandle currentProcess = ProcessHandle.current(); // Current processhandle
System.out.println("Process Id: "+currentProcess.pid()); // Process id 
System.out.println("Direct children: "+ currentProcess.children()); // Direct children of the process
System.out.println("Class name: "+currentProcess.getClass()); // Class name
System.out.println("All processes: "+ProcessHandle.allProcesses()); // All current processes 
System.out.println("Process info: "+currentProcess.info()); // Process info
System.out.println("Is process alive: "+currentProcess.isAlive()); 
System.out.println("Process's parent "+currentProcess.parent());  // Parent of the process
}
}

输出:

Process Id: 9111
Direct children: java.util.stream.ReferencePipeline$2@6adca536
Class name: class java.lang.ProcessHandleImpl
All processes: java.util.stream.IntPipeline$1@28f67ac7
Process info: [user: Optional[javatpoint], 
cmd: /usr/lib/jvm/java-9-oracle/bin/java, args: [-Dfile.encoding=UTF-8, 
-classpath, /home/javatpoint/irfan/java 9/java 9 programms/Java9Features/bin, 
ProcessApiExample], startTime: Optional[2017-11-18T06:30:57.940Z], totalTime: Optional[PT0.25S]]
Is process alive: true
Process's parent: Optional[7509]