GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing javaevolved.dev — Modern Process API | java.evolved
Concurrency
Intermediate
Modern Process APIInspect and manage OS processes with ProcessHandle. Code Comparison
✕ Java 8
Process p = Runtime.getRuntime()
.exec("ls -la");
int code = p.waitFor();
// no way to get PID
// no easy process info
✓ Java 9+
ProcessHandle ph =
ProcessHandle.current();
long pid = ph.pid();
ph.info().command()
.ifPresent(IO::println);
ph.children().forEach(
c -> IO.println(c.pid()));
See a problem with this code? Let us know.
Why the modern way wins
🔍
Full infoAccess PID, command, arguments, start time, CPU usage. 🌳
Process treeNavigate parent, children, and descendants. 📊
MonitoringonExit() returns a CompletableFuture for async monitoring. Old Approach
Runtime.exec()
Modern Approach
ProcessHandle
Since JDK
9
Difficulty
Intermediate
JDK Support
Modern Process API
Available
Widely available since JDK 9 (Sept 2017) How it works
ProcessHandle provides PIDs, process info (command, arguments, start time, CPU usage), parent/child relationships, and process destruction. No more undocumented Process internals. Related Documentation
Proof
Related patterns
Concurrency
Advanced
Scoped valuesJava 8
static final ThreadLocal<User> CURRENT =
new ThreadLocal<>();
void handle(Request req) {
CURRENT.set(authenticate(req));
try { process(); }
finally { CURRENT.remove(); }
}
Java 25
static final ScopedValue<User> CURRENT =
ScopedValue.newInstance();
void handle(Request req) {
ScopedValue.where(CURRENT,
authenticate(req)
).run(this::process);
}
JDK 25+
→
Concurrency
Intermediate
Concurrent HTTP with virtual threadsJava 8
ExecutorService pool =
Executors.newFixedThreadPool(10);
List<Future<String>> futures =
urls.stream()
.map(u -> pool.submit(
() -> fetchUrl(u)))
.toList();
// manual shutdown, blocking get()
Java 21+
try (var exec = Executors
.newVirtualThreadPerTaskExecutor()) {
var results = urls.stream()
.map(u -> exec.submit(
() -> client.send(req(u),
ofString()).body()))
.toList().stream()
.map(Future::join).toList();
}
JDK 21+
→
Concurrency
Intermediate
CompletableFuture chainingPre-Java 8
Future<String> future =
executor.submit(this::fetchData);
String data = future.get(); // blocks
String result = transform(data);
Java 8+
CompletableFuture.supplyAsync(
this::fetchData
)
.thenApply(this::transform)
.thenAccept(IO::println);
JDK 8+
→
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |