Mac*_*ski 6 java java-9 processhandle
我目前正在测试java 9中的Process API,我对以下代码有一些问题:
Process process = new ProcessBuilder( List.of("ping", "-i", "1", "-c", "4", "google.com")).start();
CompletableFuture<Void> startTimeFuture = process.toHandle().onExit()
.thenApply(ProcessHandle::info)
.thenApply(ProcessHandle.Info::startInstant)
.thenAccept(System.out::println);
startTimeFuture.get();
Run Code Online (Sandbox Code Playgroud)
当我执行这个片段时,我在终端中获得Optional.empty.Javadoc声明该info方法返回任何数据(如果可用),因此我怀疑JVM无法获取有关生成进程的信息.但是当我试图从ProcessHandle未来获得pid时,我得到了适当的价值.
总结一下,我的问题是:
ProcessHandle.Info打电话后有没有办法让非空onExit()?
我正在使用Ubuntu 16.04 LTS
编辑 - 执行时这是终端的输出ping -i 1 -c 5 google.com
PING google.com(xxx.xxx.16.46)56(84)字节的数据.
来自waw02s14-in-f14.1e100.net(xxx.xxx.16.46)的64个字节:icmp_seq = 1 ttl = 52 time = 6.71 ms
来自waw02s14-in-f14.1e100.net(xxx.xxx.16.46)的64个字节:icmp_seq = 2 ttl = 52 time = 6.26 ms
来自waw02s14-in-f14.1e100.net(xxx.xxx.16.46)的64个字节:icmp_seq = 3 ttl = 52 time = 16.6 ms
来自waw02s14-in-f14.1e100.net(xxx.xxx.16.46)的64字节:icmp_seq = 4 ttl = 52 time = 10.6 ms
来自waw02s14-in-f14.1e100.net(xxx.xxx.16.46)的64个字节:icmp_seq = 5 ttl = 52 time = 13.4 ms
--- google.com ping统计---
发送5个数据包,5个接收,0%丢包,时间4007ms rtt min/avg/max/mdev = 6.267/10.746/16.667/3.968 ms
更新的用例: -我想检查是否可以,给出命令执行的时间,例如通过调用ProcessHandle.Info::totalCpuDuration
我想我找到了这种行为的原因(至少在 Linux 发行版上)。
ProcessHandle.Info对象是通过以下方法创建的:
public static ProcessHandle.Info info(long pid, long startTime) {
Info info = new Info();
info.info0(pid);
if (startTime != info.startTime) {
info.command = null;
info.arguments = null;
info.startTime = -1L;
info.totalTime = -1L;
info.user = null;
}
return info;
}
Run Code Online (Sandbox Code Playgroud)
哪里info.info0(pid)是对本机方法的调用。所以我下载了openjdk源代码并检查了这个方法的实现。/proc/{pid}/stat在Linux上,JVM通过读取,来检索进程数据/proc/{pid}/cmdline,/proc/{pid}/exe这些数据在进程终止后不再可用。
回答我的问题:
没有办法获得ProcessHandle.Info完成的过程。
| 归档时间: |
|
| 查看次数: |
218 次 |
| 最近记录: |