< prev index next >

jdk/src/java.base/share/classes/java/util/concurrent/CompletableFuture.java

Print this page




2412             ((r == null) ?
2413              ((count == 0) ?
2414               "[Not completed]" :
2415               "[Not completed, " + count + " dependents]") :
2416              (((r instanceof AltResult) && ((AltResult)r).ex != null) ?
2417               "[Completed exceptionally]" :
2418               "[Completed normally]"));
2419     }
2420 
2421     // jdk9 additions
2422 
2423     /**
2424      * Returns a new incomplete CompletableFuture of the type to be
2425      * returned by a CompletionStage method. Subclasses should
2426      * normally override this method to return an instance of the same
2427      * class as this CompletableFuture. The default implementation
2428      * returns an instance of class CompletableFuture.
2429      *
2430      * @param <U> the type of the value
2431      * @return a new CompletableFuture
2432      * @since 1.9
2433      */
2434     public <U> CompletableFuture<U> newIncompleteFuture() {
2435         return new CompletableFuture<U>();
2436     }
2437 
2438     /**
2439      * Returns the default Executor used for async methods that do not
2440      * specify an Executor. This class uses the {@link
2441      * ForkJoinPool#commonPool()} if it supports more than one
2442      * parallel thread, or else an Executor using one thread per async
2443      * task.  This method may be overridden in subclasses to return
2444      * an Executor that provides at least one independent thread.
2445      *
2446      * @return the executor
2447      * @since 1.9
2448      */
2449     public Executor defaultExecutor() {
2450         return ASYNC_POOL;
2451     }
2452 
2453     /**
2454      * Returns a new CompletableFuture that is completed normally with
2455      * the same value as this CompletableFuture when it completes
2456      * normally. If this CompletableFuture completes exceptionally,
2457      * then the returned CompletableFuture completes exceptionally
2458      * with a CompletionException with this exception as cause. The
2459      * behavior is equivalent to {@code thenApply(x -> x)}. This
2460      * method may be useful as a form of "defensive copying", to
2461      * prevent clients from completing, while still being able to
2462      * arrange dependent actions.
2463      *
2464      * @return the new CompletableFuture
2465      * @since 1.9
2466      */
2467     public CompletableFuture<T> copy() {
2468         return uniCopyStage();
2469     }
2470 
2471     /**
2472      * Returns a new CompletionStage that is completed normally with
2473      * the same value as this CompletableFuture when it completes
2474      * normally, and cannot be independently completed or otherwise
2475      * used in ways not defined by the methods of interface {@link
2476      * CompletionStage}.  If this CompletableFuture completes
2477      * exceptionally, then the returned CompletionStage completes
2478      * exceptionally with a CompletionException with this exception as
2479      * cause.
2480      *
2481      * @return the new CompletionStage
2482      * @since 1.9
2483      */
2484     public CompletionStage<T> minimalCompletionStage() {
2485         return uniAsMinimalStage();
2486     }
2487 
2488     /**
2489      * Completes this CompletableFuture with the result of
2490      * the given Supplier function invoked from an asynchronous
2491      * task using the given executor.
2492      *
2493      * @param supplier a function returning the value to be used
2494      * to complete this CompletableFuture
2495      * @param executor the executor to use for asynchronous execution
2496      * @return this CompletableFuture
2497      * @since 1.9
2498      */
2499     public CompletableFuture<T> completeAsync(Supplier<? extends T> supplier,
2500                                               Executor executor) {
2501         if (supplier == null || executor == null)
2502             throw new NullPointerException();
2503         executor.execute(new AsyncSupply<T>(this, supplier));
2504         return this;
2505     }
2506 
2507     /**
2508      * Completes this CompletableFuture with the result of the given
2509      * Supplier function invoked from an asynchronous task using the
2510      * default executor.
2511      *
2512      * @param supplier a function returning the value to be used
2513      * to complete this CompletableFuture
2514      * @return this CompletableFuture
2515      * @since 1.9
2516      */
2517     public CompletableFuture<T> completeAsync(Supplier<? extends T> supplier) {
2518         return completeAsync(supplier, defaultExecutor());
2519     }
2520 
2521     /**
2522      * Exceptionally completes this CompletableFuture with
2523      * a {@link TimeoutException} if not otherwise completed
2524      * before the given timeout.
2525      *
2526      * @param timeout how long to wait before completing exceptionally
2527      *        with a TimeoutException, in units of {@code unit}
2528      * @param unit a {@code TimeUnit} determining how to interpret the
2529      *        {@code timeout} parameter
2530      * @return this CompletableFuture
2531      * @since 1.9
2532      */
2533     public CompletableFuture<T> orTimeout(long timeout, TimeUnit unit) {
2534         if (unit == null)
2535             throw new NullPointerException();
2536         if (result == null)
2537             whenComplete(new Canceller(Delayer.delay(new Timeout(this),
2538                                                      timeout, unit)));
2539         return this;
2540     }
2541 
2542     /**
2543      * Completes this CompletableFuture with the given value if not
2544      * otherwise completed before the given timeout.
2545      *
2546      * @param value the value to use upon timeout
2547      * @param timeout how long to wait before completing normally
2548      *        with the given value, in units of {@code unit}
2549      * @param unit a {@code TimeUnit} determining how to interpret the
2550      *        {@code timeout} parameter
2551      * @return this CompletableFuture
2552      * @since 1.9
2553      */
2554     public CompletableFuture<T> completeOnTimeout(T value, long timeout,
2555                                                   TimeUnit unit) {
2556         if (unit == null)
2557             throw new NullPointerException();
2558         if (result == null)
2559             whenComplete(new Canceller(Delayer.delay(
2560                                            new DelayedCompleter<T>(this, value),
2561                                            timeout, unit)));
2562         return this;
2563     }
2564 
2565     /**
2566      * Returns a new Executor that submits a task to the given base
2567      * executor after the given delay (or no delay if non-positive).
2568      * Each delay commences upon invocation of the returned executor's
2569      * {@code execute} method.
2570      *
2571      * @param delay how long to delay, in units of {@code unit}
2572      * @param unit a {@code TimeUnit} determining how to interpret the
2573      *        {@code delay} parameter
2574      * @param executor the base executor
2575      * @return the new delayed executor
2576      * @since 1.9
2577      */
2578     public static Executor delayedExecutor(long delay, TimeUnit unit,
2579                                            Executor executor) {
2580         if (unit == null || executor == null)
2581             throw new NullPointerException();
2582         return new DelayedExecutor(delay, unit, executor);
2583     }
2584 
2585     /**
2586      * Returns a new Executor that submits a task to the default
2587      * executor after the given delay (or no delay if non-positive).
2588      * Each delay commences upon invocation of the returned executor's
2589      * {@code execute} method.
2590      *
2591      * @param delay how long to delay, in units of {@code unit}
2592      * @param unit a {@code TimeUnit} determining how to interpret the
2593      *        {@code delay} parameter
2594      * @return the new delayed executor
2595      * @since 1.9
2596      */
2597     public static Executor delayedExecutor(long delay, TimeUnit unit) {
2598         if (unit == null)
2599             throw new NullPointerException();
2600         return new DelayedExecutor(delay, unit, ASYNC_POOL);
2601     }
2602 
2603     /**
2604      * Returns a new CompletionStage that is already completed with
2605      * the given value and supports only those methods in
2606      * interface {@link CompletionStage}.
2607      *
2608      * @param value the value
2609      * @param <U> the type of the value
2610      * @return the completed CompletionStage
2611      * @since 1.9
2612      */
2613     public static <U> CompletionStage<U> completedStage(U value) {
2614         return new MinimalStage<U>((value == null) ? NIL : value);
2615     }
2616 
2617     /**
2618      * Returns a new CompletableFuture that is already completed
2619      * exceptionally with the given exception.
2620      *
2621      * @param ex the exception
2622      * @param <U> the type of the value
2623      * @return the exceptionally completed CompletableFuture
2624      * @since 1.9
2625      */
2626     public static <U> CompletableFuture<U> failedFuture(Throwable ex) {
2627         if (ex == null) throw new NullPointerException();
2628         return new CompletableFuture<U>(new AltResult(ex));
2629     }
2630 
2631     /**
2632      * Returns a new CompletionStage that is already completed
2633      * exceptionally with the given exception and supports only those
2634      * methods in interface {@link CompletionStage}.
2635      *
2636      * @param ex the exception
2637      * @param <U> the type of the value
2638      * @return the exceptionally completed CompletionStage
2639      * @since 1.9
2640      */
2641     public static <U> CompletionStage<U> failedStage(Throwable ex) {
2642         if (ex == null) throw new NullPointerException();
2643         return new MinimalStage<U>(new AltResult(ex));
2644     }
2645 
2646     /**
2647      * Singleton delay scheduler, used only for starting and
2648      * cancelling tasks.
2649      */
2650     static final class Delayer {
2651         static ScheduledFuture<?> delay(Runnable command, long delay,
2652                                         TimeUnit unit) {
2653             return delayer.schedule(command, delay, unit);
2654         }
2655 
2656         static final class DaemonThreadFactory implements ThreadFactory {
2657             public Thread newThread(Runnable r) {
2658                 Thread t = new Thread(r);
2659                 t.setDaemon(true);




2412             ((r == null) ?
2413              ((count == 0) ?
2414               "[Not completed]" :
2415               "[Not completed, " + count + " dependents]") :
2416              (((r instanceof AltResult) && ((AltResult)r).ex != null) ?
2417               "[Completed exceptionally]" :
2418               "[Completed normally]"));
2419     }
2420 
2421     // jdk9 additions
2422 
2423     /**
2424      * Returns a new incomplete CompletableFuture of the type to be
2425      * returned by a CompletionStage method. Subclasses should
2426      * normally override this method to return an instance of the same
2427      * class as this CompletableFuture. The default implementation
2428      * returns an instance of class CompletableFuture.
2429      *
2430      * @param <U> the type of the value
2431      * @return a new CompletableFuture
2432      * @since 9
2433      */
2434     public <U> CompletableFuture<U> newIncompleteFuture() {
2435         return new CompletableFuture<U>();
2436     }
2437 
2438     /**
2439      * Returns the default Executor used for async methods that do not
2440      * specify an Executor. This class uses the {@link
2441      * ForkJoinPool#commonPool()} if it supports more than one
2442      * parallel thread, or else an Executor using one thread per async
2443      * task.  This method may be overridden in subclasses to return
2444      * an Executor that provides at least one independent thread.
2445      *
2446      * @return the executor
2447      * @since 9
2448      */
2449     public Executor defaultExecutor() {
2450         return ASYNC_POOL;
2451     }
2452 
2453     /**
2454      * Returns a new CompletableFuture that is completed normally with
2455      * the same value as this CompletableFuture when it completes
2456      * normally. If this CompletableFuture completes exceptionally,
2457      * then the returned CompletableFuture completes exceptionally
2458      * with a CompletionException with this exception as cause. The
2459      * behavior is equivalent to {@code thenApply(x -> x)}. This
2460      * method may be useful as a form of "defensive copying", to
2461      * prevent clients from completing, while still being able to
2462      * arrange dependent actions.
2463      *
2464      * @return the new CompletableFuture
2465      * @since 9
2466      */
2467     public CompletableFuture<T> copy() {
2468         return uniCopyStage();
2469     }
2470 
2471     /**
2472      * Returns a new CompletionStage that is completed normally with
2473      * the same value as this CompletableFuture when it completes
2474      * normally, and cannot be independently completed or otherwise
2475      * used in ways not defined by the methods of interface {@link
2476      * CompletionStage}.  If this CompletableFuture completes
2477      * exceptionally, then the returned CompletionStage completes
2478      * exceptionally with a CompletionException with this exception as
2479      * cause.
2480      *
2481      * @return the new CompletionStage
2482      * @since 9
2483      */
2484     public CompletionStage<T> minimalCompletionStage() {
2485         return uniAsMinimalStage();
2486     }
2487 
2488     /**
2489      * Completes this CompletableFuture with the result of
2490      * the given Supplier function invoked from an asynchronous
2491      * task using the given executor.
2492      *
2493      * @param supplier a function returning the value to be used
2494      * to complete this CompletableFuture
2495      * @param executor the executor to use for asynchronous execution
2496      * @return this CompletableFuture
2497      * @since 9
2498      */
2499     public CompletableFuture<T> completeAsync(Supplier<? extends T> supplier,
2500                                               Executor executor) {
2501         if (supplier == null || executor == null)
2502             throw new NullPointerException();
2503         executor.execute(new AsyncSupply<T>(this, supplier));
2504         return this;
2505     }
2506 
2507     /**
2508      * Completes this CompletableFuture with the result of the given
2509      * Supplier function invoked from an asynchronous task using the
2510      * default executor.
2511      *
2512      * @param supplier a function returning the value to be used
2513      * to complete this CompletableFuture
2514      * @return this CompletableFuture
2515      * @since 9
2516      */
2517     public CompletableFuture<T> completeAsync(Supplier<? extends T> supplier) {
2518         return completeAsync(supplier, defaultExecutor());
2519     }
2520 
2521     /**
2522      * Exceptionally completes this CompletableFuture with
2523      * a {@link TimeoutException} if not otherwise completed
2524      * before the given timeout.
2525      *
2526      * @param timeout how long to wait before completing exceptionally
2527      *        with a TimeoutException, in units of {@code unit}
2528      * @param unit a {@code TimeUnit} determining how to interpret the
2529      *        {@code timeout} parameter
2530      * @return this CompletableFuture
2531      * @since 9
2532      */
2533     public CompletableFuture<T> orTimeout(long timeout, TimeUnit unit) {
2534         if (unit == null)
2535             throw new NullPointerException();
2536         if (result == null)
2537             whenComplete(new Canceller(Delayer.delay(new Timeout(this),
2538                                                      timeout, unit)));
2539         return this;
2540     }
2541 
2542     /**
2543      * Completes this CompletableFuture with the given value if not
2544      * otherwise completed before the given timeout.
2545      *
2546      * @param value the value to use upon timeout
2547      * @param timeout how long to wait before completing normally
2548      *        with the given value, in units of {@code unit}
2549      * @param unit a {@code TimeUnit} determining how to interpret the
2550      *        {@code timeout} parameter
2551      * @return this CompletableFuture
2552      * @since 9
2553      */
2554     public CompletableFuture<T> completeOnTimeout(T value, long timeout,
2555                                                   TimeUnit unit) {
2556         if (unit == null)
2557             throw new NullPointerException();
2558         if (result == null)
2559             whenComplete(new Canceller(Delayer.delay(
2560                                            new DelayedCompleter<T>(this, value),
2561                                            timeout, unit)));
2562         return this;
2563     }
2564 
2565     /**
2566      * Returns a new Executor that submits a task to the given base
2567      * executor after the given delay (or no delay if non-positive).
2568      * Each delay commences upon invocation of the returned executor's
2569      * {@code execute} method.
2570      *
2571      * @param delay how long to delay, in units of {@code unit}
2572      * @param unit a {@code TimeUnit} determining how to interpret the
2573      *        {@code delay} parameter
2574      * @param executor the base executor
2575      * @return the new delayed executor
2576      * @since 9
2577      */
2578     public static Executor delayedExecutor(long delay, TimeUnit unit,
2579                                            Executor executor) {
2580         if (unit == null || executor == null)
2581             throw new NullPointerException();
2582         return new DelayedExecutor(delay, unit, executor);
2583     }
2584 
2585     /**
2586      * Returns a new Executor that submits a task to the default
2587      * executor after the given delay (or no delay if non-positive).
2588      * Each delay commences upon invocation of the returned executor's
2589      * {@code execute} method.
2590      *
2591      * @param delay how long to delay, in units of {@code unit}
2592      * @param unit a {@code TimeUnit} determining how to interpret the
2593      *        {@code delay} parameter
2594      * @return the new delayed executor
2595      * @since 9
2596      */
2597     public static Executor delayedExecutor(long delay, TimeUnit unit) {
2598         if (unit == null)
2599             throw new NullPointerException();
2600         return new DelayedExecutor(delay, unit, ASYNC_POOL);
2601     }
2602 
2603     /**
2604      * Returns a new CompletionStage that is already completed with
2605      * the given value and supports only those methods in
2606      * interface {@link CompletionStage}.
2607      *
2608      * @param value the value
2609      * @param <U> the type of the value
2610      * @return the completed CompletionStage
2611      * @since 9
2612      */
2613     public static <U> CompletionStage<U> completedStage(U value) {
2614         return new MinimalStage<U>((value == null) ? NIL : value);
2615     }
2616 
2617     /**
2618      * Returns a new CompletableFuture that is already completed
2619      * exceptionally with the given exception.
2620      *
2621      * @param ex the exception
2622      * @param <U> the type of the value
2623      * @return the exceptionally completed CompletableFuture
2624      * @since 9
2625      */
2626     public static <U> CompletableFuture<U> failedFuture(Throwable ex) {
2627         if (ex == null) throw new NullPointerException();
2628         return new CompletableFuture<U>(new AltResult(ex));
2629     }
2630 
2631     /**
2632      * Returns a new CompletionStage that is already completed
2633      * exceptionally with the given exception and supports only those
2634      * methods in interface {@link CompletionStage}.
2635      *
2636      * @param ex the exception
2637      * @param <U> the type of the value
2638      * @return the exceptionally completed CompletionStage
2639      * @since 9
2640      */
2641     public static <U> CompletionStage<U> failedStage(Throwable ex) {
2642         if (ex == null) throw new NullPointerException();
2643         return new MinimalStage<U>(new AltResult(ex));
2644     }
2645 
2646     /**
2647      * Singleton delay scheduler, used only for starting and
2648      * cancelling tasks.
2649      */
2650     static final class Delayer {
2651         static ScheduledFuture<?> delay(Runnable command, long delay,
2652                                         TimeUnit unit) {
2653             return delayer.schedule(command, delay, unit);
2654         }
2655 
2656         static final class DaemonThreadFactory implements ThreadFactory {
2657             public Thread newThread(Runnable r) {
2658                 Thread t = new Thread(r);
2659                 t.setDaemon(true);


< prev index next >