Tag Archives: wait

Java – Wait for Multiple Processes

In Java, the function Process.onExit() creates a CompletableFuture as exit handler and this is the enabling feature in Java to wait for any termination of a set of processes.

public Process anyProcTermination(List<Process> procs)  {
CompletableFuture<Process>[] exitHandlers =
procs.stream()
.map(p -> p.onExit())
.toArray(CompletableFuture[]::new);
CompletableFuture<Object> exitHandle = CompletableFuture.anyOf(exitHandlers);
return (Process) exitHandle.join();
}