2 ms·
That's also the old way, as the author says: "Now let's see a slightly more complex example, where one is getting a list of all processes running on the operat
by niftich 10y ago
That's also the old way, as the author says:
"Now let's see a slightly more complex example, where one is getting a list of all processes running on the operating system.
This is an old implementation with pure Java: [code]"
Meanwhile, further down, the new implementation is:
private static void listProcesses(){
ProcessHandle.allProcesses().forEach((h) -> printHandle(h));
}
private static void printHandle(ProcessHandle procHandle) {
// get info from handle
ProcessHandle.Info procInfo = procHandle.info();
System.out.println("PID: " + procHandle.getPid());
System.out.print("Start Parameters: ");
String[] empty = new String[]{"-"};
for (String arg : procInfo.arguments().orElse(empty)) {
System.out.print(arg + " ");
}
System.out.println();
System.out.println("Path: " + procInfo.command().orElse("-"));
System.out.println("Start: " + procInfo.startInstant().
orElse(Instant.now()).toString());
System.out.println("Runtime: " + procInfo.totalCpuDuration().
orElse(Duration.ofMillis(0)).toMillis() + "ms");
System.out.println("User: " + procInfo.user());
}
I don't disagree that the traditional Java way of chaining InputStreams to BufferedReaders and whatnot makes for verbose code, but there are in fact many libraries like Guava and Apache Commons that'll give you one-liners for these. Much of the Java stdlib's philosophy was inspired by the single responsibility principle, rather than providing sane defaults for chains of common operations.