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.1 by dl, Wed Jan 2 14:13:12 2013 UTC vs.
Revision 1.20 by jsr166, Mon Jul 22 16:05:34 2013 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines