forked from javaevolved/javaevolved.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess-api.json
More file actions
54 lines (54 loc) · 1.89 KB
/
Copy pathprocess-api.json
File metadata and controls
54 lines (54 loc) · 1.89 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
54
{
"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()\n .exec(\"ls -la\");\nint code = p.waitFor();\n// no way to get PID\n// no easy process info",
"modernCode": "ProcessHandle ph =\n ProcessHandle.current();\nlong pid = ph.pid();\nph.info().command()\n .ifPresent(System.out::println);\nph.children().forEach(\n c -> System.out.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"
}
]
}