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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines