< prev index next >

src/java.base/share/classes/java/lang/Process.java

Print this page




 351      * The {@link java.util.concurrent.CompletableFuture} provides the ability
 352      * to trigger dependent functions or actions that may be run synchronously
 353      * or asynchronously upon process termination.
 354      * When the process terminates the CompletableFuture is
 355      * {@link java.util.concurrent.CompletableFuture#complete completed} regardless
 356      * of the exit status of the process.
 357      * <p>
 358      * Calling {@code onExit().get()} waits for the process to terminate and returns
 359      * the Process. The future can be used to check if the process is
 360      * {@link java.util.concurrent.CompletableFuture#isDone done} or to
 361      * {@link java.util.concurrent.CompletableFuture#get() wait} for it to terminate.
 362      * {@link java.util.concurrent.CompletableFuture#cancel(boolean) Cancelling}
 363      * the CompletableFuture does not affect the Process.
 364      * <p>
 365      * If the process is {@link #isAlive not alive} the {@link CompletableFuture}
 366      * returned has been {@link java.util.concurrent.CompletableFuture#complete completed}.
 367      * <p>
 368      * Processes returned from {@link ProcessBuilder#start} override the
 369      * default implementation to provide an efficient mechanism to wait
 370      * for process exit.
 371      * <p>
 372      * @apiNote
 373      * Using {@link #onExit() onExit} is an alternative to
 374      * {@link #waitFor() waitFor} that enables both additional concurrency
 375      * and convenient access to the result of the Process.
 376      * Lambda expressions can be used to evaluate the result of the Process
 377      * execution.
 378      * If there is other processing to be done before the value is used
 379      * then {@linkplain #onExit onExit} is a convenient mechanism to
 380      * free the current thread and block only if and when the value is needed.
 381      * <br>
 382      * For example, launching a process to compare two files and get a boolean if they are identical:
 383      * <pre> {@code   Process p = new ProcessBuilder("cmp", "f1", "f2").start();
 384      *    Future<Boolean> identical = p.onExit().thenApply(p1 -> p1.exitValue() == 0);
 385      *    ...
 386      *    if (identical.get()) { ... }
 387      * }</pre>
 388      *
 389      * @implSpec
 390      * This implementation executes {@link #waitFor()} in a separate thread
 391      * repeatedly until it returns successfully. If the execution of




 351      * The {@link java.util.concurrent.CompletableFuture} provides the ability
 352      * to trigger dependent functions or actions that may be run synchronously
 353      * or asynchronously upon process termination.
 354      * When the process terminates the CompletableFuture is
 355      * {@link java.util.concurrent.CompletableFuture#complete completed} regardless
 356      * of the exit status of the process.
 357      * <p>
 358      * Calling {@code onExit().get()} waits for the process to terminate and returns
 359      * the Process. The future can be used to check if the process is
 360      * {@link java.util.concurrent.CompletableFuture#isDone done} or to
 361      * {@link java.util.concurrent.CompletableFuture#get() wait} for it to terminate.
 362      * {@link java.util.concurrent.CompletableFuture#cancel(boolean) Cancelling}
 363      * the CompletableFuture does not affect the Process.
 364      * <p>
 365      * If the process is {@link #isAlive not alive} the {@link CompletableFuture}
 366      * returned has been {@link java.util.concurrent.CompletableFuture#complete completed}.
 367      * <p>
 368      * Processes returned from {@link ProcessBuilder#start} override the
 369      * default implementation to provide an efficient mechanism to wait
 370      * for process exit.
 371      *
 372      * @apiNote
 373      * Using {@link #onExit() onExit} is an alternative to
 374      * {@link #waitFor() waitFor} that enables both additional concurrency
 375      * and convenient access to the result of the Process.
 376      * Lambda expressions can be used to evaluate the result of the Process
 377      * execution.
 378      * If there is other processing to be done before the value is used
 379      * then {@linkplain #onExit onExit} is a convenient mechanism to
 380      * free the current thread and block only if and when the value is needed.
 381      * <br>
 382      * For example, launching a process to compare two files and get a boolean if they are identical:
 383      * <pre> {@code   Process p = new ProcessBuilder("cmp", "f1", "f2").start();
 384      *    Future<Boolean> identical = p.onExit().thenApply(p1 -> p1.exitValue() == 0);
 385      *    ...
 386      *    if (identical.get()) { ... }
 387      * }</pre>
 388      *
 389      * @implSpec
 390      * This implementation executes {@link #waitFor()} in a separate thread
 391      * repeatedly until it returns successfully. If the execution of


< prev index next >