-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathprocess-api.yaml
More file actions
53 lines (53 loc) · 1.62 KB
/
process-api.yaml
File metadata and controls
53 lines (53 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
---
id: 53
slug: "process-api"
title: "Modern Process API"
category: "concurrency"
difficulty: "intermediate"
jdkVersion: "9"
oldLabel: "Java 8"
modernLabel: "Java 9+"
oldApproach: "Runtime.exec()"
modernApproach: "ProcessHandle"
oldCode: |-
Process p = Runtime.getRuntime()
.exec("ls -la");
int code = p.waitFor();
// no way to get PID
// no easy process info
modernCode: |-
ProcessHandle ph =
ProcessHandle.current();
long pid = ph.pid();
ph.info().command()
.ifPresent(IO::println);
ph.children().forEach(
c -> IO.println(c.pid()));
summary: "Inspect and manage OS processes with ProcessHandle."
explanation: "ProcessHandle provides PIDs, process info (command, arguments, start\
\ time, CPU usage), parent/child relationships, and process destruction. No more\
\ undocumented Process internals."
whyModernWins:
- icon: "🔍"
title: "Full info"
desc: "Access PID, command, arguments, start time, CPU usage."
- icon: "🌳"
title: "Process tree"
desc: "Navigate parent, children, and descendants."
- icon: "📊"
title: "Monitoring"
desc: "onExit() returns a CompletableFuture for async monitoring."
support:
state: "available"
description: "Widely available since JDK 9 (Sept 2017)"
prev: "concurrency/thread-sleep-duration"
next: "concurrency/concurrent-http-virtual"
related:
- "concurrency/scoped-values"
- "concurrency/concurrent-http-virtual"
- "concurrency/completablefuture-chaining"
docs:
- title: "ProcessHandle"
href: "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/ProcessHandle.html"
- title: "Process API (JEP 102)"
href: "https://openjdk.org/jeps/102"