Skip to content

Commit b90c859

Browse files
author
Nicolai Parlog
committed
Demonstrate process handle and info
1 parent 3dbc73f commit b90c859

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Check out the [jpms](http://blog.codefx.org/tag/jpms/) tag on my blog, [this dem
118118
*[`Collection`](src/main/java/org/codefx/demo/java11/api/collection/ToArray.java)
119119
* ⑪⑨ `Optional` [in Java 11](src/main/java/org/codefx/demo/java11/api/optional/IsEmpty.java)
120120
and [in Java 9](src/main/java/org/codefx/demo/java9/api/optional/Or.java) ([article](http://blog.codefx.org/java/dev/java-9-optional/))
121-
*[OS processes](src/main/java/org/codefx/demo/java9/api/processes/PipeProcessesAndAwaitCompletion.java) ([JEP 102](http://openjdk.java.net/jeps/102))
121+
*[OS processes](src/main/java/org/codefx/demo/java9/api/processes) ([JEP 102](http://openjdk.java.net/jeps/102))
122122

123123
Some of the small changes have their own articles (in which case they are linked), but many don't.
124124
Most are show-cased in these posts, though:
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.codefx.demo.java9.api.processes;
2+
3+
import java.lang.ProcessHandle.Info;
4+
import java.text.NumberFormat;
5+
import java.time.Duration;
6+
import java.time.LocalDateTime;
7+
import java.time.format.DateTimeFormatter;
8+
import java.util.TimeZone;
9+
10+
import static java.lang.String.format;
11+
import static java.util.stream.Collectors.joining;
12+
13+
public class ProcessHandleAndInfo {
14+
15+
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
16+
17+
public static void main(String[] args) {
18+
System.out.println(" PID | START | CPU TIME | USER NAME | CHILDREN | COMMAND");
19+
ProcessHandle.allProcesses()
20+
.filter(process -> process.info().command().isPresent())
21+
.map(ProcessHandleAndInfo::prettyPrint)
22+
.forEach(System.out::println);
23+
}
24+
25+
private static String prettyPrint(ProcessHandle process) {
26+
Info info = process.info();
27+
String start = info
28+
.startInstant()
29+
.map(instant -> LocalDateTime.ofInstant(instant, TimeZone.getDefault().toZoneId()))
30+
.map(DATE_TIME_FORMATTER::format)
31+
.orElse("n.a.");
32+
String cpuDuration = info
33+
.totalCpuDuration()
34+
.map(ProcessHandleAndInfo::prettyPrint)
35+
.orElse("n.a.");
36+
String user = info
37+
.user()
38+
.map(name -> name.length() <= 9
39+
? name
40+
: name.substring(0, 8) + "…")
41+
.orElse("n.a.");
42+
String command = info.command().orElse("n.a.");
43+
String children = process
44+
.children()
45+
.map(ProcessHandle::pid)
46+
.map(Object::toString)
47+
.collect(joining(", "));
48+
children = children.length() <= 10
49+
? children
50+
: children.substring(0, 9) + "…";
51+
52+
return format("%6d | %8s | %12s | %9s | %10s | %s", process.pid(), start, cpuDuration, user, children, command);
53+
}
54+
55+
private static String prettyPrint(Duration duration) {
56+
return NumberFormat.getNumberInstance().format(duration.toMillis()) + " ms";
57+
}
58+
59+
}

0 commit comments

Comments
 (0)