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(签名)
此接口包含以下方法。
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接口签名
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示例
输出:
0
相关文章:Java中的switch语句 switch语句是多向分支语句。它提供了一种简单的方法,可以根据表达式的值将执行分派到代码的不同部分。基本上, […]...
Java中的静态方法与实例方法 实例方法 实例方法是需要先创建其类的对象才能调用的方法。要调用实例方法,我们必须创建在其内定义该类的Objec […]...
Java Swing-JButton Java JButton JButton类用于创建具有平台独立实现的带标签的按钮。按下按钮后,应用程序将执行某 […]...
Java Swing-JFileChooser Java JFileChooser JFileChooser类的对象表示一个对话框,用户可以从中选择文件。它继 […]...
Java LayoutManagers-GridLayout Java GridLayout GridLayout用于将组件排列在矩形网格中。每个矩形中显示一个组件。 Gr […]...
Java8 Clock类 Java Clock类 Java Clock类用于使用时区提供对当前时刻,日期和时间的访问。它继承了Objec […]...
Java int转换为long Java将int转换为long 我们可以使用赋值运算符在Java中将int转换为long。不需要做任何额外的事 […]...
Java队列接口 Java队列接口 Java Queue接口以FIFO(先进先出)的方式对元素进行排序。在FIFO中,首先删除第 […]...