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

Comparing jsr166/src/main/java/util/concurrent/CompletableFuture.java (file contents):
Revision 1.87 by jsr166, Mon May 20 16:16:42 2013 UTC vs.
Revision 1.88 by dl, Mon Jul 1 18:47:56 2013 UTC

# Line 19 | Line 19 | import java.util.concurrent.ThreadLocalR
19   import java.util.concurrent.ExecutionException;
20   import java.util.concurrent.TimeoutException;
21   import java.util.concurrent.CancellationException;
22 + import java.util.concurrent.CompletionException;
23 + import java.util.concurrent.CompletionStage;
24   import java.util.concurrent.atomic.AtomicInteger;
25   import java.util.concurrent.locks.LockSupport;
26  
27   /**
28   * A {@link Future} that may be explicitly completed (setting its
29 < * value and status), and may include dependent functions and actions
30 < * that trigger upon its completion.
29 > * value and status), and may be used as a {@link CompletionStage},
30 > * supporting dependent functions and actions that trigger upon its
31 > * completion.
32   *
33   * <p>When two or more threads attempt to
34   * {@link #complete complete},
# Line 33 | Line 36 | import java.util.concurrent.locks.LockSu
36   * {@link #cancel cancel}
37   * a CompletableFuture, only one of them succeeds.
38   *
39 < * <p>Methods are available for adding dependents based on
40 < * user-provided Functions, Consumers, or Runnables. The appropriate
38 < * form to use depends on whether actions require arguments and/or
39 < * produce results.  Completion of a dependent action will trigger the
40 < * completion of another CompletableFuture.  Actions may also be
41 < * triggered after either or both the current and another
42 < * CompletableFuture complete.  Multiple CompletableFutures may also
43 < * be grouped as one using {@link #anyOf(CompletableFuture...)} and
44 < * {@link #allOf(CompletableFuture...)}.
39 > * <p>CompletableFuture implements {@link CompletionStage} with the
40 > * following policies: <ul>
41   *
42 < * <p>CompletableFutures themselves do not execute asynchronously.
43 < * However, actions supplied for dependent completions of another
44 < * CompletableFuture may do so, depending on whether they are provided
45 < * via one of the <em>async</em> methods (that is, methods with names
50 < * of the form <tt><var>xxx</var>Async</tt>).  The <em>async</em>
51 < * methods provide a way to commence asynchronous processing of an
52 < * action using either a given {@link Executor} or by default the
53 < * {@link ForkJoinPool#commonPool()}. To simplify monitoring,
54 < * debugging, and tracking, all generated asynchronous tasks are
55 < * instances of the marker interface {@link AsynchronousCompletionTask}.
42 > * <li>Actions supplied for dependent completions of
43 > * <em>non-async</em> methods may be performed by the thread that
44 > * completes the current CompletableFuture, or by any other caller of
45 > * these methods.</li>
46   *
47 < * <p>Actions supplied for dependent completions of <em>non-async</em>
48 < * methods may be performed by the thread that completes the current
49 < * CompletableFuture, or by any other caller of these methods.  There
50 < * are no guarantees about the order of processing completions unless
51 < * constrained by these methods.
47 > * <li>All <em>async</em> methods without an explicit Executor
48 > * argument are performed using the {@link
49 > * ForkJoinPool#commonPool()}. To simplify monitoring, debugging, and
50 > * tracking, all generated asynchronous tasks are instances of the
51 > * marker interface {@link AsynchronousCompletionTask}. </li>
52   *
53 < * <p>Since (unlike {@link FutureTask}) this class has no direct
54 < * control over the computation that causes it to be completed,
55 < * cancellation is treated as just another form of exceptional completion.
56 < * Method {@link #cancel cancel} has the same effect as
57 < * {@code completeExceptionally(new CancellationException())}.
53 > * <li>All CompletionStage methods are implemented independently of
54 > * other public methods, so the behavior of one method is not impacted
55 > * by overrides of others in subclasses.  </li> </ul>
56 > *
57 > * <p>CompletableFuture implements {@link Future} with the following
58 > * policies: <ul>
59   *
60 < * <p>Upon exceptional completion (including cancellation), or when a
61 < * completion entails an additional computation which terminates
62 < * abruptly with an (unchecked) exception or error, then all of their
63 < * dependent completions (and their dependents in turn) generally act
64 < * as {@code completeExceptionally} with a {@link CompletionException}
65 < * holding that exception as its cause.  However, the {@link
66 < * #exceptionally exceptionally} and {@link #handle handle}
76 < * completions <em>are</em> able to handle exceptional completions of
77 < * the CompletableFutures they depend on.
60 > * <li>Since (unlike {@link FutureTask}) this class has no direct
61 > * control over the computation that causes it to be completed,
62 > * cancellation is treated as just another form of exceptional
63 > * completion.  Method {@link #cancel cancel} has the same effect as
64 > * {@code completeExceptionally(new CancellationException())}. Method
65 > * {@link #isCompletedExceptionally} can be used to determine if a
66 > * CompletableFuture completed in any exceptional fashion.</li>
67   *
68 < * <p>In case of exceptional completion with a CompletionException,
68 > * <li>In case of exceptional completion with a CompletionException,
69   * methods {@link #get()} and {@link #get(long, TimeUnit)} throw an
70   * {@link ExecutionException} with the same cause as held in the
71 < * corresponding CompletionException.  However, in these cases,
72 < * methods {@link #join()} and {@link #getNow} throw the
73 < * CompletionException, which simplifies usage.
74 < *
86 < * <p>Arguments used to pass a completion result (that is, for parameters
87 < * of type {@code T}) may be null, but passing a null value for any other
88 < * parameter will result in a {@link NullPointerException} being thrown.
71 > * corresponding CompletionException.  To simplify usage in most
72 > * contexts, this class also defines methods {@link #join()} and
73 > * {@link #getNow} that instead throw the CompletionException directly
74 > * in these cases.</li> </ul>
75   *
76   * @author Doug Lea
77   * @since 1.8
78   */
79 < public class CompletableFuture<T> implements Future<T> {
79 > public class CompletableFuture<T> implements Future<T>, CompletionStage<T> {
80  
81      /*
82       * Overview:
# Line 509 | Line 495 | public class CompletableFuture<T> implem
495      static final class AsyncAccept<T> extends Async {
496          final T arg;
497          final Consumer<? super T> fn;
498 <        final CompletableFuture<Void> dst;
498 >        final CompletableFuture<?> dst;
499          AsyncAccept(T arg, Consumer<? super T> fn,
500 <                    CompletableFuture<Void> dst) {
500 >                    CompletableFuture<?> dst) {
501              this.arg = arg; this.fn = fn; this.dst = dst;
502          }
503          public final boolean exec() {
504 <            CompletableFuture<Void> d; Throwable ex;
504 >            CompletableFuture<?> d; Throwable ex;
505              if ((d = this.dst) != null && d.result == null) {
506                  try {
507                      fn.accept(arg);
# Line 534 | Line 520 | public class CompletableFuture<T> implem
520          final T arg1;
521          final U arg2;
522          final BiConsumer<? super T,? super U> fn;
523 <        final CompletableFuture<Void> dst;
523 >        final CompletableFuture<?> dst;
524          AsyncAcceptBoth(T arg1, U arg2,
525                          BiConsumer<? super T,? super U> fn,
526 <                        CompletableFuture<Void> dst) {
526 >                        CompletableFuture<?> dst) {
527              this.arg1 = arg1; this.arg2 = arg2; this.fn = fn; this.dst = dst;
528          }
529          public final boolean exec() {
530 <            CompletableFuture<Void> d; Throwable ex;
530 >            CompletableFuture<?> d; Throwable ex;
531              if ((d = this.dst) != null && d.result == null) {
532                  try {
533                      fn.accept(arg1, arg2);
# Line 558 | Line 544 | public class CompletableFuture<T> implem
544  
545      static final class AsyncCompose<T,U> extends Async {
546          final T arg;
547 <        final Function<? super T, CompletableFuture<U>> fn;
547 >        final Function<? super T, ? extends CompletionStage<U>> fn;
548          final CompletableFuture<U> dst;
549          AsyncCompose(T arg,
550 <                     Function<? super T, CompletableFuture<U>> fn,
550 >                     Function<? super T, ? extends CompletionStage<U>> fn,
551                       CompletableFuture<U> dst) {
552              this.arg = arg; this.fn = fn; this.dst = dst;
553          }
# Line 569 | Line 555 | public class CompletableFuture<T> implem
555              CompletableFuture<U> d, fr; U u; Throwable ex;
556              if ((d = this.dst) != null && d.result == null) {
557                  try {
558 <                    fr = fn.apply(arg);
558 >                    CompletionStage<U> cs = fn.apply(arg);
559 >                    fr = (cs == null) ? null : cs.toCompletableFuture();
560                      ex = (fr == null) ? new NullPointerException() : null;
561                  } catch (Throwable rex) {
562                      ex = rex;
# Line 668 | Line 655 | public class CompletableFuture<T> implem
655      static final class ThenAccept<T> extends Completion {
656          final CompletableFuture<? extends T> src;
657          final Consumer<? super T> fn;
658 <        final CompletableFuture<Void> dst;
658 >        final CompletableFuture<?> dst;
659          final Executor executor;
660          ThenAccept(CompletableFuture<? extends T> src,
661                     Consumer<? super T> fn,
662 <                   CompletableFuture<Void> dst,
662 >                   CompletableFuture<?> dst,
663                     Executor executor) {
664              this.src = src; this.fn = fn; this.dst = dst;
665              this.executor = executor;
# Line 680 | Line 667 | public class CompletableFuture<T> implem
667          public final void run() {
668              final CompletableFuture<? extends T> a;
669              final Consumer<? super T> fn;
670 <            final CompletableFuture<Void> dst;
670 >            final CompletableFuture<?> dst;
671              Object r; T t; Throwable ex;
672              if ((dst = this.dst) != null &&
673                  (fn = this.fn) != null &&
# Line 1197 | Line 1184 | public class CompletableFuture<T> implem
1184          private static final long serialVersionUID = 5232453952276885070L;
1185      }
1186  
1187 +    static final class WhenCompleteCompletion<T> extends Completion {
1188 +        final CompletableFuture<? extends T> src;
1189 +        final BiConsumer<? super T, ? super Throwable> fn;
1190 +        final CompletableFuture<T> dst;
1191 +        final Executor executor;
1192 +        WhenCompleteCompletion(CompletableFuture<? extends T> src,
1193 +                                  BiConsumer<? super T, ? super Throwable> fn,
1194 +                                  CompletableFuture<T> dst,
1195 +                                  Executor executor) {
1196 +            this.src = src; this.fn = fn; this.dst = dst;
1197 +            this.executor = executor;
1198 +        }
1199 +        public final void run() {
1200 +            final CompletableFuture<? extends T> a;
1201 +            final BiConsumer<? super T, ? super Throwable> fn;
1202 +            final CompletableFuture<T> dst;
1203 +            Object r; T t; Throwable ex;
1204 +            if ((dst = this.dst) != null &&
1205 +                (fn = this.fn) != null &&
1206 +                (a = this.src) != null &&
1207 +                (r = a.result) != null &&
1208 +                compareAndSet(0, 1)) {
1209 +                if (r instanceof AltResult) {
1210 +                    ex = ((AltResult)r).ex;
1211 +                    t = null;
1212 +                }
1213 +                else {
1214 +                    ex = null;
1215 +                    @SuppressWarnings("unchecked") T tr = (T) r;
1216 +                    t = tr;
1217 +                }
1218 +                Executor e = executor;
1219 +                Throwable dx = null;
1220 +                try {
1221 +                    if (e != null)
1222 +                        e.execute(new AsyncAcceptBoth<T,Throwable>(t, ex, fn, dst));
1223 +                    else
1224 +                        fn.accept(t, ex);
1225 +                } catch (Throwable rex) {
1226 +                    dx = rex;
1227 +                }
1228 +                if (e == null || dx != null)
1229 +                    dst.internalComplete(t, ex != null ? ex : dx);
1230 +            }
1231 +        }
1232 +        private static final long serialVersionUID = 5232453952276885070L;
1233 +    }
1234 +
1235      static final class ThenCopy<T> extends Completion {
1236          final CompletableFuture<?> src;
1237          final CompletableFuture<T> dst;
# Line 1257 | Line 1292 | public class CompletableFuture<T> implem
1292          final CompletableFuture<? extends T> src;
1293          final BiFunction<? super T, Throwable, ? extends U> fn;
1294          final CompletableFuture<U> dst;
1295 +        final Executor executor;
1296          HandleCompletion(CompletableFuture<? extends T> src,
1297                           BiFunction<? super T, Throwable, ? extends U> fn,
1298 <                         CompletableFuture<U> dst) {
1298 >                         CompletableFuture<U> dst,
1299 >                          Executor executor) {
1300              this.src = src; this.fn = fn; this.dst = dst;
1301 +            this.executor = executor;
1302          }
1303          public final void run() {
1304              final CompletableFuture<? extends T> a;
# Line 1281 | Line 1319 | public class CompletableFuture<T> implem
1319                      @SuppressWarnings("unchecked") T tr = (T) r;
1320                      t = tr;
1321                  }
1322 <                U u = null; Throwable dx = null;
1322 >                Executor e = executor;
1323 >                U u = null;
1324 >                Throwable dx = null;
1325                  try {
1326 <                    u = fn.apply(t, ex);
1326 >                    if (e != null)
1327 >                        e.execute(new AsyncCombine<T,Throwable,U>(t, ex, fn, dst));
1328 >                    else
1329 >                        u = fn.apply(t, ex);
1330                  } catch (Throwable rex) {
1331                      dx = rex;
1332                  }
1333 +                if (e == null || dx != null)
1334 +                    dst.internalComplete(u, dx);
1335                  dst.internalComplete(u, dx);
1336              }
1337          }
# Line 1295 | Line 1340 | public class CompletableFuture<T> implem
1340  
1341      static final class ThenCompose<T,U> extends Completion {
1342          final CompletableFuture<? extends T> src;
1343 <        final Function<? super T, CompletableFuture<U>> fn;
1343 >        final Function<? super T, ? extends CompletionStage<U>> fn;
1344          final CompletableFuture<U> dst;
1345          final Executor executor;
1346          ThenCompose(CompletableFuture<? extends T> src,
1347 <                    Function<? super T, CompletableFuture<U>> fn,
1347 >                    Function<? super T, ? extends CompletionStage<U>> fn,
1348                      CompletableFuture<U> dst,
1349                      Executor executor) {
1350              this.src = src; this.fn = fn; this.dst = dst;
# Line 1307 | Line 1352 | public class CompletableFuture<T> implem
1352          }
1353          public final void run() {
1354              final CompletableFuture<? extends T> a;
1355 <            final Function<? super T, CompletableFuture<U>> fn;
1355 >            final Function<? super T, ? extends CompletionStage<U>> fn;
1356              final CompletableFuture<U> dst;
1357              Object r; T t; Throwable ex; Executor e;
1358              if ((dst = this.dst) != null &&
# Line 1332 | Line 1377 | public class CompletableFuture<T> implem
1377                          e.execute(new AsyncCompose<T,U>(t, fn, dst));
1378                      else {
1379                          try {
1380 <                            if ((c = fn.apply(t)) == null)
1380 >                            CompletionStage<U> cs = fn.apply(t);
1381 >                            c = (cs == null) ? null : cs.toCompletableFuture();
1382 >                            if (c == null)
1383                                  ex = new NullPointerException();
1384                          } catch (Throwable rex) {
1385                              ex = rex;
# Line 1372 | Line 1419 | public class CompletableFuture<T> implem
1419          private static final long serialVersionUID = 5232453952276885070L;
1420      }
1421  
1422 <    // public methods
1376 <
1377 <    /**
1378 <     * Creates a new incomplete CompletableFuture.
1379 <     */
1380 <    public CompletableFuture() {
1381 <    }
1382 <
1383 <    /**
1384 <     * Returns a new CompletableFuture that is asynchronously completed
1385 <     * by a task running in the {@link ForkJoinPool#commonPool()} with
1386 <     * the value obtained by calling the given Supplier.
1387 <     *
1388 <     * @param supplier a function returning the value to be used
1389 <     * to complete the returned CompletableFuture
1390 <     * @return the new CompletableFuture
1391 <     */
1392 <    public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) {
1393 <        if (supplier == null) throw new NullPointerException();
1394 <        CompletableFuture<U> f = new CompletableFuture<U>();
1395 <        ForkJoinPool.commonPool().
1396 <            execute((ForkJoinTask<?>)new AsyncSupply<U>(supplier, f));
1397 <        return f;
1398 <    }
1399 <
1400 <    /**
1401 <     * Returns a new CompletableFuture that is asynchronously completed
1402 <     * by a task running in the given executor with the value obtained
1403 <     * by calling the given Supplier.
1404 <     *
1405 <     * @param supplier a function returning the value to be used
1406 <     * to complete the returned CompletableFuture
1407 <     * @param executor the executor to use for asynchronous execution
1408 <     * @return the new CompletableFuture
1409 <     */
1410 <    public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier,
1411 <                                                       Executor executor) {
1412 <        if (executor == null || supplier == null)
1413 <            throw new NullPointerException();
1414 <        CompletableFuture<U> f = new CompletableFuture<U>();
1415 <        executor.execute(new AsyncSupply<U>(supplier, f));
1416 <        return f;
1417 <    }
1418 <
1419 <    /**
1420 <     * Returns a new CompletableFuture that is asynchronously completed
1421 <     * by a task running in the {@link ForkJoinPool#commonPool()} after
1422 <     * it runs the given action.
1423 <     *
1424 <     * @param runnable the action to run before completing the
1425 <     * returned CompletableFuture
1426 <     * @return the new CompletableFuture
1427 <     */
1428 <    public static CompletableFuture<Void> runAsync(Runnable runnable) {
1429 <        if (runnable == null) throw new NullPointerException();
1430 <        CompletableFuture<Void> f = new CompletableFuture<Void>();
1431 <        ForkJoinPool.commonPool().
1432 <            execute((ForkJoinTask<?>)new AsyncRun(runnable, f));
1433 <        return f;
1434 <    }
1435 <
1436 <    /**
1437 <     * Returns a new CompletableFuture that is asynchronously completed
1438 <     * by a task running in the given executor after it runs the given
1439 <     * action.
1440 <     *
1441 <     * @param runnable the action to run before completing the
1442 <     * returned CompletableFuture
1443 <     * @param executor the executor to use for asynchronous execution
1444 <     * @return the new CompletableFuture
1445 <     */
1446 <    public static CompletableFuture<Void> runAsync(Runnable runnable,
1447 <                                                   Executor executor) {
1448 <        if (executor == null || runnable == null)
1449 <            throw new NullPointerException();
1450 <        CompletableFuture<Void> f = new CompletableFuture<Void>();
1451 <        executor.execute(new AsyncRun(runnable, f));
1452 <        return f;
1453 <    }
1454 <
1455 <    /**
1456 <     * Returns a new CompletableFuture that is already completed with
1457 <     * the given value.
1458 <     *
1459 <     * @param value the value
1460 <     * @return the completed CompletableFuture
1461 <     */
1462 <    public static <U> CompletableFuture<U> completedFuture(U value) {
1463 <        CompletableFuture<U> f = new CompletableFuture<U>();
1464 <        f.result = (value == null) ? NIL : value;
1465 <        return f;
1466 <    }
1467 <
1468 <    /**
1469 <     * Returns {@code true} if completed in any fashion: normally,
1470 <     * exceptionally, or via cancellation.
1471 <     *
1472 <     * @return {@code true} if completed
1473 <     */
1474 <    public boolean isDone() {
1475 <        return result != null;
1476 <    }
1477 <
1478 <    /**
1479 <     * Waits if necessary for this future to complete, and then
1480 <     * returns its result.
1481 <     *
1482 <     * @return the result value
1483 <     * @throws CancellationException if this future was cancelled
1484 <     * @throws ExecutionException if this future completed exceptionally
1485 <     * @throws InterruptedException if the current thread was interrupted
1486 <     * while waiting
1487 <     */
1488 <    public T get() throws InterruptedException, ExecutionException {
1489 <        Object r; Throwable ex, cause;
1490 <        if ((r = result) == null && (r = waitingGet(true)) == null)
1491 <            throw new InterruptedException();
1492 <        if (!(r instanceof AltResult)) {
1493 <            @SuppressWarnings("unchecked") T tr = (T) r;
1494 <            return tr;
1495 <        }
1496 <        if ((ex = ((AltResult)r).ex) == null)
1497 <            return null;
1498 <        if (ex instanceof CancellationException)
1499 <            throw (CancellationException)ex;
1500 <        if ((ex instanceof CompletionException) &&
1501 <            (cause = ex.getCause()) != null)
1502 <            ex = cause;
1503 <        throw new ExecutionException(ex);
1504 <    }
1505 <
1506 <    /**
1507 <     * Waits if necessary for at most the given time for this future
1508 <     * to complete, and then returns its result, if available.
1509 <     *
1510 <     * @param timeout the maximum time to wait
1511 <     * @param unit the time unit of the timeout argument
1512 <     * @return the result value
1513 <     * @throws CancellationException if this future was cancelled
1514 <     * @throws ExecutionException if this future completed exceptionally
1515 <     * @throws InterruptedException if the current thread was interrupted
1516 <     * while waiting
1517 <     * @throws TimeoutException if the wait timed out
1518 <     */
1519 <    public T get(long timeout, TimeUnit unit)
1520 <        throws InterruptedException, ExecutionException, TimeoutException {
1521 <        Object r; Throwable ex, cause;
1522 <        long nanos = unit.toNanos(timeout);
1523 <        if (Thread.interrupted())
1524 <            throw new InterruptedException();
1525 <        if ((r = result) == null)
1526 <            r = timedAwaitDone(nanos);
1527 <        if (!(r instanceof AltResult)) {
1528 <            @SuppressWarnings("unchecked") T tr = (T) r;
1529 <            return tr;
1530 <        }
1531 <        if ((ex = ((AltResult)r).ex) == null)
1532 <            return null;
1533 <        if (ex instanceof CancellationException)
1534 <            throw (CancellationException)ex;
1535 <        if ((ex instanceof CompletionException) &&
1536 <            (cause = ex.getCause()) != null)
1537 <            ex = cause;
1538 <        throw new ExecutionException(ex);
1539 <    }
1540 <
1541 <    /**
1542 <     * Returns the result value when complete, or throws an
1543 <     * (unchecked) exception if completed exceptionally. To better
1544 <     * conform with the use of common functional forms, if a
1545 <     * computation involved in the completion of this
1546 <     * CompletableFuture threw an exception, this method throws an
1547 <     * (unchecked) {@link CompletionException} with the underlying
1548 <     * exception as its cause.
1549 <     *
1550 <     * @return the result value
1551 <     * @throws CancellationException if the computation was cancelled
1552 <     * @throws CompletionException if this future completed
1553 <     * exceptionally or a completion computation threw an exception
1554 <     */
1555 <    public T join() {
1556 <        Object r; Throwable ex;
1557 <        if ((r = result) == null)
1558 <            r = waitingGet(false);
1559 <        if (!(r instanceof AltResult)) {
1560 <            @SuppressWarnings("unchecked") T tr = (T) r;
1561 <            return tr;
1562 <        }
1563 <        if ((ex = ((AltResult)r).ex) == null)
1564 <            return null;
1565 <        if (ex instanceof CancellationException)
1566 <            throw (CancellationException)ex;
1567 <        if (ex instanceof CompletionException)
1568 <            throw (CompletionException)ex;
1569 <        throw new CompletionException(ex);
1570 <    }
1571 <
1572 <    /**
1573 <     * Returns the result value (or throws any encountered exception)
1574 <     * if completed, else returns the given valueIfAbsent.
1575 <     *
1576 <     * @param valueIfAbsent the value to return if not completed
1577 <     * @return the result value, if completed, else the given valueIfAbsent
1578 <     * @throws CancellationException if the computation was cancelled
1579 <     * @throws CompletionException if this future completed
1580 <     * exceptionally or a completion computation threw an exception
1581 <     */
1582 <    public T getNow(T valueIfAbsent) {
1583 <        Object r; Throwable ex;
1584 <        if ((r = result) == null)
1585 <            return valueIfAbsent;
1586 <        if (!(r instanceof AltResult)) {
1587 <            @SuppressWarnings("unchecked") T tr = (T) r;
1588 <            return tr;
1589 <        }
1590 <        if ((ex = ((AltResult)r).ex) == null)
1591 <            return null;
1592 <        if (ex instanceof CancellationException)
1593 <            throw (CancellationException)ex;
1594 <        if (ex instanceof CompletionException)
1595 <            throw (CompletionException)ex;
1596 <        throw new CompletionException(ex);
1597 <    }
1598 <
1599 <    /**
1600 <     * If not already completed, sets the value returned by {@link
1601 <     * #get()} and related methods to the given value.
1602 <     *
1603 <     * @param value the result value
1604 <     * @return {@code true} if this invocation caused this CompletableFuture
1605 <     * to transition to a completed state, else {@code false}
1606 <     */
1607 <    public boolean complete(T value) {
1608 <        boolean triggered = result == null &&
1609 <            UNSAFE.compareAndSwapObject(this, RESULT, null,
1610 <                                        value == null ? NIL : value);
1611 <        postComplete();
1612 <        return triggered;
1613 <    }
1614 <
1615 <    /**
1616 <     * If not already completed, causes invocations of {@link #get()}
1617 <     * and related methods to throw the given exception.
1618 <     *
1619 <     * @param ex the exception
1620 <     * @return {@code true} if this invocation caused this CompletableFuture
1621 <     * to transition to a completed state, else {@code false}
1622 <     */
1623 <    public boolean completeExceptionally(Throwable ex) {
1624 <        if (ex == null) throw new NullPointerException();
1625 <        boolean triggered = result == null &&
1626 <            UNSAFE.compareAndSwapObject(this, RESULT, null, new AltResult(ex));
1627 <        postComplete();
1628 <        return triggered;
1629 <    }
1630 <
1631 <    /**
1632 <     * Returns a new CompletableFuture that is completed
1633 <     * when this CompletableFuture completes, with the result of the
1634 <     * given function of this CompletableFuture's result.
1635 <     *
1636 <     * <p>If this CompletableFuture completes exceptionally, or the
1637 <     * supplied function throws an exception, then the returned
1638 <     * CompletableFuture completes exceptionally with a
1639 <     * CompletionException holding the exception as its cause.
1640 <     *
1641 <     * @param fn the function to use to compute the value of
1642 <     * the returned CompletableFuture
1643 <     * @return the new CompletableFuture
1644 <     */
1645 <    public <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn) {
1646 <        return doThenApply(fn, null);
1647 <    }
1648 <
1649 <    /**
1650 <     * Returns a new CompletableFuture that is asynchronously completed
1651 <     * when this CompletableFuture completes, with the result of the
1652 <     * given function of this CompletableFuture's result from a
1653 <     * task running in the {@link ForkJoinPool#commonPool()}.
1654 <     *
1655 <     * <p>If this CompletableFuture completes exceptionally, or the
1656 <     * supplied function throws an exception, then the returned
1657 <     * CompletableFuture completes exceptionally with a
1658 <     * CompletionException holding the exception as its cause.
1659 <     *
1660 <     * @param fn the function to use to compute the value of
1661 <     * the returned CompletableFuture
1662 <     * @return the new CompletableFuture
1663 <     */
1664 <    public <U> CompletableFuture<U> thenApplyAsync
1665 <        (Function<? super T,? extends U> fn) {
1666 <        return doThenApply(fn, ForkJoinPool.commonPool());
1667 <    }
1668 <
1669 <    /**
1670 <     * Returns a new CompletableFuture that is asynchronously completed
1671 <     * when this CompletableFuture completes, with the result of the
1672 <     * given function of this CompletableFuture's result from a
1673 <     * task running in the given executor.
1674 <     *
1675 <     * <p>If this CompletableFuture completes exceptionally, or the
1676 <     * supplied function throws an exception, then the returned
1677 <     * CompletableFuture completes exceptionally with a
1678 <     * CompletionException holding the exception as its cause.
1679 <     *
1680 <     * @param fn the function to use to compute the value of
1681 <     * the returned CompletableFuture
1682 <     * @param executor the executor to use for asynchronous execution
1683 <     * @return the new CompletableFuture
1684 <     */
1685 <    public <U> CompletableFuture<U> thenApplyAsync
1686 <        (Function<? super T,? extends U> fn,
1687 <         Executor executor) {
1688 <        if (executor == null) throw new NullPointerException();
1689 <        return doThenApply(fn, executor);
1690 <    }
1422 >    // Implementations of stage methods with (plain, async, Executor) forms
1423  
1424      private <U> CompletableFuture<U> doThenApply
1425          (Function<? super T,? extends U> fn,
# Line 1734 | Line 1466 | public class CompletableFuture<T> implem
1466          return dst;
1467      }
1468  
1737    /**
1738     * Returns a new CompletableFuture that is completed
1739     * when this CompletableFuture completes, after performing the given
1740     * action with this CompletableFuture's result.
1741     *
1742     * <p>If this CompletableFuture completes exceptionally, or the
1743     * supplied action throws an exception, then the returned
1744     * CompletableFuture completes exceptionally with a
1745     * CompletionException holding the exception as its cause.
1746     *
1747     * @param block the action to perform before completing the
1748     * returned CompletableFuture
1749     * @return the new CompletableFuture
1750     */
1751    public CompletableFuture<Void> thenAccept(Consumer<? super T> block) {
1752        return doThenAccept(block, null);
1753    }
1754
1755    /**
1756     * Returns a new CompletableFuture that is asynchronously completed
1757     * when this CompletableFuture completes, after performing the given
1758     * action with this CompletableFuture's result from a task running
1759     * in the {@link ForkJoinPool#commonPool()}.
1760     *
1761     * <p>If this CompletableFuture completes exceptionally, or the
1762     * supplied action throws an exception, then the returned
1763     * CompletableFuture completes exceptionally with a
1764     * CompletionException holding the exception as its cause.
1765     *
1766     * @param block the action to perform before completing the
1767     * returned CompletableFuture
1768     * @return the new CompletableFuture
1769     */
1770    public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> block) {
1771        return doThenAccept(block, ForkJoinPool.commonPool());
1772    }
1773
1774    /**
1775     * Returns a new CompletableFuture that is asynchronously completed
1776     * when this CompletableFuture completes, after performing the given
1777     * action with this CompletableFuture's result from a task running
1778     * in the given executor.
1779     *
1780     * <p>If this CompletableFuture completes exceptionally, or the
1781     * supplied action throws an exception, then the returned
1782     * CompletableFuture completes exceptionally with a
1783     * CompletionException holding the exception as its cause.
1784     *
1785     * @param block the action to perform before completing the
1786     * returned CompletableFuture
1787     * @param executor the executor to use for asynchronous execution
1788     * @return the new CompletableFuture
1789     */
1790    public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> block,
1791                                                   Executor executor) {
1792        if (executor == null) throw new NullPointerException();
1793        return doThenAccept(block, executor);
1794    }
1795
1469      private CompletableFuture<Void> doThenAccept(Consumer<? super T> fn,
1470                                                   Executor e) {
1471          if (fn == null) throw new NullPointerException();
# Line 1836 | Line 1509 | public class CompletableFuture<T> implem
1509          return dst;
1510      }
1511  
1839    /**
1840     * Returns a new CompletableFuture that is completed
1841     * when this CompletableFuture completes, after performing the given
1842     * action.
1843     *
1844     * <p>If this CompletableFuture completes exceptionally, or the
1845     * supplied action throws an exception, then the returned
1846     * CompletableFuture completes exceptionally with a
1847     * CompletionException holding the exception as its cause.
1848     *
1849     * @param action the action to perform before completing the
1850     * returned CompletableFuture
1851     * @return the new CompletableFuture
1852     */
1853    public CompletableFuture<Void> thenRun(Runnable action) {
1854        return doThenRun(action, null);
1855    }
1856
1857    /**
1858     * Returns a new CompletableFuture that is asynchronously completed
1859     * when this CompletableFuture completes, after performing the given
1860     * action from a task running in the {@link ForkJoinPool#commonPool()}.
1861     *
1862     * <p>If this CompletableFuture completes exceptionally, or the
1863     * supplied action throws an exception, then the returned
1864     * CompletableFuture completes exceptionally with a
1865     * CompletionException holding the exception as its cause.
1866     *
1867     * @param action the action to perform before completing the
1868     * returned CompletableFuture
1869     * @return the new CompletableFuture
1870     */
1871    public CompletableFuture<Void> thenRunAsync(Runnable action) {
1872        return doThenRun(action, ForkJoinPool.commonPool());
1873    }
1874
1875    /**
1876     * Returns a new CompletableFuture that is asynchronously completed
1877     * when this CompletableFuture completes, after performing the given
1878     * action from a task running in the given executor.
1879     *
1880     * <p>If this CompletableFuture completes exceptionally, or the
1881     * supplied action throws an exception, then the returned
1882     * CompletableFuture completes exceptionally with a
1883     * CompletionException holding the exception as its cause.
1884     *
1885     * @param action the action to perform before completing the
1886     * returned CompletableFuture
1887     * @param executor the executor to use for asynchronous execution
1888     * @return the new CompletableFuture
1889     */
1890    public CompletableFuture<Void> thenRunAsync(Runnable action,
1891                                                Executor executor) {
1892        if (executor == null) throw new NullPointerException();
1893        return doThenRun(action, executor);
1894    }
1895
1512      private CompletableFuture<Void> doThenRun(Runnable action,
1513                                                Executor e) {
1514          if (action == null) throw new NullPointerException();
# Line 1931 | Line 1547 | public class CompletableFuture<T> implem
1547          return dst;
1548      }
1549  
1934    /**
1935     * Returns a new CompletableFuture that is completed
1936     * when both this and the other given CompletableFuture complete,
1937     * with the result of the given function of the results of the two
1938     * CompletableFutures.
1939     *
1940     * <p>If this and/or the other CompletableFuture complete
1941     * exceptionally, or the supplied function throws an exception,
1942     * then the returned CompletableFuture completes exceptionally
1943     * with a CompletionException holding the exception as its cause.
1944     *
1945     * @param other the other CompletableFuture
1946     * @param fn the function to use to compute the value of
1947     * the returned CompletableFuture
1948     * @return the new CompletableFuture
1949     */
1950    public <U,V> CompletableFuture<V> thenCombine
1951        (CompletableFuture<? extends U> other,
1952         BiFunction<? super T,? super U,? extends V> fn) {
1953        return doThenCombine(other, fn, null);
1954    }
1955
1956    /**
1957     * Returns a new CompletableFuture that is asynchronously completed
1958     * when both this and the other given CompletableFuture complete,
1959     * with the result of the given function of the results of the two
1960     * CompletableFutures from a task running in the
1961     * {@link ForkJoinPool#commonPool()}.
1962     *
1963     * <p>If this and/or the other CompletableFuture complete
1964     * exceptionally, or the supplied function throws an exception,
1965     * then the returned CompletableFuture completes exceptionally
1966     * with a CompletionException holding the exception as its cause.
1967     *
1968     * @param other the other CompletableFuture
1969     * @param fn the function to use to compute the value of
1970     * the returned CompletableFuture
1971     * @return the new CompletableFuture
1972     */
1973    public <U,V> CompletableFuture<V> thenCombineAsync
1974        (CompletableFuture<? extends U> other,
1975         BiFunction<? super T,? super U,? extends V> fn) {
1976        return doThenCombine(other, fn, ForkJoinPool.commonPool());
1977    }
1978
1979    /**
1980     * Returns a new CompletableFuture that is asynchronously completed
1981     * when both this and the other given CompletableFuture complete,
1982     * with the result of the given function of the results of the two
1983     * CompletableFutures from a task running in the given executor.
1984     *
1985     * <p>If this and/or the other CompletableFuture complete
1986     * exceptionally, or the supplied function throws an exception,
1987     * then the returned CompletableFuture completes exceptionally
1988     * with a CompletionException holding the exception as its cause.
1989     *
1990     * @param other the other CompletableFuture
1991     * @param fn the function to use to compute the value of
1992     * the returned CompletableFuture
1993     * @param executor the executor to use for asynchronous execution
1994     * @return the new CompletableFuture
1995     */
1996    public <U,V> CompletableFuture<V> thenCombineAsync
1997        (CompletableFuture<? extends U> other,
1998         BiFunction<? super T,? super U,? extends V> fn,
1999         Executor executor) {
2000        if (executor == null) throw new NullPointerException();
2001        return doThenCombine(other, fn, executor);
2002    }
2003
1550      private <U,V> CompletableFuture<V> doThenCombine
1551          (CompletableFuture<? extends U> other,
1552           BiFunction<? super T,? super U,? extends V> fn,
# Line 2069 | Line 1615 | public class CompletableFuture<T> implem
1615          return dst;
1616      }
1617  
2072    /**
2073     * Returns a new CompletableFuture that is completed
2074     * when both this and the other given CompletableFuture complete,
2075     * after performing the given action with the results of the two
2076     * CompletableFutures.
2077     *
2078     * <p>If this and/or the other CompletableFuture complete
2079     * exceptionally, or the supplied action throws an exception,
2080     * then the returned CompletableFuture completes exceptionally
2081     * with a CompletionException holding the exception as its cause.
2082     *
2083     * @param other the other CompletableFuture
2084     * @param block the action to perform before completing the
2085     * returned CompletableFuture
2086     * @return the new CompletableFuture
2087     */
2088    public <U> CompletableFuture<Void> thenAcceptBoth
2089        (CompletableFuture<? extends U> other,
2090         BiConsumer<? super T, ? super U> block) {
2091        return doThenAcceptBoth(other, block, null);
2092    }
2093
2094    /**
2095     * Returns a new CompletableFuture that is asynchronously completed
2096     * when both this and the other given CompletableFuture complete,
2097     * after performing the given action with the results of the two
2098     * CompletableFutures from a task running in the {@link
2099     * ForkJoinPool#commonPool()}.
2100     *
2101     * <p>If this and/or the other CompletableFuture complete
2102     * exceptionally, or the supplied action throws an exception,
2103     * then the returned CompletableFuture completes exceptionally
2104     * with a CompletionException holding the exception as its cause.
2105     *
2106     * @param other the other CompletableFuture
2107     * @param block the action to perform before completing the
2108     * returned CompletableFuture
2109     * @return the new CompletableFuture
2110     */
2111    public <U> CompletableFuture<Void> thenAcceptBothAsync
2112        (CompletableFuture<? extends U> other,
2113         BiConsumer<? super T, ? super U> block) {
2114        return doThenAcceptBoth(other, block, ForkJoinPool.commonPool());
2115    }
2116
2117    /**
2118     * Returns a new CompletableFuture that is asynchronously completed
2119     * when both this and the other given CompletableFuture complete,
2120     * after performing the given action with the results of the two
2121     * CompletableFutures from a task running in the given executor.
2122     *
2123     * <p>If this and/or the other CompletableFuture complete
2124     * exceptionally, or the supplied action throws an exception,
2125     * then the returned CompletableFuture completes exceptionally
2126     * with a CompletionException holding the exception as its cause.
2127     *
2128     * @param other the other CompletableFuture
2129     * @param block the action to perform before completing the
2130     * returned CompletableFuture
2131     * @param executor the executor to use for asynchronous execution
2132     * @return the new CompletableFuture
2133     */
2134    public <U> CompletableFuture<Void> thenAcceptBothAsync
2135        (CompletableFuture<? extends U> other,
2136         BiConsumer<? super T, ? super U> block,
2137         Executor executor) {
2138        if (executor == null) throw new NullPointerException();
2139        return doThenAcceptBoth(other, block, executor);
2140    }
2141
1618      private <U> CompletableFuture<Void> doThenAcceptBoth
1619          (CompletableFuture<? extends U> other,
1620           BiConsumer<? super T,? super U> fn,
# Line 2206 | Line 1682 | public class CompletableFuture<T> implem
1682          return dst;
1683      }
1684  
2209    /**
2210     * Returns a new CompletableFuture that is completed
2211     * when both this and the other given CompletableFuture complete,
2212     * after performing the given action.
2213     *
2214     * <p>If this and/or the other CompletableFuture complete
2215     * exceptionally, or the supplied action throws an exception,
2216     * then the returned CompletableFuture completes exceptionally
2217     * with a CompletionException holding the exception as its cause.
2218     *
2219     * @param other the other CompletableFuture
2220     * @param action the action to perform before completing the
2221     * returned CompletableFuture
2222     * @return the new CompletableFuture
2223     */
2224    public CompletableFuture<Void> runAfterBoth(CompletableFuture<?> other,
2225                                                Runnable action) {
2226        return doRunAfterBoth(other, action, null);
2227    }
2228
2229    /**
2230     * Returns a new CompletableFuture that is asynchronously completed
2231     * when both this and the other given CompletableFuture complete,
2232     * after performing the given action from a task running in the
2233     * {@link ForkJoinPool#commonPool()}.
2234     *
2235     * <p>If this and/or the other CompletableFuture complete
2236     * exceptionally, or the supplied action throws an exception,
2237     * then the returned CompletableFuture completes exceptionally
2238     * with a CompletionException holding the exception as its cause.
2239     *
2240     * @param other the other CompletableFuture
2241     * @param action the action to perform before completing the
2242     * returned CompletableFuture
2243     * @return the new CompletableFuture
2244     */
2245    public CompletableFuture<Void> runAfterBothAsync(CompletableFuture<?> other,
2246                                                     Runnable action) {
2247        return doRunAfterBoth(other, action, ForkJoinPool.commonPool());
2248    }
2249
2250    /**
2251     * Returns a new CompletableFuture that is asynchronously completed
2252     * when both this and the other given CompletableFuture complete,
2253     * after performing the given action from a task running in the
2254     * given executor.
2255     *
2256     * <p>If this and/or the other CompletableFuture complete
2257     * exceptionally, or the supplied action throws an exception,
2258     * then the returned CompletableFuture completes exceptionally
2259     * with a CompletionException holding the exception as its cause.
2260     *
2261     * @param other the other CompletableFuture
2262     * @param action the action to perform before completing the
2263     * returned CompletableFuture
2264     * @param executor the executor to use for asynchronous execution
2265     * @return the new CompletableFuture
2266     */
2267    public CompletableFuture<Void> runAfterBothAsync(CompletableFuture<?> other,
2268                                                     Runnable action,
2269                                                     Executor executor) {
2270        if (executor == null) throw new NullPointerException();
2271        return doRunAfterBoth(other, action, executor);
2272    }
2273
1685      private CompletableFuture<Void> doRunAfterBoth(CompletableFuture<?> other,
1686                                                     Runnable action,
1687                                                     Executor e) {
# Line 2324 | Line 1735 | public class CompletableFuture<T> implem
1735          return dst;
1736      }
1737  
2327    /**
2328     * Returns a new CompletableFuture that is completed
2329     * when either this or the other given CompletableFuture completes,
2330     * with the result of the given function of either this or the other
2331     * CompletableFuture's result.
2332     *
2333     * <p>If this and/or the other CompletableFuture complete
2334     * exceptionally, then the returned CompletableFuture may also do so,
2335     * with a CompletionException holding one of these exceptions as its
2336     * cause.  No guarantees are made about which result or exception is
2337     * used in the returned CompletableFuture.  If the supplied function
2338     * throws an exception, then the returned CompletableFuture completes
2339     * exceptionally with a CompletionException holding the exception as
2340     * its cause.
2341     *
2342     * @param other the other CompletableFuture
2343     * @param fn the function to use to compute the value of
2344     * the returned CompletableFuture
2345     * @return the new CompletableFuture
2346     */
2347    public <U> CompletableFuture<U> applyToEither
2348        (CompletableFuture<? extends T> other,
2349         Function<? super T, U> fn) {
2350        return doApplyToEither(other, fn, null);
2351    }
2352
2353    /**
2354     * Returns a new CompletableFuture that is asynchronously completed
2355     * when either this or the other given CompletableFuture completes,
2356     * with the result of the given function of either this or the other
2357     * CompletableFuture's result from a task running in the
2358     * {@link ForkJoinPool#commonPool()}.
2359     *
2360     * <p>If this and/or the other CompletableFuture complete
2361     * exceptionally, then the returned CompletableFuture may also do so,
2362     * with a CompletionException holding one of these exceptions as its
2363     * cause.  No guarantees are made about which result or exception is
2364     * used in the returned CompletableFuture.  If the supplied function
2365     * throws an exception, then the returned CompletableFuture completes
2366     * exceptionally with a CompletionException holding the exception as
2367     * its cause.
2368     *
2369     * @param other the other CompletableFuture
2370     * @param fn the function to use to compute the value of
2371     * the returned CompletableFuture
2372     * @return the new CompletableFuture
2373     */
2374    public <U> CompletableFuture<U> applyToEitherAsync
2375        (CompletableFuture<? extends T> other,
2376         Function<? super T, U> fn) {
2377        return doApplyToEither(other, fn, ForkJoinPool.commonPool());
2378    }
2379
2380    /**
2381     * Returns a new CompletableFuture that is asynchronously completed
2382     * when either this or the other given CompletableFuture completes,
2383     * with the result of the given function of either this or the other
2384     * CompletableFuture's result from a task running in the
2385     * given executor.
2386     *
2387     * <p>If this and/or the other CompletableFuture complete
2388     * exceptionally, then the returned CompletableFuture may also do so,
2389     * with a CompletionException holding one of these exceptions as its
2390     * cause.  No guarantees are made about which result or exception is
2391     * used in the returned CompletableFuture.  If the supplied function
2392     * throws an exception, then the returned CompletableFuture completes
2393     * exceptionally with a CompletionException holding the exception as
2394     * its cause.
2395     *
2396     * @param other the other CompletableFuture
2397     * @param fn the function to use to compute the value of
2398     * the returned CompletableFuture
2399     * @param executor the executor to use for asynchronous execution
2400     * @return the new CompletableFuture
2401     */
2402    public <U> CompletableFuture<U> applyToEitherAsync
2403        (CompletableFuture<? extends T> other,
2404         Function<? super T, U> fn,
2405         Executor executor) {
2406        if (executor == null) throw new NullPointerException();
2407        return doApplyToEither(other, fn, executor);
2408    }
2409
1738      private <U> CompletableFuture<U> doApplyToEither
1739          (CompletableFuture<? extends T> other,
1740           Function<? super T, U> fn,
# Line 2459 | Line 1787 | public class CompletableFuture<T> implem
1787          return dst;
1788      }
1789  
2462    /**
2463     * Returns a new CompletableFuture that is completed
2464     * when either this or the other given CompletableFuture completes,
2465     * after performing the given action with the result of either this
2466     * or the other CompletableFuture's result.
2467     *
2468     * <p>If this and/or the other CompletableFuture complete
2469     * exceptionally, then the returned CompletableFuture may also do so,
2470     * with a CompletionException holding one of these exceptions as its
2471     * cause.  No guarantees are made about which result or exception is
2472     * used in the returned CompletableFuture.  If the supplied action
2473     * throws an exception, then the returned CompletableFuture completes
2474     * exceptionally with a CompletionException holding the exception as
2475     * its cause.
2476     *
2477     * @param other the other CompletableFuture
2478     * @param block the action to perform before completing the
2479     * returned CompletableFuture
2480     * @return the new CompletableFuture
2481     */
2482    public CompletableFuture<Void> acceptEither
2483        (CompletableFuture<? extends T> other,
2484         Consumer<? super T> block) {
2485        return doAcceptEither(other, block, null);
2486    }
2487
2488    /**
2489     * Returns a new CompletableFuture that is asynchronously completed
2490     * when either this or the other given CompletableFuture completes,
2491     * after performing the given action with the result of either this
2492     * or the other CompletableFuture's result from a task running in
2493     * the {@link ForkJoinPool#commonPool()}.
2494     *
2495     * <p>If this and/or the other CompletableFuture complete
2496     * exceptionally, then the returned CompletableFuture may also do so,
2497     * with a CompletionException holding one of these exceptions as its
2498     * cause.  No guarantees are made about which result or exception is
2499     * used in the returned CompletableFuture.  If the supplied action
2500     * throws an exception, then the returned CompletableFuture completes
2501     * exceptionally with a CompletionException holding the exception as
2502     * its cause.
2503     *
2504     * @param other the other CompletableFuture
2505     * @param block the action to perform before completing the
2506     * returned CompletableFuture
2507     * @return the new CompletableFuture
2508     */
2509    public CompletableFuture<Void> acceptEitherAsync
2510        (CompletableFuture<? extends T> other,
2511         Consumer<? super T> block) {
2512        return doAcceptEither(other, block, ForkJoinPool.commonPool());
2513    }
2514
2515    /**
2516     * Returns a new CompletableFuture that is asynchronously completed
2517     * when either this or the other given CompletableFuture completes,
2518     * after performing the given action with the result of either this
2519     * or the other CompletableFuture's result from a task running in
2520     * the given executor.
2521     *
2522     * <p>If this and/or the other CompletableFuture complete
2523     * exceptionally, then the returned CompletableFuture may also do so,
2524     * with a CompletionException holding one of these exceptions as its
2525     * cause.  No guarantees are made about which result or exception is
2526     * used in the returned CompletableFuture.  If the supplied action
2527     * throws an exception, then the returned CompletableFuture completes
2528     * exceptionally with a CompletionException holding the exception as
2529     * its cause.
2530     *
2531     * @param other the other CompletableFuture
2532     * @param block the action to perform before completing the
2533     * returned CompletableFuture
2534     * @param executor the executor to use for asynchronous execution
2535     * @return the new CompletableFuture
2536     */
2537    public CompletableFuture<Void> acceptEitherAsync
2538        (CompletableFuture<? extends T> other,
2539         Consumer<? super T> block,
2540         Executor executor) {
2541        if (executor == null) throw new NullPointerException();
2542        return doAcceptEither(other, block, executor);
2543    }
2544
1790      private CompletableFuture<Void> doAcceptEither
1791          (CompletableFuture<? extends T> other,
1792           Consumer<? super T> fn,
# Line 2593 | Line 1838 | public class CompletableFuture<T> implem
1838          return dst;
1839      }
1840  
2596    /**
2597     * Returns a new CompletableFuture that is completed
2598     * when either this or the other given CompletableFuture completes,
2599     * after performing the given action.
2600     *
2601     * <p>If this and/or the other CompletableFuture complete
2602     * exceptionally, then the returned CompletableFuture may also do so,
2603     * with a CompletionException holding one of these exceptions as its
2604     * cause.  No guarantees are made about which result or exception is
2605     * used in the returned CompletableFuture.  If the supplied action
2606     * throws an exception, then the returned CompletableFuture completes
2607     * exceptionally with a CompletionException holding the exception as
2608     * its cause.
2609     *
2610     * @param other the other CompletableFuture
2611     * @param action the action to perform before completing the
2612     * returned CompletableFuture
2613     * @return the new CompletableFuture
2614     */
2615    public CompletableFuture<Void> runAfterEither(CompletableFuture<?> other,
2616                                                  Runnable action) {
2617        return doRunAfterEither(other, action, null);
2618    }
2619
2620    /**
2621     * Returns a new CompletableFuture that is asynchronously completed
2622     * when either this or the other given CompletableFuture completes,
2623     * after performing the given action from a task running in the
2624     * {@link ForkJoinPool#commonPool()}.
2625     *
2626     * <p>If this and/or the other CompletableFuture complete
2627     * exceptionally, then the returned CompletableFuture may also do so,
2628     * with a CompletionException holding one of these exceptions as its
2629     * cause.  No guarantees are made about which result or exception is
2630     * used in the returned CompletableFuture.  If the supplied action
2631     * throws an exception, then the returned CompletableFuture completes
2632     * exceptionally with a CompletionException holding the exception as
2633     * its cause.
2634     *
2635     * @param other the other CompletableFuture
2636     * @param action the action to perform before completing the
2637     * returned CompletableFuture
2638     * @return the new CompletableFuture
2639     */
2640    public CompletableFuture<Void> runAfterEitherAsync
2641        (CompletableFuture<?> other,
2642         Runnable action) {
2643        return doRunAfterEither(other, action, ForkJoinPool.commonPool());
2644    }
2645
2646    /**
2647     * Returns a new CompletableFuture that is asynchronously completed
2648     * when either this or the other given CompletableFuture completes,
2649     * after performing the given action from a task running in the
2650     * given executor.
2651     *
2652     * <p>If this and/or the other CompletableFuture complete
2653     * exceptionally, then the returned CompletableFuture may also do so,
2654     * with a CompletionException holding one of these exceptions as its
2655     * cause.  No guarantees are made about which result or exception is
2656     * used in the returned CompletableFuture.  If the supplied action
2657     * throws an exception, then the returned CompletableFuture completes
2658     * exceptionally with a CompletionException holding the exception as
2659     * its cause.
2660     *
2661     * @param other the other CompletableFuture
2662     * @param action the action to perform before completing the
2663     * returned CompletableFuture
2664     * @param executor the executor to use for asynchronous execution
2665     * @return the new CompletableFuture
2666     */
2667    public CompletableFuture<Void> runAfterEitherAsync
2668        (CompletableFuture<?> other,
2669         Runnable action,
2670         Executor executor) {
2671        if (executor == null) throw new NullPointerException();
2672        return doRunAfterEither(other, action, executor);
2673    }
2674
1841      private CompletableFuture<Void> doRunAfterEither
1842          (CompletableFuture<?> other,
1843           Runnable action,
# Line 2718 | Line 1884 | public class CompletableFuture<T> implem
1884          return dst;
1885      }
1886  
2721    /**
2722     * Returns a CompletableFuture that upon completion, has the same
2723     * value as produced by the given function of the result of this
2724     * CompletableFuture.
2725     *
2726     * <p>If this CompletableFuture completes exceptionally, then the
2727     * returned CompletableFuture also does so, with a
2728     * CompletionException holding this exception as its cause.
2729     * Similarly, if the computed CompletableFuture completes
2730     * exceptionally, then so does the returned CompletableFuture.
2731     *
2732     * @param fn the function returning a new CompletableFuture
2733     * @return the CompletableFuture
2734     */
2735    public <U> CompletableFuture<U> thenCompose
2736        (Function<? super T, CompletableFuture<U>> fn) {
2737        return doThenCompose(fn, null);
2738    }
2739
2740    /**
2741     * Returns a CompletableFuture that upon completion, has the same
2742     * value as that produced asynchronously using the {@link
2743     * ForkJoinPool#commonPool()} by the given function of the result
2744     * of this CompletableFuture.
2745     *
2746     * <p>If this CompletableFuture completes exceptionally, then the
2747     * returned CompletableFuture also does so, with a
2748     * CompletionException holding this exception as its cause.
2749     * Similarly, if the computed CompletableFuture completes
2750     * exceptionally, then so does the returned CompletableFuture.
2751     *
2752     * @param fn the function returning a new CompletableFuture
2753     * @return the CompletableFuture
2754     */
2755    public <U> CompletableFuture<U> thenComposeAsync
2756        (Function<? super T, CompletableFuture<U>> fn) {
2757        return doThenCompose(fn, ForkJoinPool.commonPool());
2758    }
2759
2760    /**
2761     * Returns a CompletableFuture that upon completion, has the same
2762     * value as that produced asynchronously using the given executor
2763     * by the given function of this CompletableFuture.
2764     *
2765     * <p>If this CompletableFuture completes exceptionally, then the
2766     * returned CompletableFuture also does so, with a
2767     * CompletionException holding this exception as its cause.
2768     * Similarly, if the computed CompletableFuture completes
2769     * exceptionally, then so does the returned CompletableFuture.
2770     *
2771     * @param fn the function returning a new CompletableFuture
2772     * @param executor the executor to use for asynchronous execution
2773     * @return the CompletableFuture
2774     */
2775    public <U> CompletableFuture<U> thenComposeAsync
2776        (Function<? super T, CompletableFuture<U>> fn,
2777         Executor executor) {
2778        if (executor == null) throw new NullPointerException();
2779        return doThenCompose(fn, executor);
2780    }
2781
1887      private <U> CompletableFuture<U> doThenCompose
1888 <        (Function<? super T, CompletableFuture<U>> fn,
1888 >        (Function<? super T, ? extends CompletionStage<U>> fn,
1889           Executor e) {
1890          if (fn == null) throw new NullPointerException();
1891          CompletableFuture<U> dst = null;
# Line 2815 | Line 1920 | public class CompletableFuture<T> implem
1920                  }
1921                  else {
1922                      try {
1923 <                        if ((dst = fn.apply(t)) == null)
1923 >                        CompletionStage<U> cs = fn.apply(t);
1924 >                        if (cs == null ||
1925 >                            (dst = cs.toCompletableFuture()) == null)
1926                              ex = new NullPointerException();
1927                      } catch (Throwable rex) {
1928                          ex = rex;
# Line 2832 | Line 1939 | public class CompletableFuture<T> implem
1939          return dst;
1940      }
1941  
1942 <    /**
1943 <     * Returns a new CompletableFuture that is completed when this
1944 <     * CompletableFuture completes, with the result of the given
2838 <     * function of the exception triggering this CompletableFuture's
2839 <     * completion when it completes exceptionally; otherwise, if this
2840 <     * CompletableFuture completes normally, then the returned
2841 <     * CompletableFuture also completes normally with the same value.
2842 <     *
2843 <     * @param fn the function to use to compute the value of the
2844 <     * returned CompletableFuture if this CompletableFuture completed
2845 <     * exceptionally
2846 <     * @return the new CompletableFuture
2847 <     */
2848 <    public CompletableFuture<T> exceptionally
2849 <        (Function<Throwable, ? extends T> fn) {
1942 >    private CompletableFuture<T> doWhenComplete
1943 >        (BiConsumer<? super T, ? super Throwable> fn,
1944 >         Executor e) {
1945          if (fn == null) throw new NullPointerException();
1946          CompletableFuture<T> dst = new CompletableFuture<T>();
1947 <        ExceptionCompletion<T> d = null;
1947 >        WhenCompleteCompletion<T> d = null;
1948          Object r;
1949          if ((r = result) == null) {
1950              CompletionNode p =
1951 <                new CompletionNode(d = new ExceptionCompletion<T>(this, fn, dst));
1951 >                new CompletionNode(d = new WhenCompleteCompletion<T>
1952 >                                   (this, fn, dst, e));
1953              while ((r = result) == null) {
1954                  if (UNSAFE.compareAndSwapObject(this, COMPLETIONS,
1955                                                  p.next = completions, p))
# Line 2861 | Line 1957 | public class CompletableFuture<T> implem
1957              }
1958          }
1959          if (r != null && (d == null || d.compareAndSet(0, 1))) {
1960 <            T t = null; Throwable ex, dx = null;
1960 >            T t; Throwable ex;
1961              if (r instanceof AltResult) {
1962 <                if ((ex = ((AltResult)r).ex) != null) {
1963 <                    try {
2868 <                        t = fn.apply(ex);
2869 <                    } catch (Throwable rex) {
2870 <                        dx = rex;
2871 <                    }
2872 <                }
1962 >                ex = ((AltResult)r).ex;
1963 >                t = null;
1964              }
1965              else {
1966 +                ex = null;
1967                  @SuppressWarnings("unchecked") T tr = (T) r;
1968                  t = tr;
1969              }
1970 <            dst.internalComplete(t, dx);
1970 >            Throwable dx = null;
1971 >            try {
1972 >                if (e != null)
1973 >                    e.execute(new AsyncAcceptBoth<T,Throwable>(t, ex, fn, dst));
1974 >                else
1975 >                    fn.accept(t, ex);
1976 >            } catch (Throwable rex) {
1977 >                dx = rex;
1978 >            }
1979 >            if (e == null || dx != null)
1980 >                dst.internalComplete(t, ex != null ? ex : dx);
1981          }
1982          helpPostComplete();
1983          return dst;
1984      }
1985  
1986 <    /**
1987 <     * Returns a new CompletableFuture that is completed when this
1988 <     * CompletableFuture completes, with the result of the given
2887 <     * function of the result and exception of this CompletableFuture's
2888 <     * completion.  The given function is invoked with the result (or
2889 <     * {@code null} if none) and the exception (or {@code null} if none)
2890 <     * of this CompletableFuture when complete.
2891 <     *
2892 <     * @param fn the function to use to compute the value of the
2893 <     * returned CompletableFuture
2894 <     * @return the new CompletableFuture
2895 <     */
2896 <    public <U> CompletableFuture<U> handle
2897 <        (BiFunction<? super T, Throwable, ? extends U> fn) {
1986 >    private <U> CompletableFuture<U> doHandle
1987 >        (BiFunction<? super T, Throwable, ? extends U> fn,
1988 >         Executor e) {
1989          if (fn == null) throw new NullPointerException();
1990          CompletableFuture<U> dst = new CompletableFuture<U>();
1991          HandleCompletion<T,U> d = null;
1992          Object r;
1993          if ((r = result) == null) {
1994              CompletionNode p =
1995 <                new CompletionNode(d = new HandleCompletion<T,U>(this, fn, dst));
1995 >                new CompletionNode(d = new HandleCompletion<T,U>
1996 >                                   (this, fn, dst, e));
1997              while ((r = result) == null) {
1998                  if (UNSAFE.compareAndSwapObject(this, COMPLETIONS,
1999                                                  p.next = completions, p))
# Line 2919 | Line 2011 | public class CompletableFuture<T> implem
2011                  @SuppressWarnings("unchecked") T tr = (T) r;
2012                  t = tr;
2013              }
2014 <            U u; Throwable dx;
2014 >            U u = null; Throwable dx = null;
2015 >            Throwable dx = null;
2016              try {
2017 <                u = fn.apply(t, ex);
2018 <                dx = null;
2017 >                if (e != null)
2018 >                    e.execute(new AsyncCombine<T,Throwable,U>(t, ex, fn, dst));
2019 >                else {
2020 >                    u = fn.apply(t, ex);
2021 >                    dx = null;
2022 >                }
2023              } catch (Throwable rex) {
2024                  dx = rex;
2025                  u = null;
2026              }
2027 +            if (e == null || dx != null)
2028 +                dst.internalComplete(u, dx);
2029              dst.internalComplete(u, dx);
2030          }
2031          helpPostComplete();
# Line 2934 | Line 2033 | public class CompletableFuture<T> implem
2033      }
2034  
2035  
2036 +    // public methods
2037 +
2038 +    /**
2039 +     * Creates a new incomplete CompletableFuture.
2040 +     */
2041 +    public CompletableFuture() {
2042 +    }
2043 +
2044 +    /**
2045 +     * Returns a new CompletableFuture that is asynchronously completed
2046 +     * by a task running in the {@link ForkJoinPool#commonPool()} with
2047 +     * the value obtained by calling the given Supplier.
2048 +     *
2049 +     * @param supplier a function returning the value to be used
2050 +     * to complete the returned CompletableFuture
2051 +     * @return the new CompletableFuture
2052 +     */
2053 +    public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) {
2054 +        if (supplier == null) throw new NullPointerException();
2055 +        CompletableFuture<U> f = new CompletableFuture<U>();
2056 +        ForkJoinPool.commonPool().
2057 +            execute((ForkJoinTask<?>)new AsyncSupply<U>(supplier, f));
2058 +        return f;
2059 +    }
2060 +
2061 +    /**
2062 +     * Returns a new CompletableFuture that is asynchronously completed
2063 +     * by a task running in the given executor with the value obtained
2064 +     * by calling the given Supplier.
2065 +     *
2066 +     * @param supplier a function returning the value to be used
2067 +     * to complete the returned CompletableFuture
2068 +     * @param executor the executor to use for asynchronous execution
2069 +     * @return the new CompletableFuture
2070 +     */
2071 +    public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier,
2072 +                                                       Executor executor) {
2073 +        if (executor == null || supplier == null)
2074 +            throw new NullPointerException();
2075 +        CompletableFuture<U> f = new CompletableFuture<U>();
2076 +        executor.execute(new AsyncSupply<U>(supplier, f));
2077 +        return f;
2078 +    }
2079 +
2080 +    /**
2081 +     * Returns a new CompletableFuture that is asynchronously completed
2082 +     * by a task running in the {@link ForkJoinPool#commonPool()} after
2083 +     * it runs the given action.
2084 +     *
2085 +     * @param runnable the action to run before completing the
2086 +     * returned CompletableFuture
2087 +     * @return the new CompletableFuture
2088 +     */
2089 +    public static CompletableFuture<Void> runAsync(Runnable runnable) {
2090 +        if (runnable == null) throw new NullPointerException();
2091 +        CompletableFuture<Void> f = new CompletableFuture<Void>();
2092 +        ForkJoinPool.commonPool().
2093 +            execute((ForkJoinTask<?>)new AsyncRun(runnable, f));
2094 +        return f;
2095 +    }
2096 +
2097 +    /**
2098 +     * Returns a new CompletableFuture that is asynchronously completed
2099 +     * by a task running in the given executor after it runs the given
2100 +     * action.
2101 +     *
2102 +     * @param runnable the action to run before completing the
2103 +     * returned CompletableFuture
2104 +     * @param executor the executor to use for asynchronous execution
2105 +     * @return the new CompletableFuture
2106 +     */
2107 +    public static CompletableFuture<Void> runAsync(Runnable runnable,
2108 +                                                   Executor executor) {
2109 +        if (executor == null || runnable == null)
2110 +            throw new NullPointerException();
2111 +        CompletableFuture<Void> f = new CompletableFuture<Void>();
2112 +        executor.execute(new AsyncRun(runnable, f));
2113 +        return f;
2114 +    }
2115 +
2116 +    /**
2117 +     * Returns a new CompletableFuture that is already completed with
2118 +     * the given value.
2119 +     *
2120 +     * @param value the value
2121 +     * @return the completed CompletableFuture
2122 +     */
2123 +    public static <U> CompletableFuture<U> completedFuture(U value) {
2124 +        CompletableFuture<U> f = new CompletableFuture<U>();
2125 +        f.result = (value == null) ? NIL : value;
2126 +        return f;
2127 +    }
2128 +
2129 +    /**
2130 +     * Returns {@code true} if completed in any fashion: normally,
2131 +     * exceptionally, or via cancellation.
2132 +     *
2133 +     * @return {@code true} if completed
2134 +     */
2135 +    public boolean isDone() {
2136 +        return result != null;
2137 +    }
2138 +
2139 +    /**
2140 +     * Waits if necessary for this future to complete, and then
2141 +     * returns its result.
2142 +     *
2143 +     * @return the result value
2144 +     * @throws CancellationException if this future was cancelled
2145 +     * @throws ExecutionException if this future completed exceptionally
2146 +     * @throws InterruptedException if the current thread was interrupted
2147 +     * while waiting
2148 +     */
2149 +    public T get() throws InterruptedException, ExecutionException {
2150 +        Object r; Throwable ex, cause;
2151 +        if ((r = result) == null && (r = waitingGet(true)) == null)
2152 +            throw new InterruptedException();
2153 +        if (!(r instanceof AltResult)) {
2154 +            @SuppressWarnings("unchecked") T tr = (T) r;
2155 +            return tr;
2156 +        }
2157 +        if ((ex = ((AltResult)r).ex) == null)
2158 +            return null;
2159 +        if (ex instanceof CancellationException)
2160 +            throw (CancellationException)ex;
2161 +        if ((ex instanceof CompletionException) &&
2162 +            (cause = ex.getCause()) != null)
2163 +            ex = cause;
2164 +        throw new ExecutionException(ex);
2165 +    }
2166 +
2167 +    /**
2168 +     * Waits if necessary for at most the given time for this future
2169 +     * to complete, and then returns its result, if available.
2170 +     *
2171 +     * @param timeout the maximum time to wait
2172 +     * @param unit the time unit of the timeout argument
2173 +     * @return the result value
2174 +     * @throws CancellationException if this future was cancelled
2175 +     * @throws ExecutionException if this future completed exceptionally
2176 +     * @throws InterruptedException if the current thread was interrupted
2177 +     * while waiting
2178 +     * @throws TimeoutException if the wait timed out
2179 +     */
2180 +    public T get(long timeout, TimeUnit unit)
2181 +        throws InterruptedException, ExecutionException, TimeoutException {
2182 +        Object r; Throwable ex, cause;
2183 +        long nanos = unit.toNanos(timeout);
2184 +        if (Thread.interrupted())
2185 +            throw new InterruptedException();
2186 +        if ((r = result) == null)
2187 +            r = timedAwaitDone(nanos);
2188 +        if (!(r instanceof AltResult)) {
2189 +            @SuppressWarnings("unchecked") T tr = (T) r;
2190 +            return tr;
2191 +        }
2192 +        if ((ex = ((AltResult)r).ex) == null)
2193 +            return null;
2194 +        if (ex instanceof CancellationException)
2195 +            throw (CancellationException)ex;
2196 +        if ((ex instanceof CompletionException) &&
2197 +            (cause = ex.getCause()) != null)
2198 +            ex = cause;
2199 +        throw new ExecutionException(ex);
2200 +    }
2201 +
2202 +    /**
2203 +     * Returns the result value when complete, or throws an
2204 +     * (unchecked) exception if completed exceptionally. To better
2205 +     * conform with the use of common functional forms, if a
2206 +     * computation involved in the completion of this
2207 +     * CompletableFuture threw an exception, this method throws an
2208 +     * (unchecked) {@link CompletionException} with the underlying
2209 +     * exception as its cause.
2210 +     *
2211 +     * @return the result value
2212 +     * @throws CancellationException if the computation was cancelled
2213 +     * @throws CompletionException if this future completed
2214 +     * exceptionally or a completion computation threw an exception
2215 +     */
2216 +    public T join() {
2217 +        Object r; Throwable ex;
2218 +        if ((r = result) == null)
2219 +            r = waitingGet(false);
2220 +        if (!(r instanceof AltResult)) {
2221 +            @SuppressWarnings("unchecked") T tr = (T) r;
2222 +            return tr;
2223 +        }
2224 +        if ((ex = ((AltResult)r).ex) == null)
2225 +            return null;
2226 +        if (ex instanceof CancellationException)
2227 +            throw (CancellationException)ex;
2228 +        if (ex instanceof CompletionException)
2229 +            throw (CompletionException)ex;
2230 +        throw new CompletionException(ex);
2231 +    }
2232 +
2233 +    /**
2234 +     * Returns the result value (or throws any encountered exception)
2235 +     * if completed, else returns the given valueIfAbsent.
2236 +     *
2237 +     * @param valueIfAbsent the value to return if not completed
2238 +     * @return the result value, if completed, else the given valueIfAbsent
2239 +     * @throws CancellationException if the computation was cancelled
2240 +     * @throws CompletionException if this future completed
2241 +     * exceptionally or a completion computation threw an exception
2242 +     */
2243 +    public T getNow(T valueIfAbsent) {
2244 +        Object r; Throwable ex;
2245 +        if ((r = result) == null)
2246 +            return valueIfAbsent;
2247 +        if (!(r instanceof AltResult)) {
2248 +            @SuppressWarnings("unchecked") T tr = (T) r;
2249 +            return tr;
2250 +        }
2251 +        if ((ex = ((AltResult)r).ex) == null)
2252 +            return null;
2253 +        if (ex instanceof CancellationException)
2254 +            throw (CancellationException)ex;
2255 +        if (ex instanceof CompletionException)
2256 +            throw (CompletionException)ex;
2257 +        throw new CompletionException(ex);
2258 +    }
2259 +
2260 +    /**
2261 +     * If not already completed, sets the value returned by {@link
2262 +     * #get()} and related methods to the given value.
2263 +     *
2264 +     * @param value the result value
2265 +     * @return {@code true} if this invocation caused this CompletableFuture
2266 +     * to transition to a completed state, else {@code false}
2267 +     */
2268 +    public boolean complete(T value) {
2269 +        boolean triggered = result == null &&
2270 +            UNSAFE.compareAndSwapObject(this, RESULT, null,
2271 +                                        value == null ? NIL : value);
2272 +        postComplete();
2273 +        return triggered;
2274 +    }
2275 +
2276 +    /**
2277 +     * If not already completed, causes invocations of {@link #get()}
2278 +     * and related methods to throw the given exception.
2279 +     *
2280 +     * @param ex the exception
2281 +     * @return {@code true} if this invocation caused this CompletableFuture
2282 +     * to transition to a completed state, else {@code false}
2283 +     */
2284 +    public boolean completeExceptionally(Throwable ex) {
2285 +        if (ex == null) throw new NullPointerException();
2286 +        boolean triggered = result == null &&
2287 +            UNSAFE.compareAndSwapObject(this, RESULT, null, new AltResult(ex));
2288 +        postComplete();
2289 +        return triggered;
2290 +    }
2291 +
2292 +    // CompletionStage methods
2293 +
2294 +    public <U> CompletableFuture<U> thenApply
2295 +        (Function<? super T,? extends U> fn) {
2296 +        return doThenApply(fn, null);
2297 +    }
2298 +
2299 +    public <U> CompletableFuture<U> thenApplyAsync
2300 +        (Function<? super T,? extends U> fn) {
2301 +        return doThenApply(fn, ForkJoinPool.commonPool());
2302 +    }
2303 +
2304 +    public <U> CompletableFuture<U> thenApplyAsync
2305 +        (Function<? super T,? extends U> fn,
2306 +         Executor executor) {
2307 +        if (executor == null) throw new NullPointerException();
2308 +        return doThenApply(fn, executor);
2309 +    }
2310 +
2311 +    public CompletableFuture<Void> thenAccept
2312 +        (Consumer<? super T> action) {
2313 +        return doThenAccept(action, null);
2314 +    }
2315 +
2316 +    public CompletableFuture<Void> thenAcceptAsync
2317 +        (Consumer<? super T> action) {
2318 +        return doThenAccept(action, ForkJoinPool.commonPool());
2319 +    }
2320 +
2321 +    public CompletableFuture<Void> thenAcceptAsync
2322 +        (Consumer<? super T> action,
2323 +         Executor executor) {
2324 +        if (executor == null) throw new NullPointerException();
2325 +        return doThenAccept(action, executor);
2326 +    }
2327 +
2328 +    public CompletableFuture<Void> thenRun
2329 +        (Runnable action) {
2330 +        return doThenRun(action, null);
2331 +    }
2332 +
2333 +    public CompletableFuture<Void> thenRunAsync
2334 +        (Runnable action) {
2335 +        return doThenRun(action, ForkJoinPool.commonPool());
2336 +    }
2337 +
2338 +    public CompletableFuture<Void> thenRunAsync
2339 +        (Runnable action,
2340 +         Executor executor) {
2341 +        if (executor == null) throw new NullPointerException();
2342 +        return doThenRun(action, executor);
2343 +    }
2344 +
2345 +    public <U,V> CompletableFuture<V> thenCombine
2346 +        (CompletionStage<? extends U> other,
2347 +         BiFunction<? super T,? super U,? extends V> fn) {
2348 +        return doThenCombine(other.toCompletableFuture(), fn, null);
2349 +    }
2350 +
2351 +    public <U,V> CompletableFuture<V> thenCombineAsync
2352 +        (CompletionStage<? extends U> other,
2353 +         BiFunction<? super T,? super U,? extends V> fn) {
2354 +        return doThenCombine(other.toCompletableFuture(), fn,
2355 +                             ForkJoinPool.commonPool());
2356 +    }
2357 +
2358 +    public <U,V> CompletableFuture<V> thenCombineAsync
2359 +        (CompletionStage<? extends U> other,
2360 +         BiFunction<? super T,? super U,? extends V> fn,
2361 +         Executor executor) {
2362 +        if (executor == null) throw new NullPointerException();
2363 +        return doThenCombine(other.toCompletableFuture(), fn, executor);
2364 +    }
2365 +
2366 +    public <U> CompletableFuture<Void> thenAcceptBoth
2367 +        (CompletionStage<? extends U> other,
2368 +         BiConsumer<? super T, ? super U> action) {
2369 +        return doThenAcceptBoth(other.toCompletableFuture(), action, null);
2370 +    }
2371 +
2372 +    public <U> CompletableFuture<Void> thenAcceptBothAsync
2373 +        (CompletionStage<? extends U> other,
2374 +         BiConsumer<? super T, ? super U> action) {
2375 +        return doThenAcceptBoth(other.toCompletableFuture(), action,
2376 +                                ForkJoinPool.commonPool());
2377 +    }
2378 +
2379 +    public <U> CompletableFuture<Void> thenAcceptBothAsync
2380 +        (CompletionStage<? extends U> other,
2381 +         BiConsumer<? super T, ? super U> action,
2382 +         Executor executor) {
2383 +        if (executor == null) throw new NullPointerException();
2384 +        return doThenAcceptBoth(other.toCompletableFuture(), action, executor);
2385 +    }
2386 +
2387 +    public CompletableFuture<Void> runAfterBoth
2388 +        (CompletionStage<?> other,
2389 +         Runnable action) {
2390 +        return doRunAfterBoth(other.toCompletableFuture(), action, null);
2391 +    }
2392 +
2393 +    public CompletableFuture<Void> runAfterBothAsync
2394 +        (CompletionStage<?> other,
2395 +         Runnable action) {
2396 +        return doRunAfterBoth(other.toCompletableFuture(), action,
2397 +                              ForkJoinPool.commonPool());
2398 +    }
2399 +
2400 +    public CompletableFuture<Void> runAfterBothAsync
2401 +        (CompletionStage<?> other,
2402 +         Runnable action,
2403 +         Executor executor) {
2404 +        if (executor == null) throw new NullPointerException();
2405 +        return doRunAfterBoth(other.toCompletableFuture(), action, executor);
2406 +    }
2407 +
2408 +
2409 +    public <U> CompletableFuture<U> applyToEither
2410 +        (CompletionStage<? extends T> other,
2411 +         Function<? super T, U> fn) {
2412 +        return doApplyToEither(other.toCompletableFuture(), fn, null);
2413 +    }
2414 +
2415 +    public <U> CompletableFuture<U> applyToEitherAsync
2416 +        (CompletionStage<? extends T> other,
2417 +         Function<? super T, U> fn) {
2418 +        return doApplyToEither(other.toCompletableFuture(), fn,
2419 +                               ForkJoinPool.commonPool());
2420 +    }
2421 +
2422 +    public <U> CompletableFuture<U> applyToEitherAsync
2423 +        (CompletionStage<? extends T> other,
2424 +         Function<? super T, U> fn,
2425 +         Executor executor) {
2426 +        if (executor == null) throw new NullPointerException();
2427 +        return doApplyToEither(other.toCompletableFuture(), fn, executor);
2428 +    }
2429 +
2430 +    public CompletableFuture<Void> acceptEither
2431 +        (CompletionStage<? extends T> other,
2432 +         Consumer<? super T> action) {
2433 +        return doAcceptEither(other.toCompletableFuture(), action, null);
2434 +    }
2435 +
2436 +    public CompletableFuture<Void> acceptEitherAsync
2437 +        (CompletionStage<? extends T> other,
2438 +         Consumer<? super T> action) {
2439 +        return doAcceptEither(other.toCompletableFuture(), action,
2440 +                              ForkJoinPool.commonPool());
2441 +    }
2442 +
2443 +    public CompletableFuture<Void> acceptEitherAsync
2444 +        (CompletionStage<? extends T> other,
2445 +         Consumer<? super T> action,
2446 +         Executor executor) {
2447 +        if (executor == null) throw new NullPointerException();
2448 +        return doAcceptEither(other.toCompletableFuture(), action, executor);
2449 +    }
2450 +
2451 +    public CompletableFuture<Void> runAfterEither(CompletionStage<?> other,
2452 +                                                  Runnable action) {
2453 +        return doRunAfterEither(other.toCompletableFuture(), action, null);
2454 +    }
2455 +
2456 +    public CompletableFuture<Void> runAfterEitherAsync
2457 +        (CompletionStage<?> other,
2458 +         Runnable action) {
2459 +        return doRunAfterEither(other.toCompletableFuture(), action,
2460 +                                ForkJoinPool.commonPool());
2461 +    }
2462 +
2463 +    public CompletableFuture<Void> runAfterEitherAsync
2464 +        (CompletionStage<?> other,
2465 +         Runnable action,
2466 +         Executor executor) {
2467 +        if (executor == null) throw new NullPointerException();
2468 +        return doRunAfterEither(other.toCompletableFuture(), action, executor);
2469 +    }
2470 +
2471 +    public <U> CompletableFuture<U> thenCompose
2472 +        (Function<? super T, ? extends CompletionStage<U>> fn) {
2473 +        return doThenCompose(fn, null);
2474 +    }
2475 +
2476 +    public <U> CompletableFuture<U> thenComposeAsync
2477 +        (Function<? super T, ? extends CompletionStage<U>> fn) {
2478 +        return doThenCompose(fn, ForkJoinPool.commonPool());
2479 +    }
2480 +
2481 +    public <U> CompletableFuture<U> thenComposeAsync
2482 +        (Function<? super T, ? extends CompletionStage<U>> fn,
2483 +         Executor executor) {
2484 +        if (executor == null) throw new NullPointerException();
2485 +        return doThenCompose(fn, executor);
2486 +    }
2487 +
2488 +    public CompletableFuture<T> whenComplete
2489 +        (BiConsumer<? super T, ? super Throwable> action) {
2490 +        return doWhenComplete(action, null);
2491 +    }
2492 +
2493 +    public CompletableFuture<T> whenCompleteAsync
2494 +        (BiConsumer<? super T, ? super Throwable> action) {
2495 +        return doWhenComplete(action, ForkJoinPool.commonPool());
2496 +    }
2497 +
2498 +    public CompletableFuture<T> whenCompleteAsync
2499 +        (BiConsumer<? super T, ? super Throwable> action,
2500 +         Executor executor) {
2501 +        if (executor == null) throw new NullPointerException();
2502 +        return doWhenComplete(action, executor);
2503 +    }
2504 +
2505 +    public <U> CompletableFuture<U> handle
2506 +        (BiFunction<? super T, Throwable, ? extends U> fn) {
2507 +        return doHandle(fn, null);
2508 +    }
2509 +
2510 +    public <U> CompletableFuture<U> handleAsync
2511 +        (BiFunction<? super T, Throwable, ? extends U> fn) {
2512 +        return doHandle(fn, ForkJoinPool.commonPool());
2513 +    }
2514 +
2515 +    public <U> CompletableFuture<U> handleAsync
2516 +        (BiFunction<? super T, Throwable, ? extends U> fn,
2517 +         Executor executor) {
2518 +        if (executor == null) throw new NullPointerException();
2519 +        return doHandle(fn, executor);
2520 +    }
2521 +
2522 +    /**
2523 +     * Returns this CompletableFuture
2524 +     *
2525 +     * @return this CompletableFuture
2526 +     */
2527 +    public CompletableFuture<T> toCompletableFuture() {
2528 +        return this;
2529 +    }
2530 +
2531 +    // not in interface CompletionStage
2532 +
2533 +    /**
2534 +     * Returns a new CompletableFuture that is completed when this
2535 +     * CompletableFuture completes, with the result of the given
2536 +     * function of the exception triggering this CompletableFuture's
2537 +     * completion when it completes exceptionally; otherwise, if this
2538 +     * CompletableFuture completes normally, then the returned
2539 +     * CompletableFuture also completes normally with the same value.
2540 +     * Note: More flexible versions of this functionality are
2541 +     * available using methods {@code whenComplete} and {@code handle}.
2542 +     *
2543 +     * @param fn the function to use to compute the value of the
2544 +     * returned CompletableFuture if this CompletableFuture completed
2545 +     * exceptionally
2546 +     * @return the new CompletableFuture
2547 +     */
2548 +    public CompletableFuture<T> exceptionally
2549 +        (Function<Throwable, ? extends T> fn) {
2550 +        if (fn == null) throw new NullPointerException();
2551 +        CompletableFuture<T> dst = new CompletableFuture<T>();
2552 +        ExceptionCompletion<T> d = null;
2553 +        Object r;
2554 +        if ((r = result) == null) {
2555 +            CompletionNode p =
2556 +                new CompletionNode(d = new ExceptionCompletion<T>
2557 +                                   (this, fn, dst));
2558 +            while ((r = result) == null) {
2559 +                if (UNSAFE.compareAndSwapObject(this, COMPLETIONS,
2560 +                                                p.next = completions, p))
2561 +                    break;
2562 +            }
2563 +        }
2564 +        if (r != null && (d == null || d.compareAndSet(0, 1))) {
2565 +            T t = null; Throwable ex, dx = null;
2566 +            if (r instanceof AltResult) {
2567 +                if ((ex = ((AltResult)r).ex) != null) {
2568 +                    try {
2569 +                        t = fn.apply(ex);
2570 +                    } catch (Throwable rex) {
2571 +                        dx = rex;
2572 +                    }
2573 +                }
2574 +            }
2575 +            else {
2576 +                @SuppressWarnings("unchecked") T tr = (T) r;
2577 +                t = tr;
2578 +            }
2579 +            dst.internalComplete(t, dx);
2580 +        }
2581 +        helpPostComplete();
2582 +        return dst;
2583 +    }
2584 +
2585      /* ------------- Arbitrary-arity constructions -------------- */
2586  
2587      /*
# Line 3186 | Line 2834 | public class CompletableFuture<T> implem
2834      }
2835  
2836      /**
2837 +     * Returns {@code true} if this CompletableFuture completed
2838 +     * exceptionally.
2839 +     *
2840 +     * @return {@code true} if this CompletableFuture completed
2841 +     * exceptionally
2842 +     */
2843 +    public boolean isCompletedExceptionally() {
2844 +        return (result instanceof AltResult);
2845 +    }
2846 +
2847 +    /**
2848       * Forcibly sets or resets the value subsequently returned by
2849       * method {@link #get()} and related methods, whether or not
2850       * already completed. This method is designed for use only in

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines