ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/CompletableFuture.java
(Generate patch)

Comparing jsr166/src/jsr166e/CompletableFuture.java (file contents):
Revision 1.14 by jsr166, Sat Feb 16 20:50:29 2013 UTC vs.
Revision 1.19 by jsr166, Sun Jul 14 19:55:05 2013 UTC

# Line 6 | Line 6
6  
7   package jsr166e;
8   import java.util.concurrent.Future;
9 + import java.util.concurrent.FutureTask;
10   import java.util.concurrent.TimeUnit;
11   import java.util.concurrent.Executor;
11 import java.util.concurrent.ThreadLocalRandom;
12   import java.util.concurrent.ExecutionException;
13   import java.util.concurrent.TimeoutException;
14   import java.util.concurrent.CancellationException;
# Line 18 | Line 18 | import java.util.concurrent.locks.LockSu
18   /**
19   * A {@link Future} that may be explicitly completed (setting its
20   * value and status), and may include dependent functions and actions
21 < * that trigger upon its completion.  Methods are available for adding
22 < * those based on Functions, Blocks, and Runnables, depending on
23 < * whether they require arguments and/or produce results, as well as
24 < * those triggered after either or both the current and another
25 < * CompletableFuture complete.  Functions and actions supplied for
26 < * dependent completions (mainly using methods with prefix {@code
27 < * then}) may be performed by the thread that completes the current
21 > * that trigger upon its completion.
22 > *
23 > * <p>When two or more threads attempt to
24 > * {@link #complete complete},
25 > * {@link #completeExceptionally completeExceptionally}, or
26 > * {@link #cancel cancel}
27 > * a CompletableFuture, only one of them succeeds.
28 > *
29 > * <p>Methods are available for adding dependents based on
30 > * user-provided Functions, Actions, or Runnables. The appropriate
31 > * form to use depends on whether actions require arguments and/or
32 > * produce results.  Completion of a dependent action will trigger the
33 > * completion of another CompletableFuture.  Actions may also be
34 > * triggered after either or both the current and another
35 > * CompletableFuture complete.  Multiple CompletableFutures may also
36 > * be grouped as one using {@link #anyOf(CompletableFuture...)} and
37 > * {@link #allOf(CompletableFuture...)}.
38 > *
39 > * <p>CompletableFutures themselves do not execute asynchronously.
40 > * However, actions supplied for dependent completions of another
41 > * CompletableFuture may do so, depending on whether they are provided
42 > * via one of the <em>async</em> methods (that is, methods with names
43 > * of the form <tt><var>xxx</var>Async</tt>).  The <em>async</em>
44 > * methods provide a way to commence asynchronous processing of an
45 > * action using either a given {@link Executor} or by default the
46 > * {@link ForkJoinPool#commonPool()}. To simplify monitoring,
47 > * debugging, and tracking, all generated asynchronous tasks are
48 > * instances of the marker interface {@link AsynchronousCompletionTask}.
49 > *
50 > * <p>Actions supplied for dependent completions of <em>non-async</em>
51 > * methods may be performed by the thread that completes the current
52   * CompletableFuture, or by any other caller of these methods.  There
53   * are no guarantees about the order of processing completions unless
54   * constrained by these methods.
55   *
56 < * <p>When two or more threads attempt to {@link #complete} or {@link
57 < * #completeExceptionally} a CompletableFuture, only one of them
58 < * succeeds.
56 > * <p>Since (unlike {@link FutureTask}) this class has no direct
57 > * control over the computation that causes it to be completed,
58 > * cancellation is treated as just another form of exceptional completion.
59 > * Method {@link #cancel cancel} has the same effect as
60 > * {@code completeExceptionally(new CancellationException())}.
61   *
62 < * <p>Upon exceptional completion, or when a completion entails
63 < * computation of a function or action, and it terminates abruptly
64 < * with an (unchecked) exception or error, then further completions
65 < * act as {@code completeExceptionally} with a {@link
66 < * CompletionException} holding that exception as its cause.  If a
67 < * CompletableFuture completes exceptionally, and is not followed by a
68 < * {@link #exceptionally} or {@link #handle} completion, then all of
69 < * its dependents (and their dependents) also complete exceptionally
70 < * with CompletionExceptions holding the ultimate cause.  In case of a
45 < * CompletionException, methods {@link #get()} and {@link #get(long,
46 < * TimeUnit)} throw an {@link ExecutionException} with the same cause
47 < * as would be held in the corresponding CompletionException. However,
48 < * in these cases, methods {@link #join()} and {@link #getNow} throw
49 < * the CompletionException, which simplifies usage especially within
50 < * other completion functions.
62 > * <p>Upon exceptional completion (including cancellation), or when a
63 > * completion entails an additional computation which terminates
64 > * abruptly with an (unchecked) exception or error, then all of their
65 > * dependent completions (and their dependents in turn) generally act
66 > * as {@code completeExceptionally} with a {@link CompletionException}
67 > * holding that exception as its cause.  However, the {@link
68 > * #exceptionally exceptionally} and {@link #handle handle}
69 > * completions <em>are</em> able to handle exceptional completions of
70 > * the CompletableFutures they depend on.
71   *
72 < * <p>CompletableFutures themselves do not execute asynchronously.
73 < * However, the {@code async} methods provide commonly useful ways to
74 < * commence asynchronous processing, using either a given {@link
75 < * Executor} or by default the {@link ForkJoinPool#commonPool()}, of a
76 < * function or action that will result in the completion of a new
77 < * CompletableFuture. To simplify monitoring, debugging, and tracking,
58 < * all generated asynchronous tasks are instances of the tagging
59 < * interface {@link AsynchronousCompletionTask}.
72 > * <p>In case of exceptional completion with a CompletionException,
73 > * methods {@link #get()} and {@link #get(long, TimeUnit)} throw an
74 > * {@link ExecutionException} with the same cause as held in the
75 > * corresponding CompletionException.  However, in these cases,
76 > * methods {@link #join()} and {@link #getNow} throw the
77 > * CompletionException, which simplifies usage.
78   *
79 < * <p><em>jsr166e note: During transition, this class
80 < * uses nested functional interfaces with different names but the
81 < * same forms as those expected for JDK8.</em>
79 > * <p>Arguments used to pass a completion result (that is, for parameters
80 > * of type {@code T}) may be null, but passing a null value for any other
81 > * parameter will result in a {@link NullPointerException} being thrown.
82   *
83   * @author Doug Lea
66 * @since 1.8
84   */
85   public class CompletableFuture<T> implements Future<T> {
86      // jsr166e nested interfaces
# Line 110 | Line 127 | public class CompletableFuture<T> implem
127       * extends AtomicInteger so callers can claim the action via
128       * compareAndSet(0, 1).  The Completion.run methods are all
129       * written a boringly similar uniform way (that sometimes includes
130 <     * unnecessary-looking checks, kept to maintain uniformity). There
131 <     * are enough dimensions upon which they differ that factoring to
132 <     * use common code isn't worthwhile.
130 >     * unnecessary-looking checks, kept to maintain uniformity).
131 >     * There are enough dimensions upon which they differ that
132 >     * attempts to factor commonalities while maintaining efficiency
133 >     * require more lines of code than they would save.
134       *
135       * 4. The exported then/and/or methods do support a bit of
136       * factoring (see doThenApply etc). They must cope with the
# Line 169 | Line 187 | public class CompletableFuture<T> implem
187       * CompletionException unless it is one already.  Otherwise uses
188       * the given result, boxed as NIL if null.
189       */
190 <    final void internalComplete(Object v, Throwable ex) {
190 >    final void internalComplete(T v, Throwable ex) {
191          if (result == null)
192              UNSAFE.compareAndSwapObject
193                  (this, RESULT, null,
# Line 189 | Line 207 | public class CompletableFuture<T> implem
207  
208      /* ------------- waiting for completions -------------- */
209  
210 +    /** Number of processors, for spin control */
211 +    static final int NCPU = Runtime.getRuntime().availableProcessors();
212 +
213      /**
214       * Heuristic spin value for waitingGet() before blocking on
215       * multiprocessors
216       */
217 <    static final int WAITING_GET_SPINS = 256;
217 >    static final int SPINS = (NCPU > 1) ? 1 << 8 : 0;
218  
219      /**
220       * Linked nodes to record waiting threads in a Treiber stack.  See
# Line 248 | Line 269 | public class CompletableFuture<T> implem
269      private Object waitingGet(boolean interruptible) {
270          WaitNode q = null;
271          boolean queued = false;
272 <        int h = 0, spins = 0;
272 >        int spins = SPINS;
273          for (Object r;;) {
274              if ((r = result) != null) {
275                  if (q != null) { // suppress unpark
# Line 264 | Line 285 | public class CompletableFuture<T> implem
285                  postComplete(); // help release others
286                  return r;
287              }
267            else if (h == 0) {
268                h = ThreadLocalRandom.current().nextInt();
269                if (Runtime.getRuntime().availableProcessors() > 1)
270                    spins = WAITING_GET_SPINS;
271            }
288              else if (spins > 0) {
289 <                h ^= h << 1;  // xorshift
290 <                h ^= h >>> 3;
275 <                if ((h ^= h << 10) >= 0)
289 >                int rnd = ThreadLocalRandom.current().nextInt();
290 >                if (rnd >= 0)
291                      --spins;
292              }
293              else if (q == null)
# Line 380 | Line 395 | public class CompletableFuture<T> implem
395      /* ------------- Async tasks -------------- */
396  
397      /**
398 <     * A tagging interface identifying asynchronous tasks produced by
398 >     * A marker interface identifying asynchronous tasks produced by
399       * {@code async} methods. This may be useful for monitoring,
400       * debugging, and tracking asynchronous activities.
401 +     *
402 +     * @since 1.8
403       */
404      public static interface AsynchronousCompletionTask {
405      }
# Line 441 | Line 458 | public class CompletableFuture<T> implem
458      }
459  
460      static final class AsyncApply<T,U> extends Async {
444        final Fun<? super T,? extends U> fn;
461          final T arg;
462 +        final Fun<? super T,? extends U> fn;
463          final CompletableFuture<U> dst;
464          AsyncApply(T arg, Fun<? super T,? extends U> fn,
465 <                      CompletableFuture<U> dst) {
465 >                   CompletableFuture<U> dst) {
466              this.arg = arg; this.fn = fn; this.dst = dst;
467          }
468          public final boolean exec() {
# Line 465 | Line 482 | public class CompletableFuture<T> implem
482          private static final long serialVersionUID = 5232453952276885070L;
483      }
484  
485 <    static final class AsyncBiApply<T,U,V> extends Async {
469 <        final BiFun<? super T,? super U,? extends V> fn;
485 >    static final class AsyncCombine<T,U,V> extends Async {
486          final T arg1;
487          final U arg2;
488 +        final BiFun<? super T,? super U,? extends V> fn;
489          final CompletableFuture<V> dst;
490 <        AsyncBiApply(T arg1, U arg2,
491 <                        BiFun<? super T,? super U,? extends V> fn,
492 <                        CompletableFuture<V> dst) {
490 >        AsyncCombine(T arg1, U arg2,
491 >                     BiFun<? super T,? super U,? extends V> fn,
492 >                     CompletableFuture<V> dst) {
493              this.arg1 = arg1; this.arg2 = arg2; this.fn = fn; this.dst = dst;
494          }
495          public final boolean exec() {
# Line 493 | Line 510 | public class CompletableFuture<T> implem
510      }
511  
512      static final class AsyncAccept<T> extends Async {
496        final Action<? super T> fn;
513          final T arg;
514 +        final Action<? super T> fn;
515          final CompletableFuture<Void> dst;
516          AsyncAccept(T arg, Action<? super T> fn,
517 <                   CompletableFuture<Void> dst) {
517 >                    CompletableFuture<Void> dst) {
518              this.arg = arg; this.fn = fn; this.dst = dst;
519          }
520          public final boolean exec() {
# Line 516 | Line 533 | public class CompletableFuture<T> implem
533          private static final long serialVersionUID = 5232453952276885070L;
534      }
535  
536 <    static final class AsyncBiAccept<T,U> extends Async {
520 <        final BiAction<? super T,? super U> fn;
536 >    static final class AsyncAcceptBoth<T,U> extends Async {
537          final T arg1;
538          final U arg2;
539 +        final BiAction<? super T,? super U> fn;
540          final CompletableFuture<Void> dst;
541 <        AsyncBiAccept(T arg1, U arg2,
542 <                     BiAction<? super T,? super U> fn,
543 <                     CompletableFuture<Void> dst) {
541 >        AsyncAcceptBoth(T arg1, U arg2,
542 >                        BiAction<? super T,? super U> fn,
543 >                        CompletableFuture<Void> dst) {
544              this.arg1 = arg1; this.arg2 = arg2; this.fn = fn; this.dst = dst;
545          }
546          public final boolean exec() {
# Line 542 | Line 559 | public class CompletableFuture<T> implem
559          private static final long serialVersionUID = 5232453952276885070L;
560      }
561  
562 +    static final class AsyncCompose<T,U> extends Async {
563 +        final T arg;
564 +        final Fun<? super T, CompletableFuture<U>> fn;
565 +        final CompletableFuture<U> dst;
566 +        AsyncCompose(T arg,
567 +                     Fun<? super T, CompletableFuture<U>> fn,
568 +                     CompletableFuture<U> dst) {
569 +            this.arg = arg; this.fn = fn; this.dst = dst;
570 +        }
571 +        public final boolean exec() {
572 +            CompletableFuture<U> d, fr; U u; Throwable ex;
573 +            if ((d = this.dst) != null && d.result == null) {
574 +                try {
575 +                    fr = fn.apply(arg);
576 +                    ex = (fr == null) ? new NullPointerException() : null;
577 +                } catch (Throwable rex) {
578 +                    ex = rex;
579 +                    fr = null;
580 +                }
581 +                if (ex != null)
582 +                    u = null;
583 +                else {
584 +                    Object r = fr.result;
585 +                    if (r == null)
586 +                        r = fr.waitingGet(false);
587 +                    if (r instanceof AltResult) {
588 +                        ex = ((AltResult)r).ex;
589 +                        u = null;
590 +                    }
591 +                    else {
592 +                        @SuppressWarnings("unchecked") U ur = (U) r;
593 +                        u = ur;
594 +                    }
595 +                }
596 +                d.internalComplete(u, ex);
597 +            }
598 +            return true;
599 +        }
600 +        private static final long serialVersionUID = 5232453952276885070L;
601 +    }
602 +
603      /* ------------- Completions -------------- */
604  
605      /**
# Line 560 | Line 618 | public class CompletableFuture<T> implem
618      abstract static class Completion extends AtomicInteger implements Runnable {
619      }
620  
621 <    static final class ApplyCompletion<T,U> extends Completion {
621 >    static final class ThenApply<T,U> extends Completion {
622          final CompletableFuture<? extends T> src;
623          final Fun<? super T,? extends U> fn;
624          final CompletableFuture<U> dst;
625          final Executor executor;
626 <        ApplyCompletion(CompletableFuture<? extends T> src,
627 <                        Fun<? super T,? extends U> fn,
628 <                        CompletableFuture<U> dst, Executor executor) {
626 >        ThenApply(CompletableFuture<? extends T> src,
627 >                  Fun<? super T,? extends U> fn,
628 >                  CompletableFuture<U> dst,
629 >                  Executor executor) {
630              this.src = src; this.fn = fn; this.dst = dst;
631              this.executor = executor;
632          }
# Line 609 | Line 668 | public class CompletableFuture<T> implem
668          private static final long serialVersionUID = 5232453952276885070L;
669      }
670  
671 <    static final class AcceptCompletion<T> extends Completion {
671 >    static final class ThenAccept<T> extends Completion {
672          final CompletableFuture<? extends T> src;
673          final Action<? super T> fn;
674          final CompletableFuture<Void> dst;
675          final Executor executor;
676 <        AcceptCompletion(CompletableFuture<? extends T> src,
677 <                         Action<? super T> fn,
678 <                         CompletableFuture<Void> dst, Executor executor) {
676 >        ThenAccept(CompletableFuture<? extends T> src,
677 >                   Action<? super T> fn,
678 >                   CompletableFuture<Void> dst,
679 >                   Executor executor) {
680              this.src = src; this.fn = fn; this.dst = dst;
681              this.executor = executor;
682          }
# Line 657 | Line 717 | public class CompletableFuture<T> implem
717          private static final long serialVersionUID = 5232453952276885070L;
718      }
719  
720 <    static final class RunCompletion<T> extends Completion {
721 <        final CompletableFuture<? extends T> src;
720 >    static final class ThenRun extends Completion {
721 >        final CompletableFuture<?> src;
722          final Runnable fn;
723          final CompletableFuture<Void> dst;
724          final Executor executor;
725 <        RunCompletion(CompletableFuture<? extends T> src,
726 <                      Runnable fn,
727 <                      CompletableFuture<Void> dst,
728 <                      Executor executor) {
725 >        ThenRun(CompletableFuture<?> src,
726 >                Runnable fn,
727 >                CompletableFuture<Void> dst,
728 >                Executor executor) {
729              this.src = src; this.fn = fn; this.dst = dst;
730              this.executor = executor;
731          }
732          public final void run() {
733 <            final CompletableFuture<? extends T> a;
733 >            final CompletableFuture<?> a;
734              final Runnable fn;
735              final CompletableFuture<Void> dst;
736              Object r; Throwable ex;
# Line 701 | Line 761 | public class CompletableFuture<T> implem
761          private static final long serialVersionUID = 5232453952276885070L;
762      }
763  
764 <    static final class BiApplyCompletion<T,U,V> extends Completion {
764 >    static final class ThenCombine<T,U,V> extends Completion {
765          final CompletableFuture<? extends T> src;
766          final CompletableFuture<? extends U> snd;
767          final BiFun<? super T,? super U,? extends V> fn;
768          final CompletableFuture<V> dst;
769          final Executor executor;
770 <        BiApplyCompletion(CompletableFuture<? extends T> src,
771 <                          CompletableFuture<? extends U> snd,
772 <                          BiFun<? super T,? super U,? extends V> fn,
773 <                          CompletableFuture<V> dst, Executor executor) {
770 >        ThenCombine(CompletableFuture<? extends T> src,
771 >                    CompletableFuture<? extends U> snd,
772 >                    BiFun<? super T,? super U,? extends V> fn,
773 >                    CompletableFuture<V> dst,
774 >                    Executor executor) {
775              this.src = src; this.snd = snd;
776              this.fn = fn; this.dst = dst;
777              this.executor = executor;
# Line 752 | Line 813 | public class CompletableFuture<T> implem
813                  if (ex == null) {
814                      try {
815                          if (e != null)
816 <                            e.execute(new AsyncBiApply<T,U,V>(t, u, fn, dst));
816 >                            e.execute(new AsyncCombine<T,U,V>(t, u, fn, dst));
817                          else
818                              v = fn.apply(t, u);
819                      } catch (Throwable rex) {
# Line 766 | Line 827 | public class CompletableFuture<T> implem
827          private static final long serialVersionUID = 5232453952276885070L;
828      }
829  
830 <    static final class BiAcceptCompletion<T,U> extends Completion {
830 >    static final class ThenAcceptBoth<T,U> extends Completion {
831          final CompletableFuture<? extends T> src;
832          final CompletableFuture<? extends U> snd;
833          final BiAction<? super T,? super U> fn;
834          final CompletableFuture<Void> dst;
835          final Executor executor;
836 <        BiAcceptCompletion(CompletableFuture<? extends T> src,
837 <                           CompletableFuture<? extends U> snd,
838 <                           BiAction<? super T,? super U> fn,
839 <                           CompletableFuture<Void> dst, Executor executor) {
836 >        ThenAcceptBoth(CompletableFuture<? extends T> src,
837 >                       CompletableFuture<? extends U> snd,
838 >                       BiAction<? super T,? super U> fn,
839 >                       CompletableFuture<Void> dst,
840 >                       Executor executor) {
841              this.src = src; this.snd = snd;
842              this.fn = fn; this.dst = dst;
843              this.executor = executor;
# Line 816 | Line 878 | public class CompletableFuture<T> implem
878                  if (ex == null) {
879                      try {
880                          if (e != null)
881 <                            e.execute(new AsyncBiAccept<T,U>(t, u, fn, dst));
881 >                            e.execute(new AsyncAcceptBoth<T,U>(t, u, fn, dst));
882                          else
883                              fn.accept(t, u);
884                      } catch (Throwable rex) {
# Line 830 | Line 892 | public class CompletableFuture<T> implem
892          private static final long serialVersionUID = 5232453952276885070L;
893      }
894  
895 <    static final class BiRunCompletion<T> extends Completion {
896 <        final CompletableFuture<? extends T> src;
895 >    static final class RunAfterBoth extends Completion {
896 >        final CompletableFuture<?> src;
897          final CompletableFuture<?> snd;
898          final Runnable fn;
899          final CompletableFuture<Void> dst;
900          final Executor executor;
901 <        BiRunCompletion(CompletableFuture<? extends T> src,
902 <                        CompletableFuture<?> snd,
903 <                        Runnable fn,
904 <                        CompletableFuture<Void> dst, Executor executor) {
901 >        RunAfterBoth(CompletableFuture<?> src,
902 >                     CompletableFuture<?> snd,
903 >                     Runnable fn,
904 >                     CompletableFuture<Void> dst,
905 >                     Executor executor) {
906              this.src = src; this.snd = snd;
907              this.fn = fn; this.dst = dst;
908              this.executor = executor;
909          }
910          public final void run() {
911 <            final CompletableFuture<? extends T> a;
911 >            final CompletableFuture<?> a;
912              final CompletableFuture<?> b;
913              final Runnable fn;
914              final CompletableFuture<Void> dst;
# Line 881 | Line 944 | public class CompletableFuture<T> implem
944          private static final long serialVersionUID = 5232453952276885070L;
945      }
946  
947 <    static final class OrApplyCompletion<T,U> extends Completion {
947 >    static final class AndCompletion extends Completion {
948 >        final CompletableFuture<?> src;
949 >        final CompletableFuture<?> snd;
950 >        final CompletableFuture<Void> dst;
951 >        AndCompletion(CompletableFuture<?> src,
952 >                      CompletableFuture<?> snd,
953 >                      CompletableFuture<Void> dst) {
954 >            this.src = src; this.snd = snd; this.dst = dst;
955 >        }
956 >        public final void run() {
957 >            final CompletableFuture<?> a;
958 >            final CompletableFuture<?> b;
959 >            final CompletableFuture<Void> dst;
960 >            Object r, s; Throwable ex;
961 >            if ((dst = this.dst) != null &&
962 >                (a = this.src) != null &&
963 >                (r = a.result) != null &&
964 >                (b = this.snd) != null &&
965 >                (s = b.result) != null &&
966 >                compareAndSet(0, 1)) {
967 >                if (r instanceof AltResult)
968 >                    ex = ((AltResult)r).ex;
969 >                else
970 >                    ex = null;
971 >                if (ex == null && (s instanceof AltResult))
972 >                    ex = ((AltResult)s).ex;
973 >                dst.internalComplete(null, ex);
974 >            }
975 >        }
976 >        private static final long serialVersionUID = 5232453952276885070L;
977 >    }
978 >
979 >    static final class ApplyToEither<T,U> extends Completion {
980          final CompletableFuture<? extends T> src;
981          final CompletableFuture<? extends T> snd;
982          final Fun<? super T,? extends U> fn;
983          final CompletableFuture<U> dst;
984          final Executor executor;
985 <        OrApplyCompletion(CompletableFuture<? extends T> src,
986 <                          CompletableFuture<? extends T> snd,
987 <                          Fun<? super T,? extends U> fn,
988 <                          CompletableFuture<U> dst, Executor executor) {
985 >        ApplyToEither(CompletableFuture<? extends T> src,
986 >                      CompletableFuture<? extends T> snd,
987 >                      Fun<? super T,? extends U> fn,
988 >                      CompletableFuture<U> dst,
989 >                      Executor executor) {
990              this.src = src; this.snd = snd;
991              this.fn = fn; this.dst = dst;
992              this.executor = executor;
# Line 934 | Line 1030 | public class CompletableFuture<T> implem
1030          private static final long serialVersionUID = 5232453952276885070L;
1031      }
1032  
1033 <    static final class OrAcceptCompletion<T> extends Completion {
1033 >    static final class AcceptEither<T> extends Completion {
1034          final CompletableFuture<? extends T> src;
1035          final CompletableFuture<? extends T> snd;
1036          final Action<? super T> fn;
1037          final CompletableFuture<Void> dst;
1038          final Executor executor;
1039 <        OrAcceptCompletion(CompletableFuture<? extends T> src,
1040 <                           CompletableFuture<? extends T> snd,
1041 <                           Action<? super T> fn,
1042 <                           CompletableFuture<Void> dst, Executor executor) {
1039 >        AcceptEither(CompletableFuture<? extends T> src,
1040 >                     CompletableFuture<? extends T> snd,
1041 >                     Action<? super T> fn,
1042 >                     CompletableFuture<Void> dst,
1043 >                     Executor executor) {
1044              this.src = src; this.snd = snd;
1045              this.fn = fn; this.dst = dst;
1046              this.executor = executor;
# Line 986 | Line 1083 | public class CompletableFuture<T> implem
1083          private static final long serialVersionUID = 5232453952276885070L;
1084      }
1085  
1086 <    static final class OrRunCompletion<T> extends Completion {
1087 <        final CompletableFuture<? extends T> src;
1086 >    static final class RunAfterEither extends Completion {
1087 >        final CompletableFuture<?> src;
1088          final CompletableFuture<?> snd;
1089          final Runnable fn;
1090          final CompletableFuture<Void> dst;
1091          final Executor executor;
1092 <        OrRunCompletion(CompletableFuture<? extends T> src,
1093 <                        CompletableFuture<?> snd,
1094 <                        Runnable fn,
1095 <                        CompletableFuture<Void> dst, Executor executor) {
1092 >        RunAfterEither(CompletableFuture<?> src,
1093 >                       CompletableFuture<?> snd,
1094 >                       Runnable fn,
1095 >                       CompletableFuture<Void> dst,
1096 >                       Executor executor) {
1097              this.src = src; this.snd = snd;
1098              this.fn = fn; this.dst = dst;
1099              this.executor = executor;
1100          }
1101          public final void run() {
1102 <            final CompletableFuture<? extends T> a;
1102 >            final CompletableFuture<?> a;
1103              final CompletableFuture<?> b;
1104              final Runnable fn;
1105              final CompletableFuture<Void> dst;
# Line 1033 | Line 1131 | public class CompletableFuture<T> implem
1131          private static final long serialVersionUID = 5232453952276885070L;
1132      }
1133  
1134 +    static final class OrCompletion extends Completion {
1135 +        final CompletableFuture<?> src;
1136 +        final CompletableFuture<?> snd;
1137 +        final CompletableFuture<Object> dst;
1138 +        OrCompletion(CompletableFuture<?> src,
1139 +                     CompletableFuture<?> snd,
1140 +                     CompletableFuture<Object> dst) {
1141 +            this.src = src; this.snd = snd; this.dst = dst;
1142 +        }
1143 +        public final void run() {
1144 +            final CompletableFuture<?> a;
1145 +            final CompletableFuture<?> b;
1146 +            final CompletableFuture<Object> dst;
1147 +            Object r, t; Throwable ex;
1148 +            if ((dst = this.dst) != null &&
1149 +                (((a = this.src) != null && (r = a.result) != null) ||
1150 +                 ((b = this.snd) != null && (r = b.result) != null)) &&
1151 +                compareAndSet(0, 1)) {
1152 +                if (r instanceof AltResult) {
1153 +                    ex = ((AltResult)r).ex;
1154 +                    t = null;
1155 +                }
1156 +                else {
1157 +                    ex = null;
1158 +                    t = r;
1159 +                }
1160 +                dst.internalComplete(t, ex);
1161 +            }
1162 +        }
1163 +        private static final long serialVersionUID = 5232453952276885070L;
1164 +    }
1165 +
1166      static final class ExceptionCompletion<T> extends Completion {
1167          final CompletableFuture<? extends T> src;
1168          final Fun<? super Throwable, ? extends T> fn;
# Line 1053 | Line 1183 | public class CompletableFuture<T> implem
1183                  (r = a.result) != null &&
1184                  compareAndSet(0, 1)) {
1185                  if ((r instanceof AltResult) &&
1186 <                    (ex = ((AltResult)r).ex) != null)  {
1186 >                    (ex = ((AltResult)r).ex) != null) {
1187                      try {
1188                          t = fn.apply(ex);
1189                      } catch (Throwable rex) {
# Line 1071 | Line 1201 | public class CompletableFuture<T> implem
1201      }
1202  
1203      static final class ThenCopy<T> extends Completion {
1204 <        final CompletableFuture<? extends T> src;
1204 >        final CompletableFuture<?> src;
1205          final CompletableFuture<T> dst;
1206 <        ThenCopy(CompletableFuture<? extends T> src,
1206 >        ThenCopy(CompletableFuture<?> src,
1207                   CompletableFuture<T> dst) {
1208              this.src = src; this.dst = dst;
1209          }
1210          public final void run() {
1211 <            final CompletableFuture<? extends T> a;
1211 >            final CompletableFuture<?> a;
1212              final CompletableFuture<T> dst;
1213 <            Object r; Object t; Throwable ex;
1213 >            Object r; T t; Throwable ex;
1214              if ((dst = this.dst) != null &&
1215                  (a = this.src) != null &&
1216                  (r = a.result) != null &&
# Line 1091 | Line 1221 | public class CompletableFuture<T> implem
1221                  }
1222                  else {
1223                      ex = null;
1224 <                    t = r;
1224 >                    @SuppressWarnings("unchecked") T tr = (T) r;
1225 >                    t = tr;
1226                  }
1227                  dst.internalComplete(t, ex);
1228              }
# Line 1099 | Line 1230 | public class CompletableFuture<T> implem
1230          private static final long serialVersionUID = 5232453952276885070L;
1231      }
1232  
1233 +    // version of ThenCopy for CompletableFuture<Void> dst
1234 +    static final class ThenPropagate extends Completion {
1235 +        final CompletableFuture<?> src;
1236 +        final CompletableFuture<Void> dst;
1237 +        ThenPropagate(CompletableFuture<?> src,
1238 +                      CompletableFuture<Void> dst) {
1239 +            this.src = src; this.dst = dst;
1240 +        }
1241 +        public final void run() {
1242 +            final CompletableFuture<?> a;
1243 +            final CompletableFuture<Void> dst;
1244 +            Object r; Throwable ex;
1245 +            if ((dst = this.dst) != null &&
1246 +                (a = this.src) != null &&
1247 +                (r = a.result) != null &&
1248 +                compareAndSet(0, 1)) {
1249 +                if (r instanceof AltResult)
1250 +                    ex = ((AltResult)r).ex;
1251 +                else
1252 +                    ex = null;
1253 +                dst.internalComplete(null, ex);
1254 +            }
1255 +        }
1256 +        private static final long serialVersionUID = 5232453952276885070L;
1257 +    }
1258 +
1259      static final class HandleCompletion<T,U> extends Completion {
1260          final CompletableFuture<? extends T> src;
1261          final BiFun<? super T, Throwable, ? extends U> fn;
1262          final CompletableFuture<U> dst;
1263          HandleCompletion(CompletableFuture<? extends T> src,
1264                           BiFun<? super T, Throwable, ? extends U> fn,
1265 <                         final CompletableFuture<U> dst) {
1265 >                         CompletableFuture<U> dst) {
1266              this.src = src; this.fn = fn; this.dst = dst;
1267          }
1268          public final void run() {
# Line 1139 | Line 1296 | public class CompletableFuture<T> implem
1296          private static final long serialVersionUID = 5232453952276885070L;
1297      }
1298  
1299 <    static final class ComposeCompletion<T,U> extends Completion {
1299 >    static final class ThenCompose<T,U> extends Completion {
1300          final CompletableFuture<? extends T> src;
1301          final Fun<? super T, CompletableFuture<U>> fn;
1302          final CompletableFuture<U> dst;
1303 <        ComposeCompletion(CompletableFuture<? extends T> src,
1304 <                          Fun<? super T, CompletableFuture<U>> fn,
1305 <                          final CompletableFuture<U> dst) {
1303 >        final Executor executor;
1304 >        ThenCompose(CompletableFuture<? extends T> src,
1305 >                    Fun<? super T, CompletableFuture<U>> fn,
1306 >                    CompletableFuture<U> dst,
1307 >                    Executor executor) {
1308              this.src = src; this.fn = fn; this.dst = dst;
1309 +            this.executor = executor;
1310          }
1311          public final void run() {
1312              final CompletableFuture<? extends T> a;
1313              final Fun<? super T, CompletableFuture<U>> fn;
1314              final CompletableFuture<U> dst;
1315 <            Object r; T t; Throwable ex;
1315 >            Object r; T t; Throwable ex; Executor e;
1316              if ((dst = this.dst) != null &&
1317                  (fn = this.fn) != null &&
1318                  (a = this.src) != null &&
# Line 1171 | Line 1331 | public class CompletableFuture<T> implem
1331                  U u = null;
1332                  boolean complete = false;
1333                  if (ex == null) {
1334 <                    try {
1335 <                        c = fn.apply(t);
1336 <                    } catch (Throwable rex) {
1337 <                        ex = rex;
1334 >                    if ((e = executor) != null)
1335 >                        e.execute(new AsyncCompose<T,U>(t, fn, dst));
1336 >                    else {
1337 >                        try {
1338 >                            if ((c = fn.apply(t)) == null)
1339 >                                ex = new NullPointerException();
1340 >                        } catch (Throwable rex) {
1341 >                            ex = rex;
1342 >                        }
1343                      }
1344                  }
1345 <                if (ex != null || c == null) {
1181 <                    if (ex == null)
1182 <                        ex = new NullPointerException();
1183 <                }
1184 <                else {
1345 >                if (c != null) {
1346                      ThenCopy<U> d = null;
1347                      Object s;
1348                      if ((s = c.result) == null) {
# Line 1223 | Line 1384 | public class CompletableFuture<T> implem
1384      }
1385  
1386      /**
1387 <     * Asynchronously executes in the {@link
1388 <     * ForkJoinPool#commonPool()}, a task that completes the returned
1389 <     * CompletableFuture with the result of the given Supplier.
1387 >     * Returns a new CompletableFuture that is asynchronously completed
1388 >     * by a task running in the {@link ForkJoinPool#commonPool()} with
1389 >     * the value obtained by calling the given Generator.
1390       *
1391       * @param supplier a function returning the value to be used
1392       * to complete the returned CompletableFuture
1393 <     * @return the CompletableFuture
1393 >     * @return the new CompletableFuture
1394       */
1395      public static <U> CompletableFuture<U> supplyAsync(Generator<U> supplier) {
1396          if (supplier == null) throw new NullPointerException();
# Line 1240 | Line 1401 | public class CompletableFuture<T> implem
1401      }
1402  
1403      /**
1404 <     * Asynchronously executes using the given executor, a task that
1405 <     * completes the returned CompletableFuture with the result of the
1406 <     * given Supplier.
1404 >     * Returns a new CompletableFuture that is asynchronously completed
1405 >     * by a task running in the given executor with the value obtained
1406 >     * by calling the given Generator.
1407       *
1408       * @param supplier a function returning the value to be used
1409       * to complete the returned CompletableFuture
1410       * @param executor the executor to use for asynchronous execution
1411 <     * @return the CompletableFuture
1411 >     * @return the new CompletableFuture
1412       */
1413      public static <U> CompletableFuture<U> supplyAsync(Generator<U> supplier,
1414                                                         Executor executor) {
# Line 1259 | Line 1420 | public class CompletableFuture<T> implem
1420      }
1421  
1422      /**
1423 <     * Asynchronously executes in the {@link
1424 <     * ForkJoinPool#commonPool()} a task that runs the given action,
1425 <     * and then completes the returned CompletableFuture.
1423 >     * Returns a new CompletableFuture that is asynchronously completed
1424 >     * by a task running in the {@link ForkJoinPool#commonPool()} after
1425 >     * it runs the given action.
1426       *
1427       * @param runnable the action to run before completing the
1428       * returned CompletableFuture
1429 <     * @return the CompletableFuture
1429 >     * @return the new CompletableFuture
1430       */
1431      public static CompletableFuture<Void> runAsync(Runnable runnable) {
1432          if (runnable == null) throw new NullPointerException();
# Line 1276 | Line 1437 | public class CompletableFuture<T> implem
1437      }
1438  
1439      /**
1440 <     * Asynchronously executes using the given executor, a task that
1441 <     * runs the given action, and then completes the returned
1442 <     * CompletableFuture.
1440 >     * Returns a new CompletableFuture that is asynchronously completed
1441 >     * by a task running in the given executor after it runs the given
1442 >     * action.
1443       *
1444       * @param runnable the action to run before completing the
1445       * returned CompletableFuture
1446       * @param executor the executor to use for asynchronous execution
1447 <     * @return the CompletableFuture
1447 >     * @return the new CompletableFuture
1448       */
1449      public static CompletableFuture<Void> runAsync(Runnable runnable,
1450                                                     Executor executor) {
# Line 1295 | Line 1456 | public class CompletableFuture<T> implem
1456      }
1457  
1458      /**
1459 +     * Returns a new CompletableFuture that is already completed with
1460 +     * the given value.
1461 +     *
1462 +     * @param value the value
1463 +     * @return the completed CompletableFuture
1464 +     */
1465 +    public static <U> CompletableFuture<U> completedFuture(U value) {
1466 +        CompletableFuture<U> f = new CompletableFuture<U>();
1467 +        f.result = (value == null) ? NIL : value;
1468 +        return f;
1469 +    }
1470 +
1471 +    /**
1472       * Returns {@code true} if completed in any fashion: normally,
1473       * exceptionally, or via cancellation.
1474       *
# Line 1305 | Line 1479 | public class CompletableFuture<T> implem
1479      }
1480  
1481      /**
1482 <     * Waits if necessary for the computation to complete, and then
1483 <     * retrieves its result.
1482 >     * Waits if necessary for this future to complete, and then
1483 >     * returns its result.
1484       *
1485 <     * @return the computed result
1486 <     * @throws CancellationException if the computation was cancelled
1487 <     * @throws ExecutionException if the computation threw an
1314 <     * exception
1485 >     * @return the result value
1486 >     * @throws CancellationException if this future was cancelled
1487 >     * @throws ExecutionException if this future completed exceptionally
1488       * @throws InterruptedException if the current thread was interrupted
1489       * while waiting
1490       */
# Line 1334 | Line 1507 | public class CompletableFuture<T> implem
1507      }
1508  
1509      /**
1510 <     * Waits if necessary for at most the given time for completion,
1511 <     * and then retrieves its result, if available.
1510 >     * Waits if necessary for at most the given time for this future
1511 >     * to complete, and then returns its result, if available.
1512       *
1513       * @param timeout the maximum time to wait
1514       * @param unit the time unit of the timeout argument
1515 <     * @return the computed result
1516 <     * @throws CancellationException if the computation was cancelled
1517 <     * @throws ExecutionException if the computation threw an
1345 <     * exception
1515 >     * @return the result value
1516 >     * @throws CancellationException if this future was cancelled
1517 >     * @throws ExecutionException if this future completed exceptionally
1518       * @throws InterruptedException if the current thread was interrupted
1519       * while waiting
1520       * @throws TimeoutException if the wait timed out
# Line 1380 | Line 1552 | public class CompletableFuture<T> implem
1552       *
1553       * @return the result value
1554       * @throws CancellationException if the computation was cancelled
1555 <     * @throws CompletionException if a completion computation threw
1556 <     * an exception
1555 >     * @throws CompletionException if this future completed
1556 >     * exceptionally or a completion computation threw an exception
1557       */
1558      public T join() {
1559          Object r; Throwable ex;
# Line 1407 | Line 1579 | public class CompletableFuture<T> implem
1579       * @param valueIfAbsent the value to return if not completed
1580       * @return the result value, if completed, else the given valueIfAbsent
1581       * @throws CancellationException if the computation was cancelled
1582 <     * @throws CompletionException if a completion computation threw
1583 <     * an exception
1582 >     * @throws CompletionException if this future completed
1583 >     * exceptionally or a completion computation threw an exception
1584       */
1585      public T getNow(T valueIfAbsent) {
1586          Object r; Throwable ex;
# Line 1460 | Line 1632 | public class CompletableFuture<T> implem
1632      }
1633  
1634      /**
1635 <     * Creates and returns a CompletableFuture that is completed with
1636 <     * the result of the given function of this CompletableFuture.
1637 <     * If this CompletableFuture completes exceptionally,
1638 <     * then the returned CompletableFuture also does so,
1639 <     * with a CompletionException holding this exception as
1640 <     * its cause.
1635 >     * Returns a new CompletableFuture that is completed
1636 >     * when this CompletableFuture completes, with the result of the
1637 >     * given function of this CompletableFuture's result.
1638 >     *
1639 >     * <p>If this CompletableFuture completes exceptionally, or the
1640 >     * supplied function throws an exception, then the returned
1641 >     * CompletableFuture completes exceptionally with a
1642 >     * CompletionException holding the exception as its cause.
1643       *
1644       * @param fn the function to use to compute the value of
1645       * the returned CompletableFuture
# Line 1476 | Line 1650 | public class CompletableFuture<T> implem
1650      }
1651  
1652      /**
1653 <     * Creates and returns a CompletableFuture that is asynchronously
1654 <     * completed using the {@link ForkJoinPool#commonPool()} with the
1655 <     * result of the given function of this CompletableFuture.  If
1656 <     * this CompletableFuture completes exceptionally, then the
1657 <     * returned CompletableFuture also does so, with a
1658 <     * CompletionException holding this exception as its cause.
1653 >     * Returns a new CompletableFuture that is asynchronously completed
1654 >     * when this CompletableFuture completes, with the result of the
1655 >     * given function of this CompletableFuture's result from a
1656 >     * task running in the {@link ForkJoinPool#commonPool()}.
1657 >     *
1658 >     * <p>If this CompletableFuture completes exceptionally, or the
1659 >     * supplied function throws an exception, then the returned
1660 >     * CompletableFuture completes exceptionally with a
1661 >     * CompletionException holding the exception as its cause.
1662       *
1663       * @param fn the function to use to compute the value of
1664       * the returned CompletableFuture
1665       * @return the new CompletableFuture
1666       */
1667 <    public <U> CompletableFuture<U> thenApplyAsync(Fun<? super T,? extends U> fn) {
1667 >    public <U> CompletableFuture<U> thenApplyAsync
1668 >        (Fun<? super T,? extends U> fn) {
1669          return doThenApply(fn, ForkJoinPool.commonPool());
1670      }
1671  
1672      /**
1673 <     * Creates and returns a CompletableFuture that is asynchronously
1674 <     * completed using the given executor with the result of the given
1675 <     * function of this CompletableFuture.  If this CompletableFuture
1676 <     * completes exceptionally, then the returned CompletableFuture
1677 <     * also does so, with a CompletionException holding this exception as
1678 <     * its cause.
1673 >     * Returns a new CompletableFuture that is asynchronously completed
1674 >     * when this CompletableFuture completes, with the result of the
1675 >     * given function of this CompletableFuture's result from a
1676 >     * task running in the given executor.
1677 >     *
1678 >     * <p>If this CompletableFuture completes exceptionally, or the
1679 >     * supplied function throws an exception, then the returned
1680 >     * CompletableFuture completes exceptionally with a
1681 >     * CompletionException holding the exception as its cause.
1682       *
1683       * @param fn the function to use to compute the value of
1684       * the returned CompletableFuture
1685       * @param executor the executor to use for asynchronous execution
1686       * @return the new CompletableFuture
1687       */
1688 <    public <U> CompletableFuture<U> thenApplyAsync(Fun<? super T,? extends U> fn,
1689 <                                                   Executor executor) {
1688 >    public <U> CompletableFuture<U> thenApplyAsync
1689 >        (Fun<? super T,? extends U> fn,
1690 >         Executor executor) {
1691          if (executor == null) throw new NullPointerException();
1692          return doThenApply(fn, executor);
1693      }
1694  
1695 <    private <U> CompletableFuture<U> doThenApply(Fun<? super T,? extends U> fn,
1696 <                                                 Executor e) {
1695 >    private <U> CompletableFuture<U> doThenApply
1696 >        (Fun<? super T,? extends U> fn,
1697 >         Executor e) {
1698          if (fn == null) throw new NullPointerException();
1699          CompletableFuture<U> dst = new CompletableFuture<U>();
1700 <        ApplyCompletion<T,U> d = null;
1700 >        ThenApply<T,U> d = null;
1701          Object r;
1702          if ((r = result) == null) {
1703              CompletionNode p = new CompletionNode
1704 <                (d = new ApplyCompletion<T,U>(this, fn, dst, e));
1704 >                (d = new ThenApply<T,U>(this, fn, dst, e));
1705              while ((r = result) == null) {
1706                  if (UNSAFE.compareAndSwapObject
1707                      (this, COMPLETIONS, p.next = completions, p))
# Line 1555 | Line 1738 | public class CompletableFuture<T> implem
1738      }
1739  
1740      /**
1741 <     * Creates and returns a CompletableFuture that is completed after
1742 <     * performing the given action with this CompletableFuture's
1743 <     * result when it completes.  If this CompletableFuture
1744 <     * completes exceptionally, then the returned CompletableFuture
1745 <     * also does so, with a CompletionException holding this exception as
1746 <     * its cause.
1741 >     * Returns a new CompletableFuture that is completed
1742 >     * when this CompletableFuture completes, after performing the given
1743 >     * action with this CompletableFuture's result.
1744 >     *
1745 >     * <p>If this CompletableFuture completes exceptionally, or the
1746 >     * supplied action throws an exception, then the returned
1747 >     * CompletableFuture completes exceptionally with a
1748 >     * CompletionException holding the exception as its cause.
1749       *
1750       * @param block the action to perform before completing the
1751       * returned CompletableFuture
# Line 1571 | Line 1756 | public class CompletableFuture<T> implem
1756      }
1757  
1758      /**
1759 <     * Creates and returns a CompletableFuture that is asynchronously
1760 <     * completed using the {@link ForkJoinPool#commonPool()} with this
1761 <     * CompletableFuture's result when it completes.  If this
1762 <     * CompletableFuture completes exceptionally, then the returned
1763 <     * CompletableFuture also does so, with a CompletionException holding
1764 <     * this exception as its cause.
1759 >     * Returns a new CompletableFuture that is asynchronously completed
1760 >     * when this CompletableFuture completes, after performing the given
1761 >     * action with this CompletableFuture's result from a task running
1762 >     * in the {@link ForkJoinPool#commonPool()}.
1763 >     *
1764 >     * <p>If this CompletableFuture completes exceptionally, or the
1765 >     * supplied action throws an exception, then the returned
1766 >     * CompletableFuture completes exceptionally with a
1767 >     * CompletionException holding the exception as its cause.
1768       *
1769       * @param block the action to perform before completing the
1770       * returned CompletableFuture
# Line 1587 | Line 1775 | public class CompletableFuture<T> implem
1775      }
1776  
1777      /**
1778 <     * Creates and returns a CompletableFuture that is asynchronously
1779 <     * completed using the given executor with this
1780 <     * CompletableFuture's result when it completes.  If this
1781 <     * CompletableFuture completes exceptionally, then the returned
1782 <     * CompletableFuture also does so, with a CompletionException holding
1783 <     * this exception as its cause.
1778 >     * Returns a new CompletableFuture that is asynchronously completed
1779 >     * when this CompletableFuture completes, after performing the given
1780 >     * action with this CompletableFuture's result from a task running
1781 >     * in the given executor.
1782 >     *
1783 >     * <p>If this CompletableFuture completes exceptionally, or the
1784 >     * supplied action throws an exception, then the returned
1785 >     * CompletableFuture completes exceptionally with a
1786 >     * CompletionException holding the exception as its cause.
1787       *
1788       * @param block the action to perform before completing the
1789       * returned CompletableFuture
# Line 1609 | Line 1800 | public class CompletableFuture<T> implem
1800                                                   Executor e) {
1801          if (fn == null) throw new NullPointerException();
1802          CompletableFuture<Void> dst = new CompletableFuture<Void>();
1803 <        AcceptCompletion<T> d = null;
1803 >        ThenAccept<T> d = null;
1804          Object r;
1805          if ((r = result) == null) {
1806              CompletionNode p = new CompletionNode
1807 <                (d = new AcceptCompletion<T>(this, fn, dst, e));
1807 >                (d = new ThenAccept<T>(this, fn, dst, e));
1808              while ((r = result) == null) {
1809                  if (UNSAFE.compareAndSwapObject
1810                      (this, COMPLETIONS, p.next = completions, p))
# Line 1649 | Line 1840 | public class CompletableFuture<T> implem
1840      }
1841  
1842      /**
1843 <     * Creates and returns a CompletableFuture that is completed after
1844 <     * performing the given action when this CompletableFuture
1845 <     * completes.  If this CompletableFuture completes exceptionally,
1846 <     * then the returned CompletableFuture also does so, with a
1847 <     * CompletionException holding this exception as its cause.
1843 >     * Returns a new CompletableFuture that is completed
1844 >     * when this CompletableFuture completes, after performing the given
1845 >     * action.
1846 >     *
1847 >     * <p>If this CompletableFuture completes exceptionally, or the
1848 >     * supplied action throws an exception, then the returned
1849 >     * CompletableFuture completes exceptionally with a
1850 >     * CompletionException holding the exception as its cause.
1851       *
1852       * @param action the action to perform before completing the
1853       * returned CompletableFuture
# Line 1664 | Line 1858 | public class CompletableFuture<T> implem
1858      }
1859  
1860      /**
1861 <     * Creates and returns a CompletableFuture that is asynchronously
1862 <     * completed using the {@link ForkJoinPool#commonPool()} after
1863 <     * performing the given action when this CompletableFuture
1864 <     * completes.  If this CompletableFuture completes exceptionally,
1865 <     * then the returned CompletableFuture also does so, with a
1866 <     * CompletionException holding this exception as its cause.
1861 >     * Returns a new CompletableFuture that is asynchronously completed
1862 >     * when this CompletableFuture completes, after performing the given
1863 >     * action from a task running in the {@link ForkJoinPool#commonPool()}.
1864 >     *
1865 >     * <p>If this CompletableFuture completes exceptionally, or the
1866 >     * supplied action throws an exception, then the returned
1867 >     * CompletableFuture completes exceptionally with a
1868 >     * CompletionException holding the exception as its cause.
1869       *
1870       * @param action the action to perform before completing the
1871       * returned CompletableFuture
# Line 1680 | Line 1876 | public class CompletableFuture<T> implem
1876      }
1877  
1878      /**
1879 <     * Creates and returns a CompletableFuture that is asynchronously
1880 <     * completed using the given executor after performing the given
1881 <     * action when this CompletableFuture completes.  If this
1882 <     * CompletableFuture completes exceptionally, then the returned
1883 <     * CompletableFuture also does so, with a CompletionException holding
1884 <     * this exception as its cause.
1879 >     * Returns a new CompletableFuture that is asynchronously completed
1880 >     * when this CompletableFuture completes, after performing the given
1881 >     * action from a task running in the given executor.
1882 >     *
1883 >     * <p>If this CompletableFuture completes exceptionally, or the
1884 >     * supplied action throws an exception, then the returned
1885 >     * CompletableFuture completes exceptionally with a
1886 >     * CompletionException holding the exception as its cause.
1887       *
1888       * @param action the action to perform before completing the
1889       * returned CompletableFuture
# Line 1702 | Line 1900 | public class CompletableFuture<T> implem
1900                                                Executor e) {
1901          if (action == null) throw new NullPointerException();
1902          CompletableFuture<Void> dst = new CompletableFuture<Void>();
1903 <        RunCompletion<T> d = null;
1903 >        ThenRun d = null;
1904          Object r;
1905          if ((r = result) == null) {
1906              CompletionNode p = new CompletionNode
1907 <                (d = new RunCompletion<T>(this, action, dst, e));
1907 >                (d = new ThenRun(this, action, dst, e));
1908              while ((r = result) == null) {
1909                  if (UNSAFE.compareAndSwapObject
1910                      (this, COMPLETIONS, p.next = completions, p))
# Line 1737 | Line 1935 | public class CompletableFuture<T> implem
1935      }
1936  
1937      /**
1938 <     * Creates and returns a CompletableFuture that is completed with
1939 <     * the result of the given function of this and the other given
1940 <     * CompletableFuture's results when both complete.  If this or
1941 <     * the other CompletableFuture complete exceptionally, then the
1942 <     * returned CompletableFuture also does so, with a
1943 <     * CompletionException holding the exception as its cause.
1938 >     * Returns a new CompletableFuture that is completed
1939 >     * when both this and the other given CompletableFuture complete,
1940 >     * with the result of the given function of the results of the two
1941 >     * CompletableFutures.
1942 >     *
1943 >     * <p>If this and/or the other CompletableFuture complete
1944 >     * exceptionally, or the supplied function throws an exception,
1945 >     * then the returned CompletableFuture completes exceptionally
1946 >     * with a CompletionException holding the exception as its cause.
1947       *
1948       * @param other the other CompletableFuture
1949       * @param fn the function to use to compute the value of
1950       * the returned CompletableFuture
1951       * @return the new CompletableFuture
1952       */
1953 <    public <U,V> CompletableFuture<V> thenCombine(CompletableFuture<? extends U> other,
1954 <                                                  BiFun<? super T,? super U,? extends V> fn) {
1955 <        return doThenBiApply(other, fn, null);
1953 >    public <U,V> CompletableFuture<V> thenCombine
1954 >        (CompletableFuture<? extends U> other,
1955 >         BiFun<? super T,? super U,? extends V> fn) {
1956 >        return doThenCombine(other, fn, null);
1957      }
1958  
1959      /**
1960 <     * Creates and returns a CompletableFuture that is asynchronously
1961 <     * completed using the {@link ForkJoinPool#commonPool()} with
1962 <     * the result of the given function of this and the other given
1963 <     * CompletableFuture's results when both complete.  If this or
1964 <     * the other CompletableFuture complete exceptionally, then the
1965 <     * returned CompletableFuture also does so, with a
1966 <     * CompletionException holding the exception as its cause.
1960 >     * Returns a new CompletableFuture that is asynchronously completed
1961 >     * when both this and the other given CompletableFuture complete,
1962 >     * with the result of the given function of the results of the two
1963 >     * CompletableFutures from a task running in the
1964 >     * {@link ForkJoinPool#commonPool()}.
1965 >     *
1966 >     * <p>If this and/or the other CompletableFuture complete
1967 >     * exceptionally, or the supplied function throws an exception,
1968 >     * then the returned CompletableFuture completes exceptionally
1969 >     * with a CompletionException holding the exception as its cause.
1970       *
1971       * @param other the other CompletableFuture
1972       * @param fn the function to use to compute the value of
1973       * the returned CompletableFuture
1974       * @return the new CompletableFuture
1975       */
1976 <    public <U,V> CompletableFuture<V> thenCombineAsync(CompletableFuture<? extends U> other,
1977 <                                                       BiFun<? super T,? super U,? extends V> fn) {
1978 <        return doThenBiApply(other, fn, ForkJoinPool.commonPool());
1976 >    public <U,V> CompletableFuture<V> thenCombineAsync
1977 >        (CompletableFuture<? extends U> other,
1978 >         BiFun<? super T,? super U,? extends V> fn) {
1979 >        return doThenCombine(other, fn, ForkJoinPool.commonPool());
1980      }
1981  
1982      /**
1983 <     * Creates and returns a CompletableFuture that is
1984 <     * asynchronously completed using the given executor with the
1985 <     * result of the given function of this and the other given
1986 <     * CompletableFuture's results when both complete.  If this or
1987 <     * the other CompletableFuture complete exceptionally, then the
1988 <     * returned CompletableFuture also does so, with a
1989 <     * CompletionException holding the exception as its cause.
1983 >     * Returns a new CompletableFuture that is asynchronously completed
1984 >     * when both this and the other given CompletableFuture complete,
1985 >     * with the result of the given function of the results of the two
1986 >     * CompletableFutures from a task running in the given executor.
1987 >     *
1988 >     * <p>If this and/or the other CompletableFuture complete
1989 >     * exceptionally, or the supplied function throws an exception,
1990 >     * then the returned CompletableFuture completes exceptionally
1991 >     * with a CompletionException holding the exception as its cause.
1992       *
1993       * @param other the other CompletableFuture
1994       * @param fn the function to use to compute the value of
# Line 1788 | Line 1996 | public class CompletableFuture<T> implem
1996       * @param executor the executor to use for asynchronous execution
1997       * @return the new CompletableFuture
1998       */
1999 <    public <U,V> CompletableFuture<V> thenCombineAsync(CompletableFuture<? extends U> other,
2000 <                                                       BiFun<? super T,? super U,? extends V> fn,
2001 <                                                       Executor executor) {
1999 >    public <U,V> CompletableFuture<V> thenCombineAsync
2000 >        (CompletableFuture<? extends U> other,
2001 >         BiFun<? super T,? super U,? extends V> fn,
2002 >         Executor executor) {
2003          if (executor == null) throw new NullPointerException();
2004 <        return doThenBiApply(other, fn, executor);
2004 >        return doThenCombine(other, fn, executor);
2005      }
2006  
2007 <    private <U,V> CompletableFuture<V> doThenBiApply(CompletableFuture<? extends U> other,
2008 <                                                     BiFun<? super T,? super U,? extends V> fn,
2009 <                                                     Executor e) {
2007 >    private <U,V> CompletableFuture<V> doThenCombine
2008 >        (CompletableFuture<? extends U> other,
2009 >         BiFun<? super T,? super U,? extends V> fn,
2010 >         Executor e) {
2011          if (other == null || fn == null) throw new NullPointerException();
2012          CompletableFuture<V> dst = new CompletableFuture<V>();
2013 <        BiApplyCompletion<T,U,V> d = null;
2013 >        ThenCombine<T,U,V> d = null;
2014          Object r, s = null;
2015          if ((r = result) == null || (s = other.result) == null) {
2016 <            d = new BiApplyCompletion<T,U,V>(this, other, fn, dst, e);
2016 >            d = new ThenCombine<T,U,V>(this, other, fn, dst, e);
2017              CompletionNode q = null, p = new CompletionNode(d);
2018              while ((r == null && (r = result) == null) ||
2019                     (s == null && (s = other.result) == null)) {
# Line 1847 | Line 2057 | public class CompletableFuture<T> implem
2057              if (ex == null) {
2058                  try {
2059                      if (e != null)
2060 <                        e.execute(new AsyncBiApply<T,U,V>(t, u, fn, dst));
2060 >                        e.execute(new AsyncCombine<T,U,V>(t, u, fn, dst));
2061                      else
2062                          v = fn.apply(t, u);
2063                  } catch (Throwable rex) {
# Line 1863 | Line 2073 | public class CompletableFuture<T> implem
2073      }
2074  
2075      /**
2076 <     * Creates and returns a CompletableFuture that is completed with
2077 <     * the results of this and the other given CompletableFuture if
2078 <     * both complete.  If this and/or the other CompletableFuture
2079 <     * complete exceptionally, then the returned CompletableFuture
2080 <     * also does so, with a CompletionException holding one of these
2081 <     * exceptions as its cause.
2076 >     * Returns a new CompletableFuture that is completed
2077 >     * when both this and the other given CompletableFuture complete,
2078 >     * after performing the given action with the results of the two
2079 >     * CompletableFutures.
2080 >     *
2081 >     * <p>If this and/or the other CompletableFuture complete
2082 >     * exceptionally, or the supplied action throws an exception,
2083 >     * then the returned CompletableFuture completes exceptionally
2084 >     * with a CompletionException holding the exception as its cause.
2085       *
2086       * @param other the other CompletableFuture
2087       * @param block the action to perform before completing the
2088       * returned CompletableFuture
2089       * @return the new CompletableFuture
2090       */
2091 <    public <U> CompletableFuture<Void> thenAcceptBoth(CompletableFuture<? extends U> other,
2092 <                                                      BiAction<? super T, ? super U> block) {
2093 <        return doThenBiAccept(other, block, null);
2091 >    public <U> CompletableFuture<Void> thenAcceptBoth
2092 >        (CompletableFuture<? extends U> other,
2093 >         BiAction<? super T, ? super U> block) {
2094 >        return doThenAcceptBoth(other, block, null);
2095      }
2096  
2097      /**
2098 <     * Creates and returns a CompletableFuture that is completed
2099 <     * asynchronously using the {@link ForkJoinPool#commonPool()} with
2100 <     * the results of this and the other given CompletableFuture when
2101 <     * both complete.  If this and/or the other CompletableFuture
2102 <     * complete exceptionally, then the returned CompletableFuture
2103 <     * also does so, with a CompletionException holding one of these
2104 <     * exceptions as its cause.
2098 >     * Returns a new CompletableFuture that is asynchronously completed
2099 >     * when both this and the other given CompletableFuture complete,
2100 >     * after performing the given action with the results of the two
2101 >     * CompletableFutures from a task running in the {@link
2102 >     * ForkJoinPool#commonPool()}.
2103 >     *
2104 >     * <p>If this and/or the other CompletableFuture complete
2105 >     * exceptionally, or the supplied action throws an exception,
2106 >     * then the returned CompletableFuture completes exceptionally
2107 >     * with a CompletionException holding the exception as its cause.
2108       *
2109       * @param other the other CompletableFuture
2110       * @param block the action to perform before completing the
2111       * returned CompletableFuture
2112       * @return the new CompletableFuture
2113       */
2114 <    public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletableFuture<? extends U> other,
2115 <                                                           BiAction<? super T, ? super U> block) {
2116 <        return doThenBiAccept(other, block, ForkJoinPool.commonPool());
2114 >    public <U> CompletableFuture<Void> thenAcceptBothAsync
2115 >        (CompletableFuture<? extends U> other,
2116 >         BiAction<? super T, ? super U> block) {
2117 >        return doThenAcceptBoth(other, block, ForkJoinPool.commonPool());
2118      }
2119  
2120      /**
2121 <     * Creates and returns a CompletableFuture that is completed
2122 <     * asynchronously using the given executor with the results of
2123 <     * this and the other given CompletableFuture when both complete.
2124 <     * If this and/or the other CompletableFuture complete
2125 <     * exceptionally, then the returned CompletableFuture also does
2126 <     * so, with a CompletionException holding one of these exceptions as
2127 <     * its cause.
2121 >     * Returns a new CompletableFuture that is asynchronously completed
2122 >     * when both this and the other given CompletableFuture complete,
2123 >     * after performing the given action with the results of the two
2124 >     * CompletableFutures from a task running in the given executor.
2125 >     *
2126 >     * <p>If this and/or the other CompletableFuture complete
2127 >     * exceptionally, or the supplied action throws an exception,
2128 >     * then the returned CompletableFuture completes exceptionally
2129 >     * with a CompletionException holding the exception as its cause.
2130       *
2131       * @param other the other CompletableFuture
2132       * @param block the action to perform before completing the
# Line 1914 | Line 2134 | public class CompletableFuture<T> implem
2134       * @param executor the executor to use for asynchronous execution
2135       * @return the new CompletableFuture
2136       */
2137 <    public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletableFuture<? extends U> other,
2138 <                                                           BiAction<? super T, ? super U> block,
2139 <                                                           Executor executor) {
2137 >    public <U> CompletableFuture<Void> thenAcceptBothAsync
2138 >        (CompletableFuture<? extends U> other,
2139 >         BiAction<? super T, ? super U> block,
2140 >         Executor executor) {
2141          if (executor == null) throw new NullPointerException();
2142 <        return doThenBiAccept(other, block, executor);
2142 >        return doThenAcceptBoth(other, block, executor);
2143      }
2144  
2145 <    private <U> CompletableFuture<Void> doThenBiAccept(CompletableFuture<? extends U> other,
2146 <                                                       BiAction<? super T,? super U> fn,
2147 <                                                       Executor e) {
2145 >    private <U> CompletableFuture<Void> doThenAcceptBoth
2146 >        (CompletableFuture<? extends U> other,
2147 >         BiAction<? super T,? super U> fn,
2148 >         Executor e) {
2149          if (other == null || fn == null) throw new NullPointerException();
2150          CompletableFuture<Void> dst = new CompletableFuture<Void>();
2151 <        BiAcceptCompletion<T,U> d = null;
2151 >        ThenAcceptBoth<T,U> d = null;
2152          Object r, s = null;
2153          if ((r = result) == null || (s = other.result) == null) {
2154 <            d = new BiAcceptCompletion<T,U>(this, other, fn, dst, e);
2154 >            d = new ThenAcceptBoth<T,U>(this, other, fn, dst, e);
2155              CompletionNode q = null, p = new CompletionNode(d);
2156              while ((r == null && (r = result) == null) ||
2157                     (s == null && (s = other.result) == null)) {
# Line 1972 | Line 2194 | public class CompletableFuture<T> implem
2194              if (ex == null) {
2195                  try {
2196                      if (e != null)
2197 <                        e.execute(new AsyncBiAccept<T,U>(t, u, fn, dst));
2197 >                        e.execute(new AsyncAcceptBoth<T,U>(t, u, fn, dst));
2198                      else
2199                          fn.accept(t, u);
2200                  } catch (Throwable rex) {
# Line 1988 | Line 2210 | public class CompletableFuture<T> implem
2210      }
2211  
2212      /**
2213 <     * Creates and returns a CompletableFuture that is completed
2214 <     * when this and the other given CompletableFuture both
2215 <     * complete.  If this and/or the other CompletableFuture complete
2216 <     * exceptionally, then the returned CompletableFuture also does
2217 <     * so, with a CompletionException holding one of these exceptions as
2218 <     * its cause.
2213 >     * Returns a new CompletableFuture that is completed
2214 >     * when both this and the other given CompletableFuture complete,
2215 >     * after performing the given action.
2216 >     *
2217 >     * <p>If this and/or the other CompletableFuture complete
2218 >     * exceptionally, or the supplied action throws an exception,
2219 >     * then the returned CompletableFuture completes exceptionally
2220 >     * with a CompletionException holding the exception as its cause.
2221       *
2222       * @param other the other CompletableFuture
2223       * @param action the action to perform before completing the
# Line 2002 | Line 2226 | public class CompletableFuture<T> implem
2226       */
2227      public CompletableFuture<Void> runAfterBoth(CompletableFuture<?> other,
2228                                                  Runnable action) {
2229 <        return doThenBiRun(other, action, null);
2229 >        return doRunAfterBoth(other, action, null);
2230      }
2231  
2232      /**
2233 <     * Creates and returns a CompletableFuture that is completed
2234 <     * asynchronously using the {@link ForkJoinPool#commonPool()}
2235 <     * when this and the other given CompletableFuture both
2236 <     * complete.  If this and/or the other CompletableFuture complete
2237 <     * exceptionally, then the returned CompletableFuture also does
2238 <     * so, with a CompletionException holding one of these exceptions as
2239 <     * its cause.
2233 >     * Returns a new CompletableFuture that is asynchronously completed
2234 >     * when both this and the other given CompletableFuture complete,
2235 >     * after performing the given action from a task running in the
2236 >     * {@link ForkJoinPool#commonPool()}.
2237 >     *
2238 >     * <p>If this and/or the other CompletableFuture complete
2239 >     * exceptionally, or the supplied action throws an exception,
2240 >     * then the returned CompletableFuture completes exceptionally
2241 >     * with a CompletionException holding the exception as its cause.
2242       *
2243       * @param other the other CompletableFuture
2244       * @param action the action to perform before completing the
# Line 2021 | Line 2247 | public class CompletableFuture<T> implem
2247       */
2248      public CompletableFuture<Void> runAfterBothAsync(CompletableFuture<?> other,
2249                                                       Runnable action) {
2250 <        return doThenBiRun(other, action, ForkJoinPool.commonPool());
2250 >        return doRunAfterBoth(other, action, ForkJoinPool.commonPool());
2251      }
2252  
2253      /**
2254 <     * Creates and returns a CompletableFuture that is completed
2255 <     * asynchronously using the given executor
2256 <     * when this and the other given CompletableFuture both
2257 <     * complete.  If this and/or the other CompletableFuture complete
2258 <     * exceptionally, then the returned CompletableFuture also does
2259 <     * so, with a CompletionException holding one of these exceptions as
2260 <     * its cause.
2254 >     * Returns a new CompletableFuture that is asynchronously completed
2255 >     * when both this and the other given CompletableFuture complete,
2256 >     * after performing the given action from a task running in the
2257 >     * given executor.
2258 >     *
2259 >     * <p>If this and/or the other CompletableFuture complete
2260 >     * exceptionally, or the supplied action throws an exception,
2261 >     * then the returned CompletableFuture completes exceptionally
2262 >     * with a CompletionException holding the exception as its cause.
2263       *
2264       * @param other the other CompletableFuture
2265       * @param action the action to perform before completing the
# Line 2043 | Line 2271 | public class CompletableFuture<T> implem
2271                                                       Runnable action,
2272                                                       Executor executor) {
2273          if (executor == null) throw new NullPointerException();
2274 <        return doThenBiRun(other, action, executor);
2274 >        return doRunAfterBoth(other, action, executor);
2275      }
2276  
2277 <    private CompletableFuture<Void> doThenBiRun(CompletableFuture<?> other,
2278 <                                                Runnable action,
2279 <                                                Executor e) {
2277 >    private CompletableFuture<Void> doRunAfterBoth(CompletableFuture<?> other,
2278 >                                                   Runnable action,
2279 >                                                   Executor e) {
2280          if (other == null || action == null) throw new NullPointerException();
2281          CompletableFuture<Void> dst = new CompletableFuture<Void>();
2282 <        BiRunCompletion<T> d = null;
2282 >        RunAfterBoth d = null;
2283          Object r, s = null;
2284          if ((r = result) == null || (s = other.result) == null) {
2285 <            d = new BiRunCompletion<T>(this, other, action, dst, e);
2285 >            d = new RunAfterBoth(this, other, action, dst, e);
2286              CompletionNode q = null, p = new CompletionNode(d);
2287              while ((r == null && (r = result) == null) ||
2288                     (s == null && (s = other.result) == null)) {
# Line 2100 | Line 2328 | public class CompletableFuture<T> implem
2328      }
2329  
2330      /**
2331 <     * Creates and returns a CompletableFuture that is completed with
2332 <     * the result of the given function of either this or the other
2333 <     * given CompletableFuture's results when either complete.  If
2334 <     * this and/or the other CompletableFuture complete exceptionally,
2335 <     * then the returned CompletableFuture may also do so, with a
2336 <     * CompletionException holding one of these exceptions as its cause.
2337 <     * No guarantees are made about which result or exception is used
2338 <     * in the returned CompletableFuture.
2331 >     * Returns a new CompletableFuture that is completed
2332 >     * when either this or the other given CompletableFuture completes,
2333 >     * with the result of the given function of either this or the other
2334 >     * CompletableFuture's result.
2335 >     *
2336 >     * <p>If this and/or the other CompletableFuture complete
2337 >     * exceptionally, then the returned CompletableFuture may also do so,
2338 >     * with a CompletionException holding one of these exceptions as its
2339 >     * cause.  No guarantees are made about which result or exception is
2340 >     * used in the returned CompletableFuture.  If the supplied function
2341 >     * throws an exception, then the returned CompletableFuture completes
2342 >     * exceptionally with a CompletionException holding the exception as
2343 >     * its cause.
2344       *
2345       * @param other the other CompletableFuture
2346       * @param fn the function to use to compute the value of
2347       * the returned CompletableFuture
2348       * @return the new CompletableFuture
2349       */
2350 <    public <U> CompletableFuture<U> applyToEither(CompletableFuture<? extends T> other,
2351 <                                                  Fun<? super T, U> fn) {
2352 <        return doOrApply(other, fn, null);
2350 >    public <U> CompletableFuture<U> applyToEither
2351 >        (CompletableFuture<? extends T> other,
2352 >         Fun<? super T, U> fn) {
2353 >        return doApplyToEither(other, fn, null);
2354      }
2355  
2356      /**
2357 <     * Creates and returns a CompletableFuture that is completed
2358 <     * asynchronously using the {@link ForkJoinPool#commonPool()} with
2359 <     * the result of the given function of either this or the other
2360 <     * given CompletableFuture's results when either complete.  If
2361 <     * this and/or the other CompletableFuture complete exceptionally,
2362 <     * then the returned CompletableFuture may also do so, with a
2363 <     * CompletionException holding one of these exceptions as its cause.
2364 <     * No guarantees are made about which result or exception is used
2365 <     * in the returned CompletableFuture.
2357 >     * Returns a new CompletableFuture that is asynchronously completed
2358 >     * when either this or the other given CompletableFuture completes,
2359 >     * with the result of the given function of either this or the other
2360 >     * CompletableFuture's result from a task running in the
2361 >     * {@link ForkJoinPool#commonPool()}.
2362 >     *
2363 >     * <p>If this and/or the other CompletableFuture complete
2364 >     * exceptionally, then the returned CompletableFuture may also do so,
2365 >     * with a CompletionException holding one of these exceptions as its
2366 >     * cause.  No guarantees are made about which result or exception is
2367 >     * used in the returned CompletableFuture.  If the supplied function
2368 >     * throws an exception, then the returned CompletableFuture completes
2369 >     * exceptionally with a CompletionException holding the exception as
2370 >     * its cause.
2371       *
2372       * @param other the other CompletableFuture
2373       * @param fn the function to use to compute the value of
2374       * the returned CompletableFuture
2375       * @return the new CompletableFuture
2376       */
2377 <    public <U> CompletableFuture<U> applyToEitherAsync(CompletableFuture<? extends T> other,
2378 <                                                       Fun<? super T, U> fn) {
2379 <        return doOrApply(other, fn, ForkJoinPool.commonPool());
2377 >    public <U> CompletableFuture<U> applyToEitherAsync
2378 >        (CompletableFuture<? extends T> other,
2379 >         Fun<? super T, U> fn) {
2380 >        return doApplyToEither(other, fn, ForkJoinPool.commonPool());
2381      }
2382  
2383      /**
2384 <     * Creates and returns a CompletableFuture that is completed
2385 <     * asynchronously using the given executor with the result of the
2386 <     * given function of either this or the other given
2387 <     * CompletableFuture's results when either complete.  If this
2388 <     * and/or the other CompletableFuture complete exceptionally, then
2389 <     * the returned CompletableFuture may also do so, with a
2390 <     * CompletionException holding one of these exceptions as its cause.
2391 <     * No guarantees are made about which result or exception is used
2392 <     * in the returned CompletableFuture.
2384 >     * Returns a new CompletableFuture that is asynchronously completed
2385 >     * when either this or the other given CompletableFuture completes,
2386 >     * with the result of the given function of either this or the other
2387 >     * CompletableFuture's result from a task running in the
2388 >     * given executor.
2389 >     *
2390 >     * <p>If this and/or the other CompletableFuture complete
2391 >     * exceptionally, then the returned CompletableFuture may also do so,
2392 >     * with a CompletionException holding one of these exceptions as its
2393 >     * cause.  No guarantees are made about which result or exception is
2394 >     * used in the returned CompletableFuture.  If the supplied function
2395 >     * throws an exception, then the returned CompletableFuture completes
2396 >     * exceptionally with a CompletionException holding the exception as
2397 >     * its cause.
2398       *
2399       * @param other the other CompletableFuture
2400       * @param fn the function to use to compute the value of
# Line 2157 | Line 2402 | public class CompletableFuture<T> implem
2402       * @param executor the executor to use for asynchronous execution
2403       * @return the new CompletableFuture
2404       */
2405 <    public <U> CompletableFuture<U> applyToEitherAsync(CompletableFuture<? extends T> other,
2406 <                                                       Fun<? super T, U> fn,
2407 <                                                       Executor executor) {
2405 >    public <U> CompletableFuture<U> applyToEitherAsync
2406 >        (CompletableFuture<? extends T> other,
2407 >         Fun<? super T, U> fn,
2408 >         Executor executor) {
2409          if (executor == null) throw new NullPointerException();
2410 <        return doOrApply(other, fn, executor);
2410 >        return doApplyToEither(other, fn, executor);
2411      }
2412  
2413 <    private <U> CompletableFuture<U> doOrApply(CompletableFuture<? extends T> other,
2414 <                                               Fun<? super T, U> fn,
2415 <                                               Executor e) {
2413 >    private <U> CompletableFuture<U> doApplyToEither
2414 >        (CompletableFuture<? extends T> other,
2415 >         Fun<? super T, U> fn,
2416 >         Executor e) {
2417          if (other == null || fn == null) throw new NullPointerException();
2418          CompletableFuture<U> dst = new CompletableFuture<U>();
2419 <        OrApplyCompletion<T,U> d = null;
2419 >        ApplyToEither<T,U> d = null;
2420          Object r;
2421          if ((r = result) == null && (r = other.result) == null) {
2422 <            d = new OrApplyCompletion<T,U>(this, other, fn, dst, e);
2422 >            d = new ApplyToEither<T,U>(this, other, fn, dst, e);
2423              CompletionNode q = null, p = new CompletionNode(d);
2424              while ((r = result) == null && (r = other.result) == null) {
2425                  if (q != null) {
# Line 2216 | Line 2463 | public class CompletableFuture<T> implem
2463      }
2464  
2465      /**
2466 <     * Creates and returns a CompletableFuture that is completed after
2467 <     * performing the given action with the result of either this or the
2468 <     * other given CompletableFuture's result, when either complete.
2469 <     * If this and/or the other CompletableFuture complete
2470 <     * exceptionally, then the returned CompletableFuture may also do
2471 <     * so, with a CompletionException holding one of these exceptions as
2472 <     * its cause.  No guarantees are made about which exception is
2473 <     * used in the returned CompletableFuture.
2466 >     * Returns a new CompletableFuture that is completed
2467 >     * when either this or the other given CompletableFuture completes,
2468 >     * after performing the given action with the result of either this
2469 >     * or the other CompletableFuture's result.
2470 >     *
2471 >     * <p>If this and/or the other CompletableFuture complete
2472 >     * exceptionally, then the returned CompletableFuture may also do so,
2473 >     * with a CompletionException holding one of these exceptions as its
2474 >     * cause.  No guarantees are made about which result or exception is
2475 >     * used in the returned CompletableFuture.  If the supplied action
2476 >     * throws an exception, then the returned CompletableFuture completes
2477 >     * exceptionally with a CompletionException holding the exception as
2478 >     * its cause.
2479       *
2480       * @param other the other CompletableFuture
2481       * @param block the action to perform before completing the
2482       * returned CompletableFuture
2483       * @return the new CompletableFuture
2484       */
2485 <    public CompletableFuture<Void> acceptEither(CompletableFuture<? extends T> other,
2486 <                                                Action<? super T> block) {
2487 <        return doOrAccept(other, block, null);
2485 >    public CompletableFuture<Void> acceptEither
2486 >        (CompletableFuture<? extends T> other,
2487 >         Action<? super T> block) {
2488 >        return doAcceptEither(other, block, null);
2489      }
2490  
2491      /**
2492 <     * Creates and returns a CompletableFuture that is completed
2493 <     * asynchronously using the {@link ForkJoinPool#commonPool()},
2494 <     * performing the given action with the result of either this or
2495 <     * the other given CompletableFuture's result, when either
2496 <     * complete.  If this and/or the other CompletableFuture complete
2497 <     * exceptionally, then the returned CompletableFuture may also do
2498 <     * so, with a CompletionException holding one of these exceptions as
2499 <     * its cause.  No guarantees are made about which exception is
2500 <     * used in the returned CompletableFuture.
2492 >     * Returns a new CompletableFuture that is asynchronously completed
2493 >     * when either this or the other given CompletableFuture completes,
2494 >     * after performing the given action with the result of either this
2495 >     * or the other CompletableFuture's result from a task running in
2496 >     * the {@link ForkJoinPool#commonPool()}.
2497 >     *
2498 >     * <p>If this and/or the other CompletableFuture complete
2499 >     * exceptionally, then the returned CompletableFuture may also do so,
2500 >     * with a CompletionException holding one of these exceptions as its
2501 >     * cause.  No guarantees are made about which result or exception is
2502 >     * used in the returned CompletableFuture.  If the supplied action
2503 >     * throws an exception, then the returned CompletableFuture completes
2504 >     * exceptionally with a CompletionException holding the exception as
2505 >     * its cause.
2506       *
2507       * @param other the other CompletableFuture
2508       * @param block the action to perform before completing the
2509       * returned CompletableFuture
2510       * @return the new CompletableFuture
2511       */
2512 <    public CompletableFuture<Void> acceptEitherAsync(CompletableFuture<? extends T> other,
2513 <                                                     Action<? super T> block) {
2514 <        return doOrAccept(other, block, ForkJoinPool.commonPool());
2512 >    public CompletableFuture<Void> acceptEitherAsync
2513 >        (CompletableFuture<? extends T> other,
2514 >         Action<? super T> block) {
2515 >        return doAcceptEither(other, block, ForkJoinPool.commonPool());
2516      }
2517  
2518      /**
2519 <     * Creates and returns a CompletableFuture that is completed
2520 <     * asynchronously using the given executor,
2521 <     * performing the given action with the result of either this or
2522 <     * the other given CompletableFuture's result, when either
2523 <     * complete.  If this and/or the other CompletableFuture complete
2524 <     * exceptionally, then the returned CompletableFuture may also do
2525 <     * so, with a CompletionException holding one of these exceptions as
2526 <     * its cause.  No guarantees are made about which exception is
2527 <     * used in the returned CompletableFuture.
2519 >     * Returns a new CompletableFuture that is asynchronously completed
2520 >     * when either this or the other given CompletableFuture completes,
2521 >     * after performing the given action with the result of either this
2522 >     * or the other CompletableFuture's result from a task running in
2523 >     * the given executor.
2524 >     *
2525 >     * <p>If this and/or the other CompletableFuture complete
2526 >     * exceptionally, then the returned CompletableFuture may also do so,
2527 >     * with a CompletionException holding one of these exceptions as its
2528 >     * cause.  No guarantees are made about which result or exception is
2529 >     * used in the returned CompletableFuture.  If the supplied action
2530 >     * throws an exception, then the returned CompletableFuture completes
2531 >     * exceptionally with a CompletionException holding the exception as
2532 >     * its cause.
2533       *
2534       * @param other the other CompletableFuture
2535       * @param block the action to perform before completing the
# Line 2273 | Line 2537 | public class CompletableFuture<T> implem
2537       * @param executor the executor to use for asynchronous execution
2538       * @return the new CompletableFuture
2539       */
2540 <    public CompletableFuture<Void> acceptEitherAsync(CompletableFuture<? extends T> other,
2541 <                                                     Action<? super T> block,
2542 <                                                     Executor executor) {
2540 >    public CompletableFuture<Void> acceptEitherAsync
2541 >        (CompletableFuture<? extends T> other,
2542 >         Action<? super T> block,
2543 >         Executor executor) {
2544          if (executor == null) throw new NullPointerException();
2545 <        return doOrAccept(other, block, executor);
2545 >        return doAcceptEither(other, block, executor);
2546      }
2547  
2548 <    private CompletableFuture<Void> doOrAccept(CompletableFuture<? extends T> other,
2549 <                                               Action<? super T> fn,
2550 <                                               Executor e) {
2548 >    private CompletableFuture<Void> doAcceptEither
2549 >        (CompletableFuture<? extends T> other,
2550 >         Action<? super T> fn,
2551 >         Executor e) {
2552          if (other == null || fn == null) throw new NullPointerException();
2553          CompletableFuture<Void> dst = new CompletableFuture<Void>();
2554 <        OrAcceptCompletion<T> d = null;
2554 >        AcceptEither<T> d = null;
2555          Object r;
2556          if ((r = result) == null && (r = other.result) == null) {
2557 <            d = new OrAcceptCompletion<T>(this, other, fn, dst, e);
2557 >            d = new AcceptEither<T>(this, other, fn, dst, e);
2558              CompletionNode q = null, p = new CompletionNode(d);
2559              while ((r = result) == null && (r = other.result) == null) {
2560                  if (q != null) {
# Line 2331 | Line 2597 | public class CompletableFuture<T> implem
2597      }
2598  
2599      /**
2600 <     * Creates and returns a CompletableFuture that is completed
2601 <     * after this or the other given CompletableFuture complete.  If
2602 <     * this and/or the other CompletableFuture complete exceptionally,
2603 <     * then the returned CompletableFuture may also do so, with a
2604 <     * CompletionException holding one of these exceptions as its cause.
2605 <     * No guarantees are made about which exception is used in the
2606 <     * returned CompletableFuture.
2600 >     * Returns a new CompletableFuture that is completed
2601 >     * when either this or the other given CompletableFuture completes,
2602 >     * after performing the given action.
2603 >     *
2604 >     * <p>If this and/or the other CompletableFuture complete
2605 >     * exceptionally, then the returned CompletableFuture may also do so,
2606 >     * with a CompletionException holding one of these exceptions as its
2607 >     * cause.  No guarantees are made about which result or exception is
2608 >     * used in the returned CompletableFuture.  If the supplied action
2609 >     * throws an exception, then the returned CompletableFuture completes
2610 >     * exceptionally with a CompletionException holding the exception as
2611 >     * its cause.
2612       *
2613       * @param other the other CompletableFuture
2614       * @param action the action to perform before completing the
# Line 2346 | Line 2617 | public class CompletableFuture<T> implem
2617       */
2618      public CompletableFuture<Void> runAfterEither(CompletableFuture<?> other,
2619                                                    Runnable action) {
2620 <        return doOrRun(other, action, null);
2620 >        return doRunAfterEither(other, action, null);
2621      }
2622  
2623      /**
2624 <     * Creates and returns a CompletableFuture that is completed
2625 <     * asynchronously using the {@link ForkJoinPool#commonPool()}
2626 <     * after this or the other given CompletableFuture complete.  If
2627 <     * this and/or the other CompletableFuture complete exceptionally,
2628 <     * then the returned CompletableFuture may also do so, with a
2629 <     * CompletionException holding one of these exceptions as its cause.
2630 <     * No guarantees are made about which exception is used in the
2631 <     * returned CompletableFuture.
2624 >     * Returns a new CompletableFuture that is asynchronously completed
2625 >     * when either this or the other given CompletableFuture completes,
2626 >     * after performing the given action from a task running in the
2627 >     * {@link ForkJoinPool#commonPool()}.
2628 >     *
2629 >     * <p>If this and/or the other CompletableFuture complete
2630 >     * exceptionally, then the returned CompletableFuture may also do so,
2631 >     * with a CompletionException holding one of these exceptions as its
2632 >     * cause.  No guarantees are made about which result or exception is
2633 >     * used in the returned CompletableFuture.  If the supplied action
2634 >     * throws an exception, then the returned CompletableFuture completes
2635 >     * exceptionally with a CompletionException holding the exception as
2636 >     * its cause.
2637       *
2638       * @param other the other CompletableFuture
2639       * @param action the action to perform before completing the
2640       * returned CompletableFuture
2641       * @return the new CompletableFuture
2642       */
2643 <    public CompletableFuture<Void> runAfterEitherAsync(CompletableFuture<?> other,
2644 <                                                       Runnable action) {
2645 <        return doOrRun(other, action, ForkJoinPool.commonPool());
2643 >    public CompletableFuture<Void> runAfterEitherAsync
2644 >        (CompletableFuture<?> other,
2645 >         Runnable action) {
2646 >        return doRunAfterEither(other, action, ForkJoinPool.commonPool());
2647      }
2648  
2649      /**
2650 <     * Creates and returns a CompletableFuture that is completed
2651 <     * asynchronously using the given executor after this or the other
2652 <     * given CompletableFuture complete.  If this and/or the other
2653 <     * CompletableFuture complete exceptionally, then the returned
2654 <     * CompletableFuture may also do so, with a CompletionException
2655 <     * holding one of these exceptions as its cause.  No guarantees are
2656 <     * made about which exception is used in the returned
2657 <     * CompletableFuture.
2650 >     * Returns a new CompletableFuture that is asynchronously completed
2651 >     * when either this or the other given CompletableFuture completes,
2652 >     * after performing the given action from a task running in the
2653 >     * given executor.
2654 >     *
2655 >     * <p>If this and/or the other CompletableFuture complete
2656 >     * exceptionally, then the returned CompletableFuture may also do so,
2657 >     * with a CompletionException holding one of these exceptions as its
2658 >     * cause.  No guarantees are made about which result or exception is
2659 >     * used in the returned CompletableFuture.  If the supplied action
2660 >     * throws an exception, then the returned CompletableFuture completes
2661 >     * exceptionally with a CompletionException holding the exception as
2662 >     * its cause.
2663       *
2664       * @param other the other CompletableFuture
2665       * @param action the action to perform before completing the
# Line 2385 | Line 2667 | public class CompletableFuture<T> implem
2667       * @param executor the executor to use for asynchronous execution
2668       * @return the new CompletableFuture
2669       */
2670 <    public CompletableFuture<Void> runAfterEitherAsync(CompletableFuture<?> other,
2671 <                                                       Runnable action,
2672 <                                                       Executor executor) {
2670 >    public CompletableFuture<Void> runAfterEitherAsync
2671 >        (CompletableFuture<?> other,
2672 >         Runnable action,
2673 >         Executor executor) {
2674          if (executor == null) throw new NullPointerException();
2675 <        return doOrRun(other, action, executor);
2675 >        return doRunAfterEither(other, action, executor);
2676      }
2677  
2678 <    private CompletableFuture<Void> doOrRun(CompletableFuture<?> other,
2679 <                                            Runnable action,
2680 <                                            Executor e) {
2678 >    private CompletableFuture<Void> doRunAfterEither
2679 >        (CompletableFuture<?> other,
2680 >         Runnable action,
2681 >         Executor e) {
2682          if (other == null || action == null) throw new NullPointerException();
2683          CompletableFuture<Void> dst = new CompletableFuture<Void>();
2684 <        OrRunCompletion<T> d = null;
2684 >        RunAfterEither d = null;
2685          Object r;
2686          if ((r = result) == null && (r = other.result) == null) {
2687 <            d = new OrRunCompletion<T>(this, other, action, dst, e);
2687 >            d = new RunAfterEither(this, other, action, dst, e);
2688              CompletionNode q = null, p = new CompletionNode(d);
2689              while ((r = result) == null && (r = other.result) == null) {
2690                  if (q != null) {
# Line 2438 | Line 2722 | public class CompletableFuture<T> implem
2722      }
2723  
2724      /**
2725 <     * Returns a CompletableFuture (or an equivalent one) produced by
2726 <     * the given function of the result of this CompletableFuture when
2727 <     * completed.  If this CompletableFuture completes exceptionally,
2728 <     * then the returned CompletableFuture also does so, with a
2725 >     * Returns a CompletableFuture that upon completion, has the same
2726 >     * value as produced by the given function of the result of this
2727 >     * CompletableFuture.
2728 >     *
2729 >     * <p>If this CompletableFuture completes exceptionally, then the
2730 >     * returned CompletableFuture also does so, with a
2731       * CompletionException holding this exception as its cause.
2732 +     * Similarly, if the computed CompletableFuture completes
2733 +     * exceptionally, then so does the returned CompletableFuture.
2734       *
2735       * @param fn the function returning a new CompletableFuture
2736 <     * @return the CompletableFuture, that {@code isDone()} upon
2737 <     * return if completed by the given function, or an exception
2738 <     * occurs
2736 >     * @return the CompletableFuture
2737 >     */
2738 >    public <U> CompletableFuture<U> thenCompose
2739 >        (Fun<? super T, CompletableFuture<U>> fn) {
2740 >        return doThenCompose(fn, null);
2741 >    }
2742 >
2743 >    /**
2744 >     * Returns a CompletableFuture that upon completion, has the same
2745 >     * value as that produced asynchronously using the {@link
2746 >     * ForkJoinPool#commonPool()} by the given function of the result
2747 >     * of this CompletableFuture.
2748 >     *
2749 >     * <p>If this CompletableFuture completes exceptionally, then the
2750 >     * returned CompletableFuture also does so, with a
2751 >     * CompletionException holding this exception as its cause.
2752 >     * Similarly, if the computed CompletableFuture completes
2753 >     * exceptionally, then so does the returned CompletableFuture.
2754 >     *
2755 >     * @param fn the function returning a new CompletableFuture
2756 >     * @return the CompletableFuture
2757       */
2758 <    public <U> CompletableFuture<U> thenCompose(Fun<? super T,
2759 <                                                CompletableFuture<U>> fn) {
2758 >    public <U> CompletableFuture<U> thenComposeAsync
2759 >        (Fun<? super T, CompletableFuture<U>> fn) {
2760 >        return doThenCompose(fn, ForkJoinPool.commonPool());
2761 >    }
2762 >
2763 >    /**
2764 >     * Returns a CompletableFuture that upon completion, has the same
2765 >     * value as that produced asynchronously using the given executor
2766 >     * by the given function of this CompletableFuture.
2767 >     *
2768 >     * <p>If this CompletableFuture completes exceptionally, then the
2769 >     * returned CompletableFuture also does so, with a
2770 >     * CompletionException holding this exception as its cause.
2771 >     * Similarly, if the computed CompletableFuture completes
2772 >     * exceptionally, then so does the returned CompletableFuture.
2773 >     *
2774 >     * @param fn the function returning a new CompletableFuture
2775 >     * @param executor the executor to use for asynchronous execution
2776 >     * @return the CompletableFuture
2777 >     */
2778 >    public <U> CompletableFuture<U> thenComposeAsync
2779 >        (Fun<? super T, CompletableFuture<U>> fn,
2780 >         Executor executor) {
2781 >        if (executor == null) throw new NullPointerException();
2782 >        return doThenCompose(fn, executor);
2783 >    }
2784 >
2785 >    private <U> CompletableFuture<U> doThenCompose
2786 >        (Fun<? super T, CompletableFuture<U>> fn,
2787 >         Executor e) {
2788          if (fn == null) throw new NullPointerException();
2789          CompletableFuture<U> dst = null;
2790 <        ComposeCompletion<T,U> d = null;
2790 >        ThenCompose<T,U> d = null;
2791          Object r;
2792          if ((r = result) == null) {
2793              dst = new CompletableFuture<U>();
2794              CompletionNode p = new CompletionNode
2795 <                (d = new ComposeCompletion<T,U>(this, fn, dst));
2795 >                (d = new ThenCompose<T,U>(this, fn, dst, e));
2796              while ((r = result) == null) {
2797                  if (UNSAFE.compareAndSwapObject
2798                      (this, COMPLETIONS, p.next = completions, p))
# Line 2477 | Line 2811 | public class CompletableFuture<T> implem
2811                  t = tr;
2812              }
2813              if (ex == null) {
2814 <                try {
2815 <                    dst = fn.apply(t);
2816 <                } catch (Throwable rex) {
2817 <                    ex = rex;
2814 >                if (e != null) {
2815 >                    if (dst == null)
2816 >                        dst = new CompletableFuture<U>();
2817 >                    e.execute(new AsyncCompose<T,U>(t, fn, dst));
2818 >                }
2819 >                else {
2820 >                    try {
2821 >                        if ((dst = fn.apply(t)) == null)
2822 >                            ex = new NullPointerException();
2823 >                    } catch (Throwable rex) {
2824 >                        ex = rex;
2825 >                    }
2826                  }
2827              }
2828 <            if (dst == null) {
2828 >            if (dst == null)
2829                  dst = new CompletableFuture<U>();
2830 <                if (ex == null)
2489 <                    ex = new NullPointerException();
2490 <            }
2491 <            if (ex != null)
2830 >            if (e == null || ex != null)
2831                  dst.internalComplete(null, ex);
2832          }
2833          helpPostComplete();
# Line 2497 | Line 2836 | public class CompletableFuture<T> implem
2836      }
2837  
2838      /**
2839 <     * Creates and returns a CompletableFuture that is completed with
2840 <     * the result of the given function of the exception triggering
2841 <     * this CompletableFuture's completion when it completes
2842 <     * exceptionally; Otherwise, if this CompletableFuture completes
2843 <     * normally, then the returned CompletableFuture also completes
2844 <     * normally with the same value.
2839 >     * Returns a new CompletableFuture that is completed when this
2840 >     * CompletableFuture completes, with the result of the given
2841 >     * function of the exception triggering this CompletableFuture's
2842 >     * completion when it completes exceptionally; otherwise, if this
2843 >     * CompletableFuture completes normally, then the returned
2844 >     * CompletableFuture also completes normally with the same value.
2845       *
2846       * @param fn the function to use to compute the value of the
2847       * returned CompletableFuture if this CompletableFuture completed
2848       * exceptionally
2849       * @return the new CompletableFuture
2850       */
2851 <    public CompletableFuture<T> exceptionally(Fun<Throwable, ? extends T> fn) {
2851 >    public CompletableFuture<T> exceptionally
2852 >        (Fun<Throwable, ? extends T> fn) {
2853          if (fn == null) throw new NullPointerException();
2854          CompletableFuture<T> dst = new CompletableFuture<T>();
2855          ExceptionCompletion<T> d = null;
# Line 2526 | Line 2866 | public class CompletableFuture<T> implem
2866          if (r != null && (d == null || d.compareAndSet(0, 1))) {
2867              T t = null; Throwable ex, dx = null;
2868              if (r instanceof AltResult) {
2869 <                if ((ex = ((AltResult)r).ex) != null)  {
2869 >                if ((ex = ((AltResult)r).ex) != null) {
2870                      try {
2871                          t = fn.apply(ex);
2872                      } catch (Throwable rex) {
# Line 2545 | Line 2885 | public class CompletableFuture<T> implem
2885      }
2886  
2887      /**
2888 <     * Creates and returns a CompletableFuture that is completed with
2889 <     * the result of the given function of the result and exception of
2890 <     * this CompletableFuture's completion when it completes.  The
2891 <     * given function is invoked with the result (or {@code null} if
2892 <     * none) and the exception (or {@code null} if none) of this
2893 <     * CompletableFuture when complete.
2888 >     * Returns a new CompletableFuture that is completed when this
2889 >     * CompletableFuture completes, with the result of the given
2890 >     * function of the result and exception of this CompletableFuture's
2891 >     * completion.  The given function is invoked with the result (or
2892 >     * {@code null} if none) and the exception (or {@code null} if none)
2893 >     * of this CompletableFuture when complete.
2894       *
2895       * @param fn the function to use to compute the value of the
2896       * returned CompletableFuture
2897       * @return the new CompletableFuture
2898       */
2899 <    public <U> CompletableFuture<U> handle(BiFun<? super T, Throwable, ? extends U> fn) {
2899 >    public <U> CompletableFuture<U> handle
2900 >        (BiFun<? super T, Throwable, ? extends U> fn) {
2901          if (fn == null) throw new NullPointerException();
2902          CompletableFuture<U> dst = new CompletableFuture<U>();
2903          HandleCompletion<T,U> d = null;
# Line 2595 | Line 2936 | public class CompletableFuture<T> implem
2936          return dst;
2937      }
2938  
2939 +
2940 +    /* ------------- Arbitrary-arity constructions -------------- */
2941 +
2942 +    /*
2943 +     * The basic plan of attack is to recursively form binary
2944 +     * completion trees of elements. This can be overkill for small
2945 +     * sets, but scales nicely. The And/All vs Or/Any forms use the
2946 +     * same idea, but details differ.
2947 +     */
2948 +
2949 +    /**
2950 +     * Returns a new CompletableFuture that is completed when all of
2951 +     * the given CompletableFutures complete.  If any of the given
2952 +     * CompletableFutures complete exceptionally, then the returned
2953 +     * CompletableFuture also does so, with a CompletionException
2954 +     * holding this exception as its cause.  Otherwise, the results,
2955 +     * if any, of the given CompletableFutures are not reflected in
2956 +     * the returned CompletableFuture, but may be obtained by
2957 +     * inspecting them individually. If no CompletableFutures are
2958 +     * provided, returns a CompletableFuture completed with the value
2959 +     * {@code null}.
2960 +     *
2961 +     * <p>Among the applications of this method is to await completion
2962 +     * of a set of independent CompletableFutures before continuing a
2963 +     * program, as in: {@code CompletableFuture.allOf(c1, c2,
2964 +     * c3).join();}.
2965 +     *
2966 +     * @param cfs the CompletableFutures
2967 +     * @return a new CompletableFuture that is completed when all of the
2968 +     * given CompletableFutures complete
2969 +     * @throws NullPointerException if the array or any of its elements are
2970 +     * {@code null}
2971 +     */
2972 +    public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs) {
2973 +        int len = cfs.length; // Directly handle empty and singleton cases
2974 +        if (len > 1)
2975 +            return allTree(cfs, 0, len - 1);
2976 +        else {
2977 +            CompletableFuture<Void> dst = new CompletableFuture<Void>();
2978 +            CompletableFuture<?> f;
2979 +            if (len == 0)
2980 +                dst.result = NIL;
2981 +            else if ((f = cfs[0]) == null)
2982 +                throw new NullPointerException();
2983 +            else {
2984 +                ThenPropagate d = null;
2985 +                CompletionNode p = null;
2986 +                Object r;
2987 +                while ((r = f.result) == null) {
2988 +                    if (d == null)
2989 +                        d = new ThenPropagate(f, dst);
2990 +                    else if (p == null)
2991 +                        p = new CompletionNode(d);
2992 +                    else if (UNSAFE.compareAndSwapObject
2993 +                             (f, COMPLETIONS, p.next = f.completions, p))
2994 +                        break;
2995 +                }
2996 +                if (r != null && (d == null || d.compareAndSet(0, 1)))
2997 +                    dst.internalComplete(null, (r instanceof AltResult) ?
2998 +                                         ((AltResult)r).ex : null);
2999 +                f.helpPostComplete();
3000 +            }
3001 +            return dst;
3002 +        }
3003 +    }
3004 +
3005 +    /**
3006 +     * Recursively constructs an And'ed tree of CompletableFutures.
3007 +     * Called only when array known to have at least two elements.
3008 +     */
3009 +    private static CompletableFuture<Void> allTree(CompletableFuture<?>[] cfs,
3010 +                                                   int lo, int hi) {
3011 +        CompletableFuture<?> fst, snd;
3012 +        int mid = (lo + hi) >>> 1;
3013 +        if ((fst = (lo == mid   ? cfs[lo] : allTree(cfs, lo,    mid))) == null ||
3014 +            (snd = (hi == mid+1 ? cfs[hi] : allTree(cfs, mid+1, hi))) == null)
3015 +            throw new NullPointerException();
3016 +        CompletableFuture<Void> dst = new CompletableFuture<Void>();
3017 +        AndCompletion d = null;
3018 +        CompletionNode p = null, q = null;
3019 +        Object r = null, s = null;
3020 +        while ((r = fst.result) == null || (s = snd.result) == null) {
3021 +            if (d == null)
3022 +                d = new AndCompletion(fst, snd, dst);
3023 +            else if (p == null)
3024 +                p = new CompletionNode(d);
3025 +            else if (q == null) {
3026 +                if (UNSAFE.compareAndSwapObject
3027 +                    (fst, COMPLETIONS, p.next = fst.completions, p))
3028 +                    q = new CompletionNode(d);
3029 +            }
3030 +            else if (UNSAFE.compareAndSwapObject
3031 +                     (snd, COMPLETIONS, q.next = snd.completions, q))
3032 +                break;
3033 +        }
3034 +        if ((r != null || (r = fst.result) != null) &&
3035 +            (s != null || (s = snd.result) != null) &&
3036 +            (d == null || d.compareAndSet(0, 1))) {
3037 +            Throwable ex;
3038 +            if (r instanceof AltResult)
3039 +                ex = ((AltResult)r).ex;
3040 +            else
3041 +                ex = null;
3042 +            if (ex == null && (s instanceof AltResult))
3043 +                ex = ((AltResult)s).ex;
3044 +            dst.internalComplete(null, ex);
3045 +        }
3046 +        fst.helpPostComplete();
3047 +        snd.helpPostComplete();
3048 +        return dst;
3049 +    }
3050 +
3051      /**
3052 <     * Attempts to complete this CompletableFuture with
3053 <     * a {@link CancellationException}.
3052 >     * Returns a new CompletableFuture that is completed when any of
3053 >     * the given CompletableFutures complete, with the same result.
3054 >     * Otherwise, if it completed exceptionally, the returned
3055 >     * CompletableFuture also does so, with a CompletionException
3056 >     * holding this exception as its cause.  If no CompletableFutures
3057 >     * are provided, returns an incomplete CompletableFuture.
3058 >     *
3059 >     * @param cfs the CompletableFutures
3060 >     * @return a new CompletableFuture that is completed with the
3061 >     * result or exception of any of the given CompletableFutures when
3062 >     * one completes
3063 >     * @throws NullPointerException if the array or any of its elements are
3064 >     * {@code null}
3065 >     */
3066 >    public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs) {
3067 >        int len = cfs.length; // Same idea as allOf
3068 >        if (len > 1)
3069 >            return anyTree(cfs, 0, len - 1);
3070 >        else {
3071 >            CompletableFuture<Object> dst = new CompletableFuture<Object>();
3072 >            CompletableFuture<?> f;
3073 >            if (len == 0)
3074 >                ; // skip
3075 >            else if ((f = cfs[0]) == null)
3076 >                throw new NullPointerException();
3077 >            else {
3078 >                ThenCopy<Object> d = null;
3079 >                CompletionNode p = null;
3080 >                Object r;
3081 >                while ((r = f.result) == null) {
3082 >                    if (d == null)
3083 >                        d = new ThenCopy<Object>(f, dst);
3084 >                    else if (p == null)
3085 >                        p = new CompletionNode(d);
3086 >                    else if (UNSAFE.compareAndSwapObject
3087 >                             (f, COMPLETIONS, p.next = f.completions, p))
3088 >                        break;
3089 >                }
3090 >                if (r != null && (d == null || d.compareAndSet(0, 1))) {
3091 >                    Throwable ex; Object t;
3092 >                    if (r instanceof AltResult) {
3093 >                        ex = ((AltResult)r).ex;
3094 >                        t = null;
3095 >                    }
3096 >                    else {
3097 >                        ex = null;
3098 >                        t = r;
3099 >                    }
3100 >                    dst.internalComplete(t, ex);
3101 >                }
3102 >                f.helpPostComplete();
3103 >            }
3104 >            return dst;
3105 >        }
3106 >    }
3107 >
3108 >    /**
3109 >     * Recursively constructs an Or'ed tree of CompletableFutures.
3110 >     */
3111 >    private static CompletableFuture<Object> anyTree(CompletableFuture<?>[] cfs,
3112 >                                                     int lo, int hi) {
3113 >        CompletableFuture<?> fst, snd;
3114 >        int mid = (lo + hi) >>> 1;
3115 >        if ((fst = (lo == mid   ? cfs[lo] : anyTree(cfs, lo,    mid))) == null ||
3116 >            (snd = (hi == mid+1 ? cfs[hi] : anyTree(cfs, mid+1, hi))) == null)
3117 >            throw new NullPointerException();
3118 >        CompletableFuture<Object> dst = new CompletableFuture<Object>();
3119 >        OrCompletion d = null;
3120 >        CompletionNode p = null, q = null;
3121 >        Object r;
3122 >        while ((r = fst.result) == null && (r = snd.result) == null) {
3123 >            if (d == null)
3124 >                d = new OrCompletion(fst, snd, dst);
3125 >            else if (p == null)
3126 >                p = new CompletionNode(d);
3127 >            else if (q == null) {
3128 >                if (UNSAFE.compareAndSwapObject
3129 >                    (fst, COMPLETIONS, p.next = fst.completions, p))
3130 >                    q = new CompletionNode(d);
3131 >            }
3132 >            else if (UNSAFE.compareAndSwapObject
3133 >                     (snd, COMPLETIONS, q.next = snd.completions, q))
3134 >                break;
3135 >        }
3136 >        if ((r != null || (r = fst.result) != null ||
3137 >             (r = snd.result) != null) &&
3138 >            (d == null || d.compareAndSet(0, 1))) {
3139 >            Throwable ex; Object t;
3140 >            if (r instanceof AltResult) {
3141 >                ex = ((AltResult)r).ex;
3142 >                t = null;
3143 >            }
3144 >            else {
3145 >                ex = null;
3146 >                t = r;
3147 >            }
3148 >            dst.internalComplete(t, ex);
3149 >        }
3150 >        fst.helpPostComplete();
3151 >        snd.helpPostComplete();
3152 >        return dst;
3153 >    }
3154 >
3155 >    /* ------------- Control and status methods -------------- */
3156 >
3157 >    /**
3158 >     * If not already completed, completes this CompletableFuture with
3159 >     * a {@link CancellationException}. Dependent CompletableFutures
3160 >     * that have not already completed will also complete
3161 >     * exceptionally, with a {@link CompletionException} caused by
3162 >     * this {@code CancellationException}.
3163       *
3164       * @param mayInterruptIfRunning this value has no effect in this
3165       * implementation because interrupts are not used to control
# Line 2606 | Line 3168 | public class CompletableFuture<T> implem
3168       * @return {@code true} if this task is now cancelled
3169       */
3170      public boolean cancel(boolean mayInterruptIfRunning) {
3171 <        Object r;
3172 <        while ((r = result) == null) {
3173 <            r = new AltResult(new CancellationException());
3174 <            if (UNSAFE.compareAndSwapObject(this, RESULT, null, r)) {
3175 <                postComplete();
2614 <                return true;
2615 <            }
2616 <        }
2617 <        return ((r instanceof AltResult) &&
2618 <                (((AltResult)r).ex instanceof CancellationException));
3171 >        boolean cancelled = (result == null) &&
3172 >            UNSAFE.compareAndSwapObject
3173 >            (this, RESULT, null, new AltResult(new CancellationException()));
3174 >        postComplete();
3175 >        return cancelled || isCancelled();
3176      }
3177  
3178      /**
# Line 2662 | Line 3219 | public class CompletableFuture<T> implem
3219          postComplete();
3220      }
3221  
3222 +    /**
3223 +     * Returns the estimated number of CompletableFutures whose
3224 +     * completions are awaiting completion of this CompletableFuture.
3225 +     * This method is designed for use in monitoring system state, not
3226 +     * for synchronization control.
3227 +     *
3228 +     * @return the number of dependent CompletableFutures
3229 +     */
3230 +    public int getNumberOfDependents() {
3231 +        int count = 0;
3232 +        for (CompletionNode p = completions; p != null; p = p.next)
3233 +            ++count;
3234 +        return count;
3235 +    }
3236 +
3237 +    /**
3238 +     * Returns a string identifying this CompletableFuture, as well as
3239 +     * its completion state.  The state, in brackets, contains the
3240 +     * String {@code "Completed Normally"} or the String {@code
3241 +     * "Completed Exceptionally"}, or the String {@code "Not
3242 +     * completed"} followed by the number of CompletableFutures
3243 +     * dependent upon its completion, if any.
3244 +     *
3245 +     * @return a string identifying this CompletableFuture, as well as its state
3246 +     */
3247 +    public String toString() {
3248 +        Object r = result;
3249 +        int count;
3250 +        return super.toString() +
3251 +            ((r == null) ?
3252 +             (((count = getNumberOfDependents()) == 0) ?
3253 +              "[Not completed]" :
3254 +              "[Not completed, " + count + " dependents]") :
3255 +             (((r instanceof AltResult) && ((AltResult)r).ex != null) ?
3256 +              "[Completed exceptionally]" :
3257 +              "[Completed normally]"));
3258 +    }
3259 +
3260      // Unsafe mechanics
3261      private static final sun.misc.Unsafe UNSAFE;
3262      private static final long RESULT;
# Line 2682 | Line 3277 | public class CompletableFuture<T> implem
3277          }
3278      }
3279  
2685
3280      /**
3281       * Returns a sun.misc.Unsafe.  Suitable for use in a 3rd party package.
3282       * Replace with a simple call to Unsafe.getUnsafe when integrating

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines