Java9 进程API详细介绍(2)

以下代码开启了一个进程,并将此进程的一些信息输出:

import java.io.IOException; import java.time.Duration; import java.time.Instant; public class ProcessDemo { public static void main(String[] args) throws InterruptedException, IOException { dumpProcessInfo(ProcessHandle.current()); Process p = new ProcessBuilder("notepad.exe", "C:\\temp\\names.txt").start(); dumpProcessInfo(p.toHandle()); p.waitFor(); dumpProcessInfo(p.toHandle()); } static void dumpProcessInfo(ProcessHandle ph) { System.out.println("PROCESS INFORMATION"); System.out.println("==================="); System.out.printf("Process id: %d%n", ph.getPid()); ProcessHandle.Info info = ph.info(); System.out.printf("Command: %s%n", info.command().orElse("")); String[] args = info.arguments().orElse(new String[]{}); System.out.println("Arguments:"); for (String arg: args) System.out.printf(" %s%n", arg); System.out.printf("Command line: %s%n", info.commandLine().orElse("")); System.out.printf("Start time: %s%n", info.startInstant().orElse(Instant.now()).toString()); System.out.printf("Run time duration: %sms%n", info.totalCpuDuration() .orElse(Duration.ofMillis(0)).toMillis()); System.out.printf("Owner: %s%n", info.user().orElse("")); System.out.println(); } }

在main() 方法里面首先调用 ProcessHandle.current() 来获取当前进程的句柄,然后用dumpProcessInfo() 方法来输出dump进程的详细信息。接下来启动notepad.exe ,并且dump其进程信息。等到notepad.exe 终止之后,再一次dump了它的信息。

dumpProcessInfo() 方法首先输出头标识信息,随后输出PID,随后获取ProcessHandle.Info 引用。接下来,调用command()和其他Info方法,输出它们的值。 如果方法返回null(因为信息不可用),则通过Optional的orElse()方法来返回信息。

以下是输出内容,期间可以看到notepad窗口:

PROCESS INFORMATION =================== Process id: 1140 Command: C:\PROGRA~1\Java\jdk-9\bin\java.exe Arguments: Command line: Start time: 2017-03-02T22:24:40.998Z Run time duration: 890ms Owner: jeff\jeffrey PROCESS INFORMATION =================== Process id: 5516 Command: C:\Windows\System32\notepad.exe Arguments: Command line: Start time: 2017-03-02T22:24:41.763Z Run time duration: 0ms Owner: jeff\jeffrey PROCESS INFORMATION =================== Process id: 5516 Command: Arguments: Command line: Start time: 2017-03-02T22:24:41.763Z Run time duration: 234ms Owner: jeff\jeffrey

第三部分的PROCESS INFORMATION迟迟没有出现,直到notepad界面消失才出现。Info的arguments() 方法没有向C:\temp\names.txt 文件里打印命令行,可能是因为此时信息是不可用的,抑或是因为出现了bug。在进程结束之后,command()方法返回null。最后,当command()方法或者arguments() 方法其中之一返回了null,commandLine()方法也将返回null。

4、获取所有进程的信息

ProcessHandle 中的allProcesses() 方法以Java8中Stream API的方式返回当前系统中所有可见的进程句柄。下面的代码展示了如何使用Stream来获取进程句柄, 取前四个进程,dump出它们的信息。

import java.io.IOException; import java.time.Duration; import java.time.Instant; public class ProcessDemo { public static void main(String[] args) { ProcessHandle.allProcesses() .filter(ph -> ph.info().command().isPresent()) .limit(4) .forEach((process) -> dumpProcessInfo(process)); } static void dumpProcessInfo(ProcessHandle ph) { System.out.println("PROCESS INFORMATION"); System.out.println("==================="); System.out.printf("Process id: %d%n", ph.getPid()); ProcessHandle.Info info = ph.info(); System.out.printf("Command: %s%n", info.command().orElse("")); String[] args = info.arguments().orElse(new String[]{}); System.out.println("Arguments:"); for (String arg: args) System.out.printf(" %s%n", arg); System.out.printf("Command line: %s%n", info.commandLine().orElse("")); System.out.printf("Start time: %s%n", info.startInstant().orElse(Instant.now()).toString()); System.out.printf("Run time duration: %sms%n", info.totalCpuDuration() .orElse(Duration.ofMillis(0)).toMillis()); System.out.printf("Owner: %s%n", info.user().orElse("")); System.out.println(); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/47d750fb3af6f60f18eb68311710a064.html