JShell 是一种Java Shell工具,用于执行简单的Java语句,例如类,方法,接口,枚举 等。对其进行评估,并在命令行提示符下显示结果。
Java改进了Process API,以管理和控制操作系统进程。ProcessHandle 接口标识并提供对本机进程,检查进程 活动性和破坏 进程的方法的控制。ProcessHandle.Info接口提供流程的信息快照 。
在下面的代码片段,我们可以打印PID,信息,孩子, 和破坏 的过程流程 API。在JShell 工具。
jshell> ProcessHandle currentProcess = ProcessHandle.current(); currentProcess ==> 3960 jshell> System.out.println("Current Process Id: = " + currentProcess.pid()); Current Process Id: = 3960 jshell> currentProcess.info(); $3 ==> [user: Optional[Nhooo\User], cmd: C:\Program Files\Java\jdk-9.0.4\bin\java.exe, startTime: Optional[2020-05-03T06:43:37.510Z], totalTime: Optional[PT1.265625S]] jshell> currentProcess.pid(); $4 ==> 3960 jshell> ProcessHandle.of(3960) $5 ==> Optional[3960] jshell> $5.get() $6 ==> 3960 jshell> $6.info() $7 ==> [user: Optional[Nhooo\User], cmd: C:\Program Files\Java\jdk-9.0.4\bin\java.exe, startTime: Optional[2020-05-03T06:43:37.510Z], totalTime: Optional[PT1.390625S]] jshell> Stream<ProcessHandle> childProc = ProcessHandle.current().children(); childProc ==> java.util.stream.ReferencePipeline$2@6895a785 jshell> childProc.count() $10 ==> 1 jshell> childProc.forEach(procHandle -> { System.out.println(procHandle.destroy() ? "Could not kill process " + procHandle.pid() : "Terminated " + procHandle.pid()); }); | java.lang.IllegalStateException thrown: stream has already been operated upon or closed | at AbstractPipeline.evaluate (AbstractPipeline.java:229) | at ReferencePipeline.forEach (ReferencePipeline.java:430) | at (#11:1)