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.103 by jsr166, Mon Apr 14 19:50:44 2014 UTC vs.
Revision 1.104 by dl, Fri May 2 16:34:00 2014 UTC

# Line 21 | Line 21 | import java.util.concurrent.TimeoutExcep
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;
24   import java.util.concurrent.locks.LockSupport;
25  
26   /**
# Line 84 | Line 83 | public class CompletableFuture<T> implem
83      /*
84       * Overview:
85       *
86 <     * 1. Non-nullness of field result (set via CAS) indicates done.
87 <     * An AltResult is used to box null as a result, as well as to
88 <     * hold exceptions.  Using a single field makes completion fast
89 <     * and simple to detect and trigger, at the expense of a lot of
90 <     * encoding and decoding that infiltrates many methods. One minor
91 <     * simplification relies on the (static) NIL (to box null results)
92 <     * being the only AltResult with a null exception field, so we
93 <     * don't usually need explicit comparisons with NIL. The CF
94 <     * exception propagation mechanics surrounding decoding rely on
95 <     * unchecked casts of decoded results really being unchecked,
96 <     * where user type errors are caught at point of use, as is
97 <     * currently the case in Java. These are highlighted by using
98 <     * SuppressWarnings-annotated temporaries.
99 <     *
100 <     * 2. Waiters are held in a Treiber stack similar to the one used
101 <     * in FutureTask, Phaser, and SynchronousQueue. See their
102 <     * internal documentation for algorithmic details.
103 <     *
104 <     * 3. Completions are also kept in a list/stack, and pulled off
105 <     * and run when completion of an observable CF is triggered. (We
106 <     * could even use the same stack as for waiters, but would give up
107 <     * the potential parallelism obtained because woken waiters help
108 <     * release/run others -- see method postComplete).  Because
109 <     * post-processing may race with direct calls, class Completion
110 <     * opportunistically extends AtomicInteger so callers can claim
111 <     * the action via compareAndSet(0, 1).  The Completion.tryComplete
112 <     * methods are all written a boringly similar uniform way (that
113 <     * sometimes includes unnecessary-looking checks, kept to maintain
114 <     * uniformity).  There are enough dimensions upon which they
115 <     * differ that attempts to factor commonalities while maintaining
116 <     * efficiency require more lines of code than they would save.
117 <     *
118 <     * 4. The exported then/and/or methods do support a bit of
119 <     * factoring (see doThenApply etc). They must cope with the
120 <     * intrinsic races surrounding addition of a dependent action
121 <     * versus performing the action directly because the task is
122 <     * already complete.  For example, a CF may not be complete upon
123 <     * entry, so a dependent completion is added, but by the time it
124 <     * is added, the target CF is complete, so must be directly
125 <     * executed. This is all done while avoiding unnecessary object
126 <     * construction in safe-bypass cases.
127 <     */
128 <
129 <    // preliminaries
130 <
131 <    static final class AltResult {
132 <        final Throwable ex; // null only for NIL
133 <        AltResult(Throwable ex) { this.ex = ex; }
86 >     * A CompletableFuture may have dependent completion actions,
87 >     * collected in a linked stack. It atomically completes by CASing
88 >     * a result field, and then pops off and runs those actions. This
89 >     * applies across normal vs exceptional outcomes, sync vs async
90 >     * actions, binary triggers, and various forms of completions.
91 >     *
92 >     * Non-nullness of field result (set via CAS) indicates done.  An
93 >     * AltResult is used to box null as a result, as well as to hold
94 >     * exceptions.  Using a single field makes completion simple to
95 >     * detect and trigger.  Encoding and decoding is straightforward
96 >     * but adds vertical sprawl. One minor simplification relies on
97 >     * the (static) NIL (to box null results) being the only AltResult
98 >     * with a null exception field, so we don't usually need explicit
99 >     * comparisons with NIL.  Exception propagation mechanics
100 >     * surrounding decoding rely on unchecked casts of decoded results
101 >     * really being unchecked, and user type errors being caught at
102 >     * point of use, as is currently the case in Java. These are
103 >     * highlighted by using SuppressWarnings annotated temporaries.
104 >     *
105 >     * Dependent actions are represented by Completion objects linked
106 >     * as Treiber stacks headed by field completions. There are four
107 >     * kinds of Completions: single-source (UniCompletion), two-source
108 >     * (BiCompletion), shared (CoBiCompletion, used by the second
109 >     * source of a BiCompletion), and Signallers that unblock waiters.
110 >     *
111 >     * The same patterns of methods and classes are used for each form
112 >     * of Completion (apply, combine, etc), and are written in a
113 >     * similar style.  For each form X there is, when applicable:
114 >     *
115 >     * * Method nowX (for example nowApply) that immediately executes
116 >     *   a supplied function and sets result
117 >     * * Class AsyncX class (for example AsyncApply) that calls nowX
118 >     *   from another task,
119 >     * * Class DelayedX (for example DelayedApply) that holds
120 >     *   arguments and calls Xnow when ready.
121 >     *
122 >     * For each public CompletionStage method M* (for example
123 >     * thenApply{Async}), there is a method doM (for example
124 >     * doThenApply) that creates and/or invokes the appropriate form.
125 >     * Each deals with three cases that can arise when adding a
126 >     * dependent completion to CompletableFuture f:
127 >     *
128 >     * * f is already complete, so the dependent action is run
129 >     *   immediately, via  a "now" method, which, if async,
130 >     *   starts the action in a new task.
131 >     * * f is not complete, so a Completion action is created and
132 >     *   pushed to f's completions. It is triggered via
133 >     *   f.postComplete when f completes.
134 >     * * f is not complete, but completes while adding the completion
135 >     *   action, so we try to trigger it upon adding (see method
136 >     *   unipush and derivatives) to cover races.
137 >     *
138 >     * Methods with two sources (for example thenCombine) must deal
139 >     * with races across both while pushing actions.  The second
140 >     * completion is an CoBiCompletion pointing to the first, shared
141 >     * to ensure that at most one claims and performs the action.  The
142 >     * multiple-arity method allOf does this pairwise to form a tree
143 >     * of completions. (Method anyOf just uses a depth-one Or tree.)
144 >     *
145 >     * Upon setting results, method postComplete is called unless
146 >     * the target is guaranteed not to be observable (i.e., not yet
147 >     * returned or linked). Multiple threads can call postComplete,
148 >     * which atomically pops each dependent action, and tries to
149 >     * trigger it via method tryAct. Any such action must be performed
150 >     * only once, even if called from several threads, so Completions
151 >     * maintain status via CAS, and on success run one of the "now"
152 >     * methods.  Triggering can propagate recursively, so tryAct
153 >     * returns a completed dependent (if one exists) for further
154 >     * processing by its caller.
155 >     *
156 >     * Blocking methods get() and join() rely on Signaller Completions
157 >     * that wake up waiting threads.  The mechanics are similar to
158 >     * Treiber stack wait-nodes used in FutureTask, Phaser, and
159 >     * SynchronousQueue. See their internal documentation for
160 >     * algorithmic details.
161 >     *
162 >     * Without precautions, CompletableFutures would be prone to
163 >     * garbage accumulation as chains of completions build up, each
164 >     * pointing back to its sources. So we detach (null out) most
165 >     * Completion fields as soon as possible.  To support this,
166 >     * internal methods check for and harmlessly ignore null arguments
167 >     * that may have been obtained during races with threads nulling
168 >     * out fields. (Some of these checked cases cannot currently
169 >     * happen.)  Fields of Async classes can be but currently are not
170 >     * fully detached, because they do not in general form cycles.
171 >     */
172 >
173 >    volatile Object result;             // Either the result or boxed AltResult
174 >    volatile Completion<?> completions; // Treiber stack of dependent actions
175 >
176 >    final boolean internalComplete(Object r) { // CAS from null to r
177 >        return UNSAFE.compareAndSwapObject(this, RESULT, null, r);
178 >    }
179 >
180 >    final boolean casCompletions(Completion<?> cmp, Completion<?> val) {
181 >        return UNSAFE.compareAndSwapObject(this, COMPLETIONS, cmp, val);
182 >    }
183 >
184 >    /* ------------- Encoding and decoding outcomes -------------- */
185 >
186 >    static final class AltResult { // See above
187 >        final Throwable ex;        // null only for NIL
188 >        AltResult(Throwable x) { this.ex = x; }
189      }
190  
191      static final AltResult NIL = new AltResult(null);
192  
139    // Fields
140
141    volatile Object result;    // Either the result or boxed AltResult
142    volatile WaitNode waiters; // Treiber stack of threads blocked on get()
143    volatile CompletionNode completions; // list (Treiber stack) of completions
144
145    // Basic utilities for triggering and processing completions
146
193      /**
194 <     * Triggers completion with the encoding of the given arguments:
195 <     * if the exception is non-null, encodes it as a wrapped
150 <     * CompletionException unless it is one already.  Otherwise uses
151 <     * the given result, boxed as NIL if null.
194 >     * Returns the encoding of the given (non-null) exception as a
195 >     * wrapped CompletionException unless it is one already.
196       */
197 <    final void internalComplete(T v, Throwable ex) {
198 <        if (result == null)
199 <            UNSAFE.compareAndSwapObject
156 <                (this, RESULT, null,
157 <                 (ex == null) ? (v == null) ? NIL : v :
158 <                 new AltResult((ex instanceof CompletionException) ? ex :
159 <                               new CompletionException(ex)));
197 >    static AltResult altThrowable(Throwable x) {
198 >        return new AltResult((x instanceof CompletionException) ? x :
199 >                             new CompletionException(x));
200      }
201  
202      /**
203 <     * Signals waiters and triggers all enabled dependent completions
204 <     * reachable from src.
205 <     *
166 <     * @param src if non-null a completed CompletableFuture
203 >     * Returns the encoding of the given arguments: if the exception
204 >     * is non-null, encodes as altThrowable.  Otherwise uses the given
205 >     * value, boxed as NIL if null.
206       */
207 <    static final void postComplete(CompletableFuture<?> src) {
208 <        /*
170 <         * CF "src" is always the base of a possible chain of
171 <         * completions that may need further processing.  To avoid
172 <         * potential StackOverflowErrors, we extend along only one
173 <         * path ("dep") at a time, holding others by pushing them on
174 <         * src's completion list, which advances tail-recursion style
175 <         * when possible.  On each step, "f" is dep if non-null, else
176 <         * src.
177 <         */
178 <        for (CompletableFuture<?> f = src, dep = null; f != null;) {
179 <            WaitNode q; CompletionNode h; Thread w;
180 <            if ((q = f.waiters) != null) {
181 <                if (UNSAFE.compareAndSwapObject(f, WAITERS, q, q.next) &&
182 <                    (w = q.thread) != null) {
183 <                    q.thread = null;
184 <                    LockSupport.unpark(w);
185 <                }
186 <            }
187 <            else if ((h = f.completions) == null) {
188 <                if (dep == null)
189 <                    break;
190 <                dep = null;
191 <                f = src;
192 <            }
193 <            else if (UNSAFE.compareAndSwapObject(f, COMPLETIONS, h, h.next)) {
194 <                Completion c; CompletableFuture<?> d;
195 <                if (dep != null && dep.completions != null) { // push to src
196 <                    do {} while (!UNSAFE.compareAndSwapObject(
197 <                                     src, COMPLETIONS,
198 <                                     h.next = src.completions, h));
199 <                }
200 <                else if ((c = h.completion) == null ||
201 <                         (d = c.tryComplete()) == null ||
202 <                         d.result == null) {
203 <                    dep = null;
204 <                    f = src;
205 <                }
206 <                else if (src.completions == null) {
207 <                    dep = null;
208 <                    f = src = d;
209 <                }
210 <                else
211 <                    f = dep = d;
212 <            }
213 <        }
207 >    static Object encodeOutcome(Object v, Throwable x) {
208 >        return (x != null) ? altThrowable(x) : (v == null) ? NIL : v;
209      }
210  
216    /* ------------- waiting for completions -------------- */
217
218    /** Number of processors, for spin control */
219    static final int NCPU = Runtime.getRuntime().availableProcessors();
220
221    /**
222     * Heuristic spin value for waitingGet() before blocking on
223     * multiprocessors
224     */
225    static final int SPINS = (NCPU > 1) ? 1 << 8 : 0;
226
211      /**
212 <     * Linked nodes to record waiting threads in a Treiber stack.  See
229 <     * other classes such as Phaser and SynchronousQueue for more
230 <     * detailed explanation. This class implements ManagedBlocker to
231 <     * avoid starvation when blocking actions pile up in
232 <     * ForkJoinPools.
212 >     * Decodes outcome to return result or throw unchecked exception
213       */
214 <    static final class WaitNode implements ForkJoinPool.ManagedBlocker {
215 <        long nanos;          // wait time if timed
216 <        final long deadline; // non-zero if timed
217 <        volatile int interruptControl; // > 0: interruptible, < 0: interrupted
218 <        volatile Thread thread;
219 <        volatile WaitNode next;
220 <        WaitNode(boolean interruptible, long nanos, long deadline) {
221 <            this.thread = Thread.currentThread();
222 <            this.interruptControl = interruptible ? 1 : 0;
223 <            this.nanos = nanos;
244 <            this.deadline = deadline;
245 <        }
246 <        public boolean isReleasable() {
247 <            if (thread == null)
248 <                return true;
249 <            if (Thread.interrupted()) {
250 <                int i = interruptControl;
251 <                interruptControl = -1;
252 <                if (i > 0)
253 <                    return true;
254 <            }
255 <            if (deadline != 0L &&
256 <                (nanos <= 0L || (nanos = deadline - System.nanoTime()) <= 0L)) {
257 <                thread = null;
258 <                return true;
259 <            }
260 <            return false;
261 <        }
262 <        public boolean block() {
263 <            if (isReleasable())
264 <                return true;
265 <            else if (deadline == 0L)
266 <                LockSupport.park(this);
267 <            else if (nanos > 0L)
268 <                LockSupport.parkNanos(this, nanos);
269 <            return isReleasable();
214 >    private static <T> T reportJoin(Object r) {
215 >        if (r instanceof AltResult) {
216 >            Throwable x;
217 >            if ((x = ((AltResult)r).ex) == null)
218 >                return null;
219 >            if (x instanceof CancellationException)
220 >                throw (CancellationException)x;
221 >            if (x instanceof CompletionException)
222 >                throw (CompletionException)x;
223 >            throw new CompletionException(x);
224          }
225 +        @SuppressWarnings("unchecked") T tr = (T) r;
226 +        return tr;
227      }
228  
229      /**
230 <     * Returns raw result after waiting, or null if interruptible and
275 <     * interrupted.
230 >     * Reports result using Future.get conventions
231       */
232 <    private Object waitingGet(boolean interruptible) {
233 <        WaitNode q = null;
234 <        boolean queued = false;
235 <        int spins = SPINS;
236 <        for (Object r;;) {
237 <            if ((r = result) != null) {
238 <                if (q != null) { // suppress unpark
284 <                    q.thread = null;
285 <                    if (q.interruptControl < 0) {
286 <                        if (interruptible) {
287 <                            removeCancelledWaiters();
288 <                            return null;
289 <                        }
290 <                        Thread.currentThread().interrupt();
291 <                    }
292 <                }
293 <                postComplete(this); // help release others
294 <                return r;
295 <            }
296 <            else if (spins > 0) {
297 <                int rnd = ThreadLocalRandom.nextSecondarySeed();
298 <                if (rnd == 0)
299 <                    rnd = ThreadLocalRandom.current().nextInt();
300 <                if (rnd >= 0)
301 <                    --spins;
302 <            }
303 <            else if (q == null)
304 <                q = new WaitNode(interruptible, 0L, 0L);
305 <            else if (!queued)
306 <                queued = UNSAFE.compareAndSwapObject(this, WAITERS,
307 <                                                     q.next = waiters, q);
308 <            else if (interruptible && q.interruptControl < 0) {
309 <                q.thread = null;
310 <                removeCancelledWaiters();
232 >    private static <T> T reportGet(Object r)
233 >        throws InterruptedException, ExecutionException {
234 >        if (r == null) // by convention below, null means interrupted
235 >            throw new InterruptedException();
236 >        if (r instanceof AltResult) {
237 >            Throwable x, cause;
238 >            if ((x = ((AltResult)r).ex) == null)
239                  return null;
240 <            }
241 <            else if (q.thread != null && result == null) {
242 <                try {
243 <                    ForkJoinPool.managedBlock(q);
244 <                } catch (InterruptedException ex) {
245 <                    q.interruptControl = -1;
318 <                }
319 <            }
240 >            if (x instanceof CancellationException)
241 >                throw (CancellationException)x;
242 >            if ((x instanceof CompletionException) &&
243 >                (cause = x.getCause()) != null)
244 >                x = cause;
245 >            throw new ExecutionException(x);
246          }
247 +        @SuppressWarnings("unchecked") T tr = (T) r;
248 +        return tr;
249      }
250  
251 +    /* ------------- Async Tasks -------------- */
252 +
253      /**
254 <     * Awaits completion or aborts on interrupt or timeout.
255 <     *
326 <     * @param nanos time to wait
327 <     * @return raw result
254 >     * Default executor -- ForkJoinPool.commonPool() unless it cannot
255 >     * support parallelism.
256       */
257 <    private Object timedAwaitDone(long nanos)
258 <        throws InterruptedException, TimeoutException {
259 <        WaitNode q = null;
332 <        boolean queued = false;
333 <        for (Object r;;) {
334 <            if ((r = result) != null) {
335 <                if (q != null) {
336 <                    q.thread = null;
337 <                    if (q.interruptControl < 0) {
338 <                        removeCancelledWaiters();
339 <                        throw new InterruptedException();
340 <                    }
341 <                }
342 <                postComplete(this);
343 <                return r;
344 <            }
345 <            else if (q == null) {
346 <                if (nanos <= 0L)
347 <                    throw new TimeoutException();
348 <                long d = System.nanoTime() + nanos;
349 <                q = new WaitNode(true, nanos, d == 0L ? 1L : d); // avoid 0
350 <            }
351 <            else if (!queued)
352 <                queued = UNSAFE.compareAndSwapObject(this, WAITERS,
353 <                                                     q.next = waiters, q);
354 <            else if (q.interruptControl < 0) {
355 <                q.thread = null;
356 <                removeCancelledWaiters();
357 <                throw new InterruptedException();
358 <            }
359 <            else if (q.nanos <= 0L) {
360 <                if (result == null) {
361 <                    q.thread = null;
362 <                    removeCancelledWaiters();
363 <                    throw new TimeoutException();
364 <                }
365 <            }
366 <            else if (q.thread != null && result == null) {
367 <                try {
368 <                    ForkJoinPool.managedBlock(q);
369 <                } catch (InterruptedException ex) {
370 <                    q.interruptControl = -1;
371 <                }
372 <            }
373 <        }
374 <    }
257 >    static final Executor asyncPool =
258 >        (ForkJoinPool.getCommonPoolParallelism() > 1) ?
259 >        ForkJoinPool.commonPool() : new ThreadPerTaskExecutor();
260  
261 <    /**
262 <     * Tries to unlink timed-out or interrupted wait nodes to avoid
263 <     * accumulating garbage.  Internal nodes are simply unspliced
379 <     * without CAS since it is harmless if they are traversed anyway
380 <     * by releasers.  To avoid effects of unsplicing from already
381 <     * removed nodes, the list is retraversed in case of an apparent
382 <     * race.  This is slow when there are a lot of nodes, but we don't
383 <     * expect lists to be long enough to outweigh higher-overhead
384 <     * schemes.
385 <     */
386 <    private void removeCancelledWaiters() {
387 <        retry: for (;;) {          // restart on removeWaiter race
388 <            for (WaitNode pred = null, q = waiters, s; q != null; q = s) {
389 <                s = q.next;
390 <                if (q.thread != null)
391 <                    pred = q;
392 <                else if (pred != null) {
393 <                    pred.next = s;
394 <                    if (pred.thread == null) // check for race
395 <                        continue retry;
396 <                }
397 <                else if (!UNSAFE.compareAndSwapObject(this, WAITERS, q, s))
398 <                    continue retry;
399 <            }
400 <            break;
401 <        }
261 >    /** Fallback if ForkJoinPool.commonPool() cannot support parallelism */
262 >    static final class ThreadPerTaskExecutor implements Executor {
263 >        public void execute(Runnable r) { new Thread(r).start(); }
264      }
265  
404    /* ------------- Async tasks -------------- */
405
266      /**
267       * A marker interface identifying asynchronous tasks produced by
268       * {@code async} methods. This may be useful for monitoring,
# Line 413 | Line 273 | public class CompletableFuture<T> implem
273      public static interface AsynchronousCompletionTask {
274      }
275  
276 <    /** Base class can act as either FJ or plain Runnable */
277 <    @SuppressWarnings("serial")
278 <    abstract static class Async extends ForkJoinTask<Void>
276 >    /**
277 >     * Base class for tasks that can act as either FJ or plain
278 >     * Runnables. Abstract method compute calls an associated "now"
279 >     * method.  Method exec calls compute if its CompletableFuture is
280 >     * not already done, and runs completions if done. Fields are not
281 >     * in general final and can be nulled out after use (but most
282 >     * currently are not).  Classes include serialVersionUIDs even
283 >     * though they are currently never serialized.
284 >     */
285 >    abstract static class Async<T> extends ForkJoinTask<Void>
286          implements Runnable, AsynchronousCompletionTask {
287 +        CompletableFuture<T> dep; // the CompletableFuture to trigger
288 +        Async(CompletableFuture<T> dep) { this.dep = dep; }
289 +
290 +        abstract void compute(); // call the associated "now" method
291 +
292 +        public final boolean exec() {
293 +            CompletableFuture<T> d;
294 +            if ((d = dep) != null) {
295 +                if (d.result == null) // suppress if cancelled
296 +                    compute();
297 +                if (d.result != null)
298 +                    d.postComplete();
299 +                dep = null; // detach
300 +            }
301 +            return true;
302 +        }
303          public final Void getRawResult() { return null; }
304          public final void setRawResult(Void v) { }
305          public final void run() { exec(); }
306 +        private static final long serialVersionUID = 5232453952276885070L;
307      }
308  
309 <    /**
310 <     * Starts the given async task using the given executor, unless
311 <     * the executor is ForkJoinPool.commonPool and it has been
312 <     * disabled, in which case starts a new thread.
313 <     */
314 <    static void execAsync(Executor e, Async r) {
315 <        if (e == ForkJoinPool.commonPool() &&
316 <            ForkJoinPool.getCommonPoolParallelism() <= 1)
317 <            new Thread(r).start();
318 <        else
435 <            e.execute(r);
309 >    /* ------------- Completions -------------- */
310 >
311 >    static abstract class Completion<T> { // See above
312 >        volatile Completion<?> next;      // Treiber stack link
313 >
314 >        /**
315 >         * Performs completion action if enabled, returning a
316 >         * completed dependent Completablefuture, if one exists.
317 >         */
318 >        abstract CompletableFuture<?> tryAct();
319      }
320  
321 <    static final class AsyncRun extends Async {
322 <        final Runnable fn;
323 <        final CompletableFuture<Void> dst;
324 <        AsyncRun(Runnable fn, CompletableFuture<Void> dst) {
325 <            this.fn = fn; this.dst = dst;
326 <        }
327 <        public final boolean exec() {
328 <            CompletableFuture<Void> d; Throwable ex;
329 <            if ((d = this.dst) != null && d.result == null) {
330 <                try {
331 <                    fn.run();
332 <                    ex = null;
333 <                } catch (Throwable rex) {
334 <                    ex = rex;
321 >    /**
322 >     * Triggers all reaachble enabled dependents.  Call only when
323 >     * known to be done.
324 >     */
325 >    final void postComplete() {
326 >        /*
327 >         * On each step, variable f holds current completions to pop
328 >         * and run.  It is extended along only one path at a time,
329 >         * pushing others to avoid StackOverflowErrors on recursion.
330 >         */
331 >        CompletableFuture<?> f = this; Completion<?> h;
332 >        while ((h = f.completions) != null ||
333 >               (f != this && (h = (f = this).completions) != null)) {
334 >            CompletableFuture<?> d; Completion<?> t;
335 >            if (f.casCompletions(h, t = h.next)) {
336 >                if (t != null) {
337 >                    if (f != this) {  // push
338 >                        do {} while (!casCompletions(h.next = completions, h));
339 >                        continue;
340 >                    }
341 >                    h.next = null;    // detach
342                  }
343 <                d.internalComplete(null, ex);
343 >                f = (d = h.tryAct()) == null ? this : d;
344              }
455            postComplete(d);
456            return true;
345          }
458        private static final long serialVersionUID = 5232453952276885070L;
346      }
347  
348 <    static final class AsyncSupply<U> extends Async {
349 <        final Supplier<U> fn;
350 <        final CompletableFuture<U> dst;
351 <        AsyncSupply(Supplier<U> fn, CompletableFuture<U> dst) {
352 <            this.fn = fn; this.dst = dst;
353 <        }
354 <        public final boolean exec() {
355 <            CompletableFuture<U> d; U u; Throwable ex;
356 <            if ((d = this.dst) != null && d.result == null) {
357 <                try {
358 <                    u = fn.get();
359 <                    ex = null;
360 <                } catch (Throwable rex) {
361 <                    ex = rex;
362 <                    u = null;
363 <                }
477 <                d.internalComplete(u, ex);
478 <            }
479 <            postComplete(d);
480 <            return true;
348 >    /* ------------- One-source Completions -------------- */
349 >
350 >    /**
351 >     * A Completion with a source and dependent.  The "dep" field acts
352 >     * as a claim, nulled out to disable further attempts to
353 >     * trigger. Fields can only be observed by other threads upon
354 >     * successful push; and should be nulled out after claim.
355 >     */
356 >    static abstract class UniCompletion<T> extends Completion<T> {
357 >        Executor async;                    // executor to use (null if none)
358 >        CompletableFuture<T> dep;          // the dependent to complete
359 >        CompletableFuture<?> src;          // source of value for tryAct
360 >
361 >        UniCompletion(Executor async, CompletableFuture<T> dep,
362 >                      CompletableFuture<?> src) {
363 >            this.async = async; this.dep = dep; this.src = src;
364          }
482        private static final long serialVersionUID = 5232453952276885070L;
483    }
365  
366 <    static final class AsyncApply<T,U> extends Async {
367 <        final T arg;
368 <        final Function<? super T,? extends U> fn;
488 <        final CompletableFuture<U> dst;
489 <        AsyncApply(T arg, Function<? super T,? extends U> fn,
490 <                   CompletableFuture<U> dst) {
491 <            this.arg = arg; this.fn = fn; this.dst = dst;
492 <        }
493 <        public final boolean exec() {
494 <            CompletableFuture<U> d; U u; Throwable ex;
495 <            if ((d = this.dst) != null && d.result == null) {
496 <                try {
497 <                    u = fn.apply(arg);
498 <                    ex = null;
499 <                } catch (Throwable rex) {
500 <                    ex = rex;
501 <                    u = null;
502 <                }
503 <                d.internalComplete(u, ex);
504 <            }
505 <            postComplete(d);
506 <            return true;
366 >        /** Tries to claim completion action by CASing dep to null */
367 >        final boolean claim(CompletableFuture<T> d) {
368 >            return UNSAFE.compareAndSwapObject(this, DEP, d, null);
369          }
508        private static final long serialVersionUID = 5232453952276885070L;
509    }
370  
371 <    static final class AsyncCombine<T,U,V> extends Async {
372 <        final T arg1;
373 <        final U arg2;
374 <        final BiFunction<? super T,? super U,? extends V> fn;
375 <        final CompletableFuture<V> dst;
376 <        AsyncCombine(T arg1, U arg2,
377 <                     BiFunction<? super T,? super U,? extends V> fn,
378 <                     CompletableFuture<V> dst) {
379 <            this.arg1 = arg1; this.arg2 = arg2; this.fn = fn; this.dst = dst;
380 <        }
521 <        public final boolean exec() {
522 <            CompletableFuture<V> d; V v; Throwable ex;
523 <            if ((d = this.dst) != null && d.result == null) {
524 <                try {
525 <                    v = fn.apply(arg1, arg2);
526 <                    ex = null;
527 <                } catch (Throwable rex) {
528 <                    ex = rex;
529 <                    v = null;
530 <                }
531 <                d.internalComplete(v, ex);
371 >        private static final sun.misc.Unsafe UNSAFE;
372 >        private static final long DEP;
373 >        static {
374 >            try {
375 >                UNSAFE = sun.misc.Unsafe.getUnsafe();
376 >                Class<?> k = UniCompletion.class;
377 >                DEP = UNSAFE.objectFieldOffset
378 >                    (k.getDeclaredField("dep"));
379 >            } catch (Exception x) {
380 >                throw new Error(x);
381              }
533            postComplete(d);
534            return true;
382          }
536        private static final long serialVersionUID = 5232453952276885070L;
383      }
384  
385 <    static final class AsyncAccept<T> extends Async {
386 <        final T arg;
387 <        final Consumer<? super T> fn;
388 <        final CompletableFuture<?> dst;
389 <        AsyncAccept(T arg, Consumer<? super T> fn,
390 <                    CompletableFuture<?> dst) {
391 <            this.arg = arg; this.fn = fn; this.dst = dst;
392 <        }
393 <        public final boolean exec() {
394 <            CompletableFuture<?> d; Throwable ex;
549 <            if ((d = this.dst) != null && d.result == null) {
550 <                try {
551 <                    fn.accept(arg);
552 <                    ex = null;
553 <                } catch (Throwable rex) {
554 <                    ex = rex;
555 <                }
556 <                d.internalComplete(null, ex);
557 <            }
558 <            postComplete(d);
559 <            return true;
385 >    /** Pushes c on to completions, and triggers c if done. */
386 >    private void unipush(UniCompletion<?> c) {
387 >        if (c != null) {
388 >            CompletableFuture<?> d;
389 >            while (result == null && !casCompletions(c.next = completions, c))
390 >                c.next = null;            // clear on CAS failure
391 >            if ((d = c.tryAct()) != null) // cover races
392 >                d.postComplete();
393 >            if (result != null)           // clean stack
394 >                postComplete();
395          }
561        private static final long serialVersionUID = 5232453952276885070L;
396      }
397  
398 <    static final class AsyncAcceptBoth<T,U> extends Async {
399 <        final T arg1;
400 <        final U arg2;
401 <        final BiConsumer<? super T,? super U> fn;
402 <        final CompletableFuture<?> dst;
403 <        AsyncAcceptBoth(T arg1, U arg2,
404 <                        BiConsumer<? super T,? super U> fn,
405 <                        CompletableFuture<?> dst) {
406 <            this.arg1 = arg1; this.arg2 = arg2; this.fn = fn; this.dst = dst;
407 <        }
408 <        public final boolean exec() {
409 <            CompletableFuture<?> d; Throwable ex;
410 <            if ((d = this.dst) != null && d.result == null) {
398 >    // Immediate, async, delayed, and routing support for Function/apply
399 >
400 >    static <T,U> void nowApply(Executor e, CompletableFuture<U> d, Object r,
401 >                               Function<? super T,? extends U> f) {
402 >        if (d != null && f != null) {
403 >            T t; U u; Throwable x;
404 >            if (r instanceof AltResult) {
405 >                t = null;
406 >                x = ((AltResult)r).ex;
407 >            }
408 >            else {
409 >                @SuppressWarnings("unchecked") T tr = (T) r; t = tr;
410 >                x = null;
411 >            }
412 >            if (x == null) {
413                  try {
414 <                    fn.accept(arg1, arg2);
415 <                    ex = null;
416 <                } catch (Throwable rex) {
417 <                    ex = rex;
414 >                    if (e != null) {
415 >                        e.execute(new AsyncApply<T,U>(d, t, f));
416 >                        return;
417 >                    }
418 >                    u = f.apply(t);
419 >                } catch (Throwable ex) {
420 >                    x = ex;
421 >                    u = null;
422                  }
583                d.internalComplete(null, ex);
423              }
424 <            postComplete(d);
425 <            return true;
424 >            else
425 >                u = null;
426 >            d.internalComplete(encodeOutcome(u, x));
427          }
588        private static final long serialVersionUID = 5232453952276885070L;
428      }
429  
430 <    static final class AsyncCompose<T,U> extends Async {
431 <        final T arg;
432 <        final Function<? super T, ? extends CompletionStage<U>> fn;
433 <        final CompletableFuture<U> dst;
434 <        AsyncCompose(T arg,
596 <                     Function<? super T, ? extends CompletionStage<U>> fn,
597 <                     CompletableFuture<U> dst) {
598 <            this.arg = arg; this.fn = fn; this.dst = dst;
599 <        }
600 <        public final boolean exec() {
601 <            CompletableFuture<U> d, fr; U u; Throwable ex;
602 <            if ((d = this.dst) != null && d.result == null) {
603 <                try {
604 <                    CompletionStage<U> cs = fn.apply(arg);
605 <                    fr = (cs == null) ? null : cs.toCompletableFuture();
606 <                    ex = (fr == null) ? new NullPointerException() : null;
607 <                } catch (Throwable rex) {
608 <                    ex = rex;
609 <                    fr = null;
610 <                }
611 <                if (ex != null)
612 <                    u = null;
613 <                else {
614 <                    Object r = fr.result;
615 <                    if (r == null)
616 <                        r = fr.waitingGet(false);
617 <                    if (r instanceof AltResult) {
618 <                        ex = ((AltResult)r).ex;
619 <                        u = null;
620 <                    }
621 <                    else {
622 <                        @SuppressWarnings("unchecked") U ur = (U) r;
623 <                        u = ur;
624 <                    }
625 <                }
626 <                d.internalComplete(u, ex);
627 <            }
628 <            postComplete(d);
629 <            return true;
430 >    static final class AsyncApply<T,U> extends Async<U> {
431 >        T arg;  Function<? super T,? extends U> fn;
432 >        AsyncApply(CompletableFuture<U> dep, T arg,
433 >                   Function<? super T,? extends U> fn) {
434 >            super(dep); this.arg = arg; this.fn = fn;
435          }
436 +        final void compute() { nowApply(null, dep, arg, fn); }
437          private static final long serialVersionUID = 5232453952276885070L;
438      }
439  
440 <    static final class AsyncWhenComplete<T> extends Async {
441 <        final T arg1;
442 <        final Throwable arg2;
443 <        final BiConsumer<? super T,? super Throwable> fn;
444 <        final CompletableFuture<T> dst;
445 <        AsyncWhenComplete(T arg1, Throwable arg2,
640 <                          BiConsumer<? super T,? super Throwable> fn,
641 <                          CompletableFuture<T> dst) {
642 <            this.arg1 = arg1; this.arg2 = arg2; this.fn = fn; this.dst = dst;
440 >    static final class DelayedApply<T,U> extends UniCompletion<U> {
441 >        Function<? super T,? extends U> fn;
442 >        DelayedApply(Executor async, CompletableFuture<U> dep,
443 >                     CompletableFuture<?> src,
444 >                     Function<? super T,? extends U> fn) {
445 >            super(async, dep, src); this.fn = fn;
446          }
447 <        public final boolean exec() {
448 <            CompletableFuture<T> d;
449 <            if ((d = this.dst) != null && d.result == null) {
450 <                Throwable ex = arg2;
451 <                try {
452 <                    fn.accept(arg1, ex);
453 <                } catch (Throwable rex) {
651 <                    if (ex == null)
652 <                        ex = rex;
653 <                }
654 <                d.internalComplete(arg1, ex);
447 >        final CompletableFuture<?> tryAct() {
448 >            CompletableFuture<U> d; CompletableFuture<?> a; Object r;
449 >            if ((d = dep) != null && (a = src) != null &&
450 >                (r = a.result) != null && claim(d)) {
451 >                nowApply(async, d, r, fn);
452 >                src = null; fn = null;
453 >                if (d.result != null) return d;
454              }
455 <            postComplete(d);
657 <            return true;
455 >            return null;
456          }
659        private static final long serialVersionUID = 5232453952276885070L;
457      }
458  
459 <    /* ------------- Completions -------------- */
460 <
461 <    /**
462 <     * Simple linked list nodes to record completions, used in
463 <     * basically the same way as WaitNodes. (We separate nodes from
464 <     * the Completions themselves mainly because for the And and Or
465 <     * methods, the same Completion object resides in two lists.)
466 <     */
467 <    static final class CompletionNode {
468 <        final Completion completion;
672 <        volatile CompletionNode next;
673 <        CompletionNode(Completion completion) { this.completion = completion; }
459 >    private <U> CompletableFuture<U> doThenApply(
460 >        Function<? super T,? extends U> fn, Executor e) {
461 >        if (fn == null) throw new NullPointerException();
462 >        CompletableFuture<U> d = new CompletableFuture<U>();
463 >        Object r = result;
464 >        if (r == null)
465 >            unipush(new DelayedApply<T,U>(e, d, this, fn));
466 >        else
467 >            nowApply(e, d, r, fn);
468 >        return d;
469      }
470  
471 <    // Opportunistically subclass AtomicInteger to use compareAndSet to claim.
677 <    @SuppressWarnings("serial")
678 <    abstract static class Completion extends AtomicInteger {
679 <        /**
680 <         * Completes a dependent Completablefuture if enabled.
681 <         * @return the dependent Completablefuture
682 <         */
683 <        public abstract CompletableFuture<?> tryComplete();
684 <    }
471 >    // Consumer/accept
472  
473 <    static final class ThenApply<T,U> extends Completion {
474 <        final CompletableFuture<? extends T> src;
475 <        final Function<? super T,? extends U> fn;
476 <        final CompletableFuture<U> dst;
477 <        final Executor executor;
478 <        ThenApply(CompletableFuture<? extends T> src,
479 <                  Function<? super T,? extends U> fn,
480 <                  CompletableFuture<U> dst,
481 <                  Executor executor) {
482 <            this.src = src; this.fn = fn; this.dst = dst;
483 <            this.executor = executor;
484 <        }
485 <        public final CompletableFuture<?> tryComplete() {
486 <            final CompletableFuture<? extends T> a;
487 <            final Function<? super T,? extends U> fn;
488 <            final CompletableFuture<U> dst;
489 <            Object r; T t; Throwable ex;
703 <            if ((dst = this.dst) != null &&
704 <                (fn = this.fn) != null &&
705 <                (a = this.src) != null &&
706 <                (r = a.result) != null &&
707 <                get() == 0 && compareAndSet(0, 1)) {
708 <                if (r instanceof AltResult) {
709 <                    ex = ((AltResult)r).ex;
710 <                    t = null;
711 <                }
712 <                else {
713 <                    ex = null;
714 <                    @SuppressWarnings("unchecked") T tr = (T) r;
715 <                    t = tr;
716 <                }
717 <                Executor e = executor;
718 <                U u = null;
719 <                if (ex == null) {
720 <                    try {
721 <                        if (e != null)
722 <                            execAsync(e, new AsyncApply<T,U>(t, fn, dst));
723 <                        else
724 <                            u = fn.apply(t);
725 <                    } catch (Throwable rex) {
726 <                        ex = rex;
473 >    static <T,U> void nowAccept(Executor e, CompletableFuture<U> d,
474 >                                Object r, Consumer<? super T> f) {
475 >        if (d != null && f != null) {
476 >            T t; Throwable x;
477 >            if (r instanceof AltResult) {
478 >                t = null;
479 >                x = ((AltResult)r).ex;
480 >            }
481 >            else {
482 >                @SuppressWarnings("unchecked") T tr = (T) r; t = tr;
483 >                x = null;
484 >            }
485 >            if (x == null) {
486 >                try {
487 >                    if (e != null) {
488 >                        e.execute(new AsyncAccept<T,U>(d, t, f));
489 >                        return;
490                      }
491 +                    f.accept(t);
492 +                } catch (Throwable ex) {
493 +                    x = ex;
494                  }
729                if (e == null || ex != null)
730                    dst.internalComplete(u, ex);
495              }
496 <            return dst;
496 >            d.internalComplete(encodeOutcome(null, x));
497          }
734        private static final long serialVersionUID = 5232453952276885070L;
498      }
499  
500 <    static final class ThenAccept<T> extends Completion {
501 <        final CompletableFuture<? extends T> src;
502 <        final Consumer<? super T> fn;
503 <        final CompletableFuture<?> dst;
504 <        final Executor executor;
742 <        ThenAccept(CompletableFuture<? extends T> src,
743 <                   Consumer<? super T> fn,
744 <                   CompletableFuture<?> dst,
745 <                   Executor executor) {
746 <            this.src = src; this.fn = fn; this.dst = dst;
747 <            this.executor = executor;
748 <        }
749 <        public final CompletableFuture<?> tryComplete() {
750 <            final CompletableFuture<? extends T> a;
751 <            final Consumer<? super T> fn;
752 <            final CompletableFuture<?> dst;
753 <            Object r; T t; Throwable ex;
754 <            if ((dst = this.dst) != null &&
755 <                (fn = this.fn) != null &&
756 <                (a = this.src) != null &&
757 <                (r = a.result) != null &&
758 <                get() == 0 && compareAndSet(0, 1)) {
759 <                if (r instanceof AltResult) {
760 <                    ex = ((AltResult)r).ex;
761 <                    t = null;
762 <                }
763 <                else {
764 <                    ex = null;
765 <                    @SuppressWarnings("unchecked") T tr = (T) r;
766 <                    t = tr;
767 <                }
768 <                Executor e = executor;
769 <                if (ex == null) {
770 <                    try {
771 <                        if (e != null)
772 <                            execAsync(e, new AsyncAccept<T>(t, fn, dst));
773 <                        else
774 <                            fn.accept(t);
775 <                    } catch (Throwable rex) {
776 <                        ex = rex;
777 <                    }
778 <                }
779 <                if (e == null || ex != null)
780 <                    dst.internalComplete(null, ex);
781 <            }
782 <            return dst;
500 >    static final class AsyncAccept<T,U> extends Async<U> {
501 >        T arg; Consumer<? super T> fn;
502 >        AsyncAccept(CompletableFuture<U> dep, T arg,
503 >                    Consumer<? super T> fn) {
504 >            super(dep); this.arg = arg; this.fn = fn;
505          }
506 +        final void compute() { nowAccept(null, dep, arg, fn); }
507          private static final long serialVersionUID = 5232453952276885070L;
508      }
509  
510 <    static final class ThenRun extends Completion {
511 <        final CompletableFuture<?> src;
512 <        final Runnable fn;
513 <        final CompletableFuture<Void> dst;
514 <        final Executor executor;
515 <        ThenRun(CompletableFuture<?> src,
516 <                Runnable fn,
517 <                CompletableFuture<Void> dst,
518 <                Executor executor) {
519 <            this.src = src; this.fn = fn; this.dst = dst;
520 <            this.executor = executor;
521 <        }
522 <        public final CompletableFuture<?> tryComplete() {
800 <            final CompletableFuture<?> a;
801 <            final Runnable fn;
802 <            final CompletableFuture<Void> dst;
803 <            Object r; Throwable ex;
804 <            if ((dst = this.dst) != null &&
805 <                (fn = this.fn) != null &&
806 <                (a = this.src) != null &&
807 <                (r = a.result) != null &&
808 <                get() == 0 && compareAndSet(0, 1)) {
809 <                if (r instanceof AltResult)
810 <                    ex = ((AltResult)r).ex;
811 <                else
812 <                    ex = null;
813 <                Executor e = executor;
814 <                if (ex == null) {
815 <                    try {
816 <                        if (e != null)
817 <                            execAsync(e, new AsyncRun(fn, dst));
818 <                        else
819 <                            fn.run();
820 <                    } catch (Throwable rex) {
821 <                        ex = rex;
822 <                    }
823 <                }
824 <                if (e == null || ex != null)
825 <                    dst.internalComplete(null, ex);
510 >    static final class DelayedAccept<T> extends UniCompletion<Void> {
511 >        Consumer<? super T> fn;
512 >        DelayedAccept(Executor async, CompletableFuture<Void> dep,
513 >                      CompletableFuture<?> src, Consumer<? super T> fn) {
514 >            super(async, dep, src); this.fn = fn;
515 >        }
516 >        final CompletableFuture<?> tryAct() {
517 >            CompletableFuture<Void> d; CompletableFuture<?> a; Object r;
518 >            if ((d = dep) != null && (a = src) != null &&
519 >                (r = a.result) != null && claim(d)) {
520 >                nowAccept(async, d, r, fn);
521 >                src = null; fn = null;
522 >                if (d.result != null) return d;
523              }
524 <            return dst;
524 >            return null;
525          }
829        private static final long serialVersionUID = 5232453952276885070L;
526      }
527  
528 <    static final class ThenCombine<T,U,V> extends Completion {
529 <        final CompletableFuture<? extends T> src;
530 <        final CompletableFuture<? extends U> snd;
531 <        final BiFunction<? super T,? super U,? extends V> fn;
532 <        final CompletableFuture<V> dst;
533 <        final Executor executor;
534 <        ThenCombine(CompletableFuture<? extends T> src,
535 <                    CompletableFuture<? extends U> snd,
536 <                    BiFunction<? super T,? super U,? extends V> fn,
537 <                    CompletableFuture<V> dst,
538 <                    Executor executor) {
539 <            this.src = src; this.snd = snd;
540 <            this.fn = fn; this.dst = dst;
541 <            this.executor = executor;
542 <        }
543 <        public final CompletableFuture<?> tryComplete() {
544 <            final CompletableFuture<? extends T> a;
545 <            final CompletableFuture<? extends U> b;
546 <            final BiFunction<? super T,? super U,? extends V> fn;
547 <            final CompletableFuture<V> dst;
548 <            Object r, s; T t; U u; Throwable ex;
549 <            if ((dst = this.dst) != null &&
550 <                (fn = this.fn) != null &&
855 <                (a = this.src) != null &&
856 <                (r = a.result) != null &&
857 <                (b = this.snd) != null &&
858 <                (s = b.result) != null &&
859 <                get() == 0 && compareAndSet(0, 1)) {
860 <                if (r instanceof AltResult) {
861 <                    ex = ((AltResult)r).ex;
862 <                    t = null;
863 <                }
864 <                else {
865 <                    ex = null;
866 <                    @SuppressWarnings("unchecked") T tr = (T) r;
867 <                    t = tr;
868 <                }
869 <                if (ex != null)
870 <                    u = null;
871 <                else if (s instanceof AltResult) {
872 <                    ex = ((AltResult)s).ex;
873 <                    u = null;
874 <                }
875 <                else {
876 <                    @SuppressWarnings("unchecked") U us = (U) s;
877 <                    u = us;
878 <                }
879 <                Executor e = executor;
880 <                V v = null;
881 <                if (ex == null) {
882 <                    try {
883 <                        if (e != null)
884 <                            execAsync(e, new AsyncCombine<T,U,V>(t, u, fn, dst));
885 <                        else
886 <                            v = fn.apply(t, u);
887 <                    } catch (Throwable rex) {
888 <                        ex = rex;
528 >    private CompletableFuture<Void> doThenAccept(Consumer<? super T> fn,
529 >                                                 Executor e) {
530 >        if (fn == null) throw new NullPointerException();
531 >        CompletableFuture<Void> d = new CompletableFuture<Void>();
532 >        Object r = result;
533 >        if (r == null)
534 >            unipush(new DelayedAccept<T>(e, d, this, fn));
535 >        else
536 >            nowAccept(e, d, r, fn);
537 >        return d;
538 >    }
539 >
540 >    // Runnable/run
541 >
542 >    static <T> void nowRun(Executor e, CompletableFuture<T> d, Object r,
543 >                           Runnable f) {
544 >        if (d != null && f != null) {
545 >            Throwable x = (r instanceof AltResult) ? ((AltResult)r).ex : null;
546 >            if (x == null) {
547 >                try {
548 >                    if (e != null) {
549 >                        e.execute(new AsyncRun<T>(d, f));
550 >                        return;
551                      }
552 +                    f.run();
553 +                } catch (Throwable ex) {
554 +                    x = ex;
555                  }
891                if (e == null || ex != null)
892                    dst.internalComplete(v, ex);
556              }
557 <            return dst;
557 >            d.internalComplete(encodeOutcome(null, x));
558          }
896        private static final long serialVersionUID = 5232453952276885070L;
559      }
560  
561 <    static final class ThenAcceptBoth<T,U> extends Completion {
562 <        final CompletableFuture<? extends T> src;
563 <        final CompletableFuture<? extends U> snd;
564 <        final BiConsumer<? super T,? super U> fn;
903 <        final CompletableFuture<Void> dst;
904 <        final Executor executor;
905 <        ThenAcceptBoth(CompletableFuture<? extends T> src,
906 <                       CompletableFuture<? extends U> snd,
907 <                       BiConsumer<? super T,? super U> fn,
908 <                       CompletableFuture<Void> dst,
909 <                       Executor executor) {
910 <            this.src = src; this.snd = snd;
911 <            this.fn = fn; this.dst = dst;
912 <            this.executor = executor;
913 <        }
914 <        public final CompletableFuture<?> tryComplete() {
915 <            final CompletableFuture<? extends T> a;
916 <            final CompletableFuture<? extends U> b;
917 <            final BiConsumer<? super T,? super U> fn;
918 <            final CompletableFuture<Void> dst;
919 <            Object r, s; T t; U u; Throwable ex;
920 <            if ((dst = this.dst) != null &&
921 <                (fn = this.fn) != null &&
922 <                (a = this.src) != null &&
923 <                (r = a.result) != null &&
924 <                (b = this.snd) != null &&
925 <                (s = b.result) != null &&
926 <                get() == 0 && compareAndSet(0, 1)) {
927 <                if (r instanceof AltResult) {
928 <                    ex = ((AltResult)r).ex;
929 <                    t = null;
930 <                }
931 <                else {
932 <                    ex = null;
933 <                    @SuppressWarnings("unchecked") T tr = (T) r;
934 <                    t = tr;
935 <                }
936 <                if (ex != null)
937 <                    u = null;
938 <                else if (s instanceof AltResult) {
939 <                    ex = ((AltResult)s).ex;
940 <                    u = null;
941 <                }
942 <                else {
943 <                    @SuppressWarnings("unchecked") U us = (U) s;
944 <                    u = us;
945 <                }
946 <                Executor e = executor;
947 <                if (ex == null) {
948 <                    try {
949 <                        if (e != null)
950 <                            execAsync(e, new AsyncAcceptBoth<T,U>(t, u, fn, dst));
951 <                        else
952 <                            fn.accept(t, u);
953 <                    } catch (Throwable rex) {
954 <                        ex = rex;
955 <                    }
956 <                }
957 <                if (e == null || ex != null)
958 <                    dst.internalComplete(null, ex);
959 <            }
960 <            return dst;
561 >    static final class AsyncRun<T> extends Async<T> {
562 >        Runnable fn;
563 >        AsyncRun(CompletableFuture<T> dep, Runnable fn) {
564 >            super(dep); this.fn = fn;
565          }
566 +        final void compute() { nowRun(null, dep, null, fn); }
567          private static final long serialVersionUID = 5232453952276885070L;
568      }
569  
570 <    static final class RunAfterBoth extends Completion {
571 <        final CompletableFuture<?> src;
572 <        final CompletableFuture<?> snd;
573 <        final Runnable fn;
574 <        final CompletableFuture<Void> dst;
575 <        final Executor executor;
576 <        RunAfterBoth(CompletableFuture<?> src,
577 <                     CompletableFuture<?> snd,
578 <                     Runnable fn,
579 <                     CompletableFuture<Void> dst,
580 <                     Executor executor) {
581 <            this.src = src; this.snd = snd;
582 <            this.fn = fn; this.dst = dst;
978 <            this.executor = executor;
979 <        }
980 <        public final CompletableFuture<?> tryComplete() {
981 <            final CompletableFuture<?> a;
982 <            final CompletableFuture<?> b;
983 <            final Runnable fn;
984 <            final CompletableFuture<Void> dst;
985 <            Object r, s; Throwable ex;
986 <            if ((dst = this.dst) != null &&
987 <                (fn = this.fn) != null &&
988 <                (a = this.src) != null &&
989 <                (r = a.result) != null &&
990 <                (b = this.snd) != null &&
991 <                (s = b.result) != null &&
992 <                get() == 0 && compareAndSet(0, 1)) {
993 <                if (r instanceof AltResult)
994 <                    ex = ((AltResult)r).ex;
995 <                else
996 <                    ex = null;
997 <                if (ex == null && (s instanceof AltResult))
998 <                    ex = ((AltResult)s).ex;
999 <                Executor e = executor;
1000 <                if (ex == null) {
1001 <                    try {
1002 <                        if (e != null)
1003 <                            execAsync(e, new AsyncRun(fn, dst));
1004 <                        else
1005 <                            fn.run();
1006 <                    } catch (Throwable rex) {
1007 <                        ex = rex;
1008 <                    }
1009 <                }
1010 <                if (e == null || ex != null)
1011 <                    dst.internalComplete(null, ex);
570 >    static final class DelayedRun extends UniCompletion<Void> {
571 >        Runnable fn;
572 >        DelayedRun(Executor async, CompletableFuture<Void> dep,
573 >                   CompletableFuture<?> src, Runnable fn) {
574 >            super(async, dep, src); this.fn = fn;
575 >        }
576 >        final CompletableFuture<?> tryAct() {
577 >            CompletableFuture<Void> d; CompletableFuture<?> a; Object r;
578 >            if ((d = dep) != null && (a = src) != null &&
579 >                (r = a.result) != null && claim(d)) {
580 >                nowRun(async, d, r, fn);
581 >                src = null; fn = null; // clear refs
582 >                if (d.result != null) return d;
583              }
584 <            return dst;
584 >            return null;
585          }
1015        private static final long serialVersionUID = 5232453952276885070L;
586      }
587  
588 <    static final class AndCompletion extends Completion {
589 <        final CompletableFuture<?> src;
590 <        final CompletableFuture<?> snd;
591 <        final CompletableFuture<Void> dst;
592 <        AndCompletion(CompletableFuture<?> src,
593 <                      CompletableFuture<?> snd,
594 <                      CompletableFuture<Void> dst) {
595 <            this.src = src; this.snd = snd; this.dst = dst;
596 <        }
597 <        public final CompletableFuture<?> tryComplete() {
598 <            final CompletableFuture<?> a;
599 <            final CompletableFuture<?> b;
600 <            final CompletableFuture<Void> dst;
601 <            Object r, s; Throwable ex;
602 <            if ((dst = this.dst) != null &&
603 <                (a = this.src) != null &&
604 <                (r = a.result) != null &&
605 <                (b = this.snd) != null &&
606 <                (s = b.result) != null &&
607 <                get() == 0 && compareAndSet(0, 1)) {
608 <                if (r instanceof AltResult)
609 <                    ex = ((AltResult)r).ex;
1040 <                else
1041 <                    ex = null;
1042 <                if (ex == null && (s instanceof AltResult))
1043 <                    ex = ((AltResult)s).ex;
1044 <                dst.internalComplete(null, ex);
588 >    private CompletableFuture<Void> doThenRun(Runnable fn, Executor e) {
589 >        if (fn == null) throw new NullPointerException();
590 >        CompletableFuture<Void> d = new CompletableFuture<Void>();
591 >        Object r = result;
592 >        if (r == null)
593 >            unipush(new DelayedRun(e, d, this, fn));
594 >        else
595 >            nowRun(e, d, r, fn);
596 >        return d;
597 >    }
598 >
599 >    // Supplier/get
600 >
601 >    static <T> void nowSupply(CompletableFuture<T> d, Supplier<T> f) {
602 >        if (d != null && f != null) {
603 >            T t; Throwable x;
604 >            try {
605 >                t = f.get();
606 >                x = null;
607 >            } catch (Throwable ex) {
608 >                x = ex;
609 >                t = null;
610              }
611 <            return dst;
611 >            d.internalComplete(encodeOutcome(t, x));
612          }
1048        private static final long serialVersionUID = 5232453952276885070L;
613      }
614  
615 <    static final class ApplyToEither<T,U> extends Completion {
616 <        final CompletableFuture<? extends T> src;
617 <        final CompletableFuture<? extends T> snd;
618 <        final Function<? super T,? extends U> fn;
1055 <        final CompletableFuture<U> dst;
1056 <        final Executor executor;
1057 <        ApplyToEither(CompletableFuture<? extends T> src,
1058 <                      CompletableFuture<? extends T> snd,
1059 <                      Function<? super T,? extends U> fn,
1060 <                      CompletableFuture<U> dst,
1061 <                      Executor executor) {
1062 <            this.src = src; this.snd = snd;
1063 <            this.fn = fn; this.dst = dst;
1064 <            this.executor = executor;
1065 <        }
1066 <        public final CompletableFuture<?> tryComplete() {
1067 <            final CompletableFuture<? extends T> a;
1068 <            final CompletableFuture<? extends T> b;
1069 <            final Function<? super T,? extends U> fn;
1070 <            final CompletableFuture<U> dst;
1071 <            Object r; T t; Throwable ex;
1072 <            if ((dst = this.dst) != null &&
1073 <                (fn = this.fn) != null &&
1074 <                (((a = this.src) != null && (r = a.result) != null) ||
1075 <                 ((b = this.snd) != null && (r = b.result) != null)) &&
1076 <                get() == 0 && compareAndSet(0, 1)) {
1077 <                if (r instanceof AltResult) {
1078 <                    ex = ((AltResult)r).ex;
1079 <                    t = null;
1080 <                }
1081 <                else {
1082 <                    ex = null;
1083 <                    @SuppressWarnings("unchecked") T tr = (T) r;
1084 <                    t = tr;
1085 <                }
1086 <                Executor e = executor;
1087 <                U u = null;
1088 <                if (ex == null) {
1089 <                    try {
1090 <                        if (e != null)
1091 <                            execAsync(e, new AsyncApply<T,U>(t, fn, dst));
1092 <                        else
1093 <                            u = fn.apply(t);
1094 <                    } catch (Throwable rex) {
1095 <                        ex = rex;
1096 <                    }
1097 <                }
1098 <                if (e == null || ex != null)
1099 <                    dst.internalComplete(u, ex);
1100 <            }
1101 <            return dst;
615 >    static final class AsyncSupply<T> extends Async<T> {
616 >        Supplier<T> fn;
617 >        AsyncSupply(CompletableFuture<T> dep, Supplier<T> fn) {
618 >            super(dep); this.fn = fn;
619          }
620 +        final void compute() { nowSupply(dep, fn); }
621          private static final long serialVersionUID = 5232453952276885070L;
622      }
623  
624 <    static final class AcceptEither<T> extends Completion {
625 <        final CompletableFuture<? extends T> src;
626 <        final CompletableFuture<? extends T> snd;
627 <        final Consumer<? super T> fn;
628 <        final CompletableFuture<Void> dst;
629 <        final Executor executor;
630 <        AcceptEither(CompletableFuture<? extends T> src,
631 <                     CompletableFuture<? extends T> snd,
632 <                     Consumer<? super T> fn,
633 <                     CompletableFuture<Void> dst,
634 <                     Executor executor) {
635 <            this.src = src; this.snd = snd;
636 <            this.fn = fn; this.dst = dst;
637 <            this.executor = executor;
638 <        }
639 <        public final CompletableFuture<?> tryComplete() {
640 <            final CompletableFuture<? extends T> a;
641 <            final CompletableFuture<? extends T> b;
1124 <            final Consumer<? super T> fn;
1125 <            final CompletableFuture<Void> dst;
1126 <            Object r; T t; Throwable ex;
1127 <            if ((dst = this.dst) != null &&
1128 <                (fn = this.fn) != null &&
1129 <                (((a = this.src) != null && (r = a.result) != null) ||
1130 <                 ((b = this.snd) != null && (r = b.result) != null)) &&
1131 <                get() == 0 && compareAndSet(0, 1)) {
1132 <                if (r instanceof AltResult) {
1133 <                    ex = ((AltResult)r).ex;
1134 <                    t = null;
1135 <                }
1136 <                else {
1137 <                    ex = null;
1138 <                    @SuppressWarnings("unchecked") T tr = (T) r;
1139 <                    t = tr;
1140 <                }
1141 <                Executor e = executor;
1142 <                if (ex == null) {
1143 <                    try {
1144 <                        if (e != null)
1145 <                            execAsync(e, new AsyncAccept<T>(t, fn, dst));
1146 <                        else
1147 <                            fn.accept(t);
1148 <                    } catch (Throwable rex) {
1149 <                        ex = rex;
1150 <                    }
624 >    // WhenComplete
625 >
626 >    static <T> void nowWhen(Executor e, CompletableFuture<T> d, Object r,
627 >                            BiConsumer<? super T,? super Throwable> f) {
628 >        if (d != null && f != null) {
629 >            T t; Throwable x, dx;
630 >            if (r instanceof AltResult) {
631 >                t = null;
632 >                x = ((AltResult)r).ex;
633 >            }
634 >            else {
635 >                @SuppressWarnings("unchecked") T tr = (T) r; t = tr;
636 >                x = null;
637 >            }
638 >            try {
639 >                if (e != null) {
640 >                    e.execute(new AsyncWhen<T>(d, r, f));
641 >                    return;
642                  }
643 <                if (e == null || ex != null)
644 <                    dst.internalComplete(null, ex);
643 >                f.accept(t, x);
644 >                dx = null;
645 >            } catch (Throwable ex) {
646 >                dx = ex;
647              }
648 <            return dst;
648 >            d.internalComplete(encodeOutcome(t, x != null ? x : dx));
649          }
1157        private static final long serialVersionUID = 5232453952276885070L;
650      }
651  
652 <    static final class RunAfterEither extends Completion {
653 <        final CompletableFuture<?> src;
654 <        final CompletableFuture<?> snd;
655 <        final Runnable fn;
656 <        final CompletableFuture<Void> dst;
1165 <        final Executor executor;
1166 <        RunAfterEither(CompletableFuture<?> src,
1167 <                       CompletableFuture<?> snd,
1168 <                       Runnable fn,
1169 <                       CompletableFuture<Void> dst,
1170 <                       Executor executor) {
1171 <            this.src = src; this.snd = snd;
1172 <            this.fn = fn; this.dst = dst;
1173 <            this.executor = executor;
1174 <        }
1175 <        public final CompletableFuture<?> tryComplete() {
1176 <            final CompletableFuture<?> a;
1177 <            final CompletableFuture<?> b;
1178 <            final Runnable fn;
1179 <            final CompletableFuture<Void> dst;
1180 <            Object r; Throwable ex;
1181 <            if ((dst = this.dst) != null &&
1182 <                (fn = this.fn) != null &&
1183 <                (((a = this.src) != null && (r = a.result) != null) ||
1184 <                 ((b = this.snd) != null && (r = b.result) != null)) &&
1185 <                get() == 0 && compareAndSet(0, 1)) {
1186 <                if (r instanceof AltResult)
1187 <                    ex = ((AltResult)r).ex;
1188 <                else
1189 <                    ex = null;
1190 <                Executor e = executor;
1191 <                if (ex == null) {
1192 <                    try {
1193 <                        if (e != null)
1194 <                            execAsync(e, new AsyncRun(fn, dst));
1195 <                        else
1196 <                            fn.run();
1197 <                    } catch (Throwable rex) {
1198 <                        ex = rex;
1199 <                    }
1200 <                }
1201 <                if (e == null || ex != null)
1202 <                    dst.internalComplete(null, ex);
1203 <            }
1204 <            return dst;
652 >    static final class AsyncWhen<T> extends Async<T> {
653 >        Object arg; BiConsumer<? super T,? super Throwable> fn;
654 >        AsyncWhen(CompletableFuture<T> dep, Object arg,
655 >                  BiConsumer<? super T,? super Throwable> fn) {
656 >            super(dep); this.arg = arg; this.fn = fn;
657          }
658 +        final void compute() { nowWhen(null, dep, arg, fn); }
659          private static final long serialVersionUID = 5232453952276885070L;
660      }
661  
662 <    static final class OrCompletion extends Completion {
663 <        final CompletableFuture<?> src;
664 <        final CompletableFuture<?> snd;
665 <        final CompletableFuture<Object> dst;
666 <        OrCompletion(CompletableFuture<?> src,
667 <                     CompletableFuture<?> snd,
668 <                     CompletableFuture<Object> dst) {
669 <            this.src = src; this.snd = snd; this.dst = dst;
670 <        }
671 <        public final CompletableFuture<?> tryComplete() {
672 <            final CompletableFuture<?> a;
673 <            final CompletableFuture<?> b;
674 <            final CompletableFuture<Object> dst;
675 <            Object r, t; Throwable ex;
1223 <            if ((dst = this.dst) != null &&
1224 <                (((a = this.src) != null && (r = a.result) != null) ||
1225 <                 ((b = this.snd) != null && (r = b.result) != null)) &&
1226 <                get() == 0 && compareAndSet(0, 1)) {
1227 <                if (r instanceof AltResult) {
1228 <                    ex = ((AltResult)r).ex;
1229 <                    t = null;
1230 <                }
1231 <                else {
1232 <                    ex = null;
1233 <                    t = r;
1234 <                }
1235 <                dst.internalComplete(t, ex);
662 >    static final class DelayedWhen<T> extends UniCompletion<T> {
663 >        BiConsumer<? super T, ? super Throwable> fn;
664 >        DelayedWhen(Executor async, CompletableFuture<T> dep,
665 >                    CompletableFuture<?> src,
666 >                    BiConsumer<? super T, ? super Throwable> fn) {
667 >            super(async, dep, src); this.fn = fn;
668 >        }
669 >        final CompletableFuture<?> tryAct() {
670 >            CompletableFuture<T> d; CompletableFuture<?> a; Object r;
671 >            if ((d = dep) != null && (a = src) != null &&
672 >                (r = a.result) != null && claim(d)) {
673 >                nowWhen(async, d, r, fn);
674 >                src = null; fn = null;
675 >                if (d.result != null) return d;
676              }
677 <            return dst;
677 >            return null;
678          }
1239        private static final long serialVersionUID = 5232453952276885070L;
679      }
680  
681 <    static final class ExceptionCompletion<T> extends Completion {
682 <        final CompletableFuture<? extends T> src;
683 <        final Function<? super Throwable, ? extends T> fn;
684 <        final CompletableFuture<T> dst;
685 <        ExceptionCompletion(CompletableFuture<? extends T> src,
686 <                            Function<? super Throwable, ? extends T> fn,
687 <                            CompletableFuture<T> dst) {
688 <            this.src = src; this.fn = fn; this.dst = dst;
689 <        }
690 <        public final CompletableFuture<?> tryComplete() {
691 <            final CompletableFuture<? extends T> a;
692 <            final Function<? super Throwable, ? extends T> fn;
693 <            final CompletableFuture<T> dst;
694 <            Object r; T t = null; Throwable ex, dx = null;
695 <            if ((dst = this.dst) != null &&
696 <                (fn = this.fn) != null &&
697 <                (a = this.src) != null &&
698 <                (r = a.result) != null &&
699 <                get() == 0 && compareAndSet(0, 1)) {
700 <                if ((r instanceof AltResult) &&
701 <                    (ex = ((AltResult)r).ex) != null) {
702 <                    try {
703 <                        t = fn.apply(ex);
704 <                    } catch (Throwable rex) {
705 <                        dx = rex;
706 <                    }
707 <                }
708 <                else {
709 <                    @SuppressWarnings("unchecked") T tr = (T) r;
710 <                    t = tr;
681 >    private CompletableFuture<T> doWhenComplete(
682 >        BiConsumer<? super T, ? super Throwable> fn, Executor e) {
683 >        if (fn == null) throw new NullPointerException();
684 >        CompletableFuture<T> d = new CompletableFuture<T>();
685 >        Object r = result;
686 >        if (r == null)
687 >            unipush(new DelayedWhen<T>(e, d, this, fn));
688 >        else
689 >            nowWhen(e, d, r, fn);
690 >        return d;
691 >    }
692 >
693 >    // Handle
694 >
695 >    static <T,U> void nowHandle(Executor e, CompletableFuture<U> d, Object r,
696 >                                BiFunction<? super T, Throwable, ? extends U> f) {
697 >        if (d != null && f != null) {
698 >            T t; U u; Throwable x, dx;
699 >            if (r instanceof AltResult) {
700 >                t = null;
701 >                x = ((AltResult)r).ex;
702 >            }
703 >            else {
704 >                @SuppressWarnings("unchecked") T tr = (T) r; t = tr;
705 >                x = null;
706 >            }
707 >            try {
708 >                if (e != null) {
709 >                    e.execute(new AsyncCombine<T,Throwable,U>(d, t, x, f));
710 >                    return;
711                  }
712 <                dst.internalComplete(t, dx);
712 >                u = f.apply(t, x);
713 >                dx = null;
714 >            } catch (Throwable ex) {
715 >                dx = ex;
716 >                u = null;
717              }
718 <            return dst;
718 >            d.internalComplete(encodeOutcome(u, dx));
719          }
1277        private static final long serialVersionUID = 5232453952276885070L;
720      }
721  
722 <    static final class WhenCompleteCompletion<T> extends Completion {
723 <        final CompletableFuture<? extends T> src;
724 <        final BiConsumer<? super T, ? super Throwable> fn;
725 <        final CompletableFuture<T> dst;
726 <        final Executor executor;
727 <        WhenCompleteCompletion(CompletableFuture<? extends T> src,
728 <                               BiConsumer<? super T, ? super Throwable> fn,
729 <                               CompletableFuture<T> dst,
730 <                               Executor executor) {
731 <            this.src = src; this.fn = fn; this.dst = dst;
732 <            this.executor = executor;
733 <        }
734 <        public final CompletableFuture<?> tryComplete() {
735 <            final CompletableFuture<? extends T> a;
1294 <            final BiConsumer<? super T, ? super Throwable> fn;
1295 <            final CompletableFuture<T> dst;
1296 <            Object r; T t; Throwable ex;
1297 <            if ((dst = this.dst) != null &&
1298 <                (fn = this.fn) != null &&
1299 <                (a = this.src) != null &&
1300 <                (r = a.result) != null &&
1301 <                get() == 0 && compareAndSet(0, 1)) {
1302 <                if (r instanceof AltResult) {
1303 <                    ex = ((AltResult)r).ex;
1304 <                    t = null;
1305 <                }
1306 <                else {
1307 <                    ex = null;
1308 <                    @SuppressWarnings("unchecked") T tr = (T) r;
1309 <                    t = tr;
1310 <                }
1311 <                Executor e = executor;
1312 <                Throwable dx = null;
1313 <                try {
1314 <                    if (e != null)
1315 <                        execAsync(e, new AsyncWhenComplete<T>(t, ex, fn, dst));
1316 <                    else
1317 <                        fn.accept(t, ex);
1318 <                } catch (Throwable rex) {
1319 <                    dx = rex;
1320 <                }
1321 <                if (e == null || dx != null)
1322 <                    dst.internalComplete(t, ex != null ? ex : dx);
722 >    static final class DelayedHandle<T,U> extends UniCompletion<U> {
723 >        BiFunction<? super T, Throwable, ? extends U> fn;
724 >        DelayedHandle(Executor async, CompletableFuture<U> dep,
725 >                      CompletableFuture<?> src,
726 >                      BiFunction<? super T, Throwable, ? extends U> fn) {
727 >            super(async, dep, src); this.fn = fn;
728 >        }
729 >        final CompletableFuture<?> tryAct() {
730 >            CompletableFuture<U> d; CompletableFuture<?> a; Object r;
731 >            if ((d = dep) != null && (a = src) != null &&
732 >                (r = a.result) != null && claim(d)) {
733 >                nowHandle(async, d, r, fn);
734 >                src = null; fn = null;
735 >                if (d.result != null) return d;
736              }
737 <            return dst;
737 >            return null;
738          }
1326        private static final long serialVersionUID = 5232453952276885070L;
739      }
740  
741 <    static final class ThenCopy<T> extends Completion {
742 <        final CompletableFuture<?> src;
743 <        final CompletableFuture<T> dst;
744 <        ThenCopy(CompletableFuture<?> src,
745 <                 CompletableFuture<T> dst) {
746 <            this.src = src; this.dst = dst;
747 <        }
748 <        public final CompletableFuture<?> tryComplete() {
749 <            final CompletableFuture<?> a;
750 <            final CompletableFuture<T> dst;
751 <            Object r; T t; Throwable ex;
752 <            if ((dst = this.dst) != null &&
753 <                (a = this.src) != null &&
754 <                (r = a.result) != null &&
755 <                get() == 0 && compareAndSet(0, 1)) {
756 <                if (r instanceof AltResult) {
757 <                    ex = ((AltResult)r).ex;
741 >    private <U> CompletableFuture<U> doHandle(
742 >        BiFunction<? super T, Throwable, ? extends U> fn,
743 >        Executor e) {
744 >        if (fn == null) throw new NullPointerException();
745 >        CompletableFuture<U> d = new CompletableFuture<U>();
746 >        Object r = result;
747 >        if (r == null)
748 >            unipush(new DelayedHandle<T,U>(e, d, this, fn));
749 >        else
750 >            nowHandle(e, d, r, fn);
751 >        return d;
752 >    }
753 >
754 >    // Exceptionally
755 >
756 >    static <T> void nowExceptionally(CompletableFuture<T> d, Object r,
757 >                                     Function<? super Throwable, ? extends T> f) {
758 >        if (d != null && f != null) {
759 >            T t; Throwable x, dx;
760 >            if ((r instanceof AltResult) && (x = ((AltResult)r).ex) != null) {
761 >                try {
762 >                    t = f.apply(x);
763 >                    dx = null;
764 >                } catch (Throwable ex) {
765 >                    dx = ex;
766                      t = null;
767                  }
1348                else {
1349                    ex = null;
1350                    @SuppressWarnings("unchecked") T tr = (T) r;
1351                    t = tr;
1352                }
1353                dst.internalComplete(t, ex);
768              }
769 <            return dst;
769 >            else {
770 >                @SuppressWarnings("unchecked") T tr = (T) r; t = tr;
771 >                dx = null;
772 >            }
773 >            d.internalComplete(encodeOutcome(t, dx));
774          }
1357        private static final long serialVersionUID = 5232453952276885070L;
775      }
776  
777 <    // version of ThenCopy for CompletableFuture<Void> dst
778 <    static final class ThenPropagate extends Completion {
779 <        final CompletableFuture<?> src;
780 <        final CompletableFuture<Void> dst;
781 <        ThenPropagate(CompletableFuture<?> src,
782 <                      CompletableFuture<Void> dst) {
783 <            this.src = src; this.dst = dst;
784 <        }
785 <        public final CompletableFuture<?> tryComplete() {
786 <            final CompletableFuture<?> a;
787 <            final CompletableFuture<Void> dst;
788 <            Object r; Throwable ex;
789 <            if ((dst = this.dst) != null &&
1373 <                (a = this.src) != null &&
1374 <                (r = a.result) != null &&
1375 <                get() == 0 && compareAndSet(0, 1)) {
1376 <                if (r instanceof AltResult)
1377 <                    ex = ((AltResult)r).ex;
1378 <                else
1379 <                    ex = null;
1380 <                dst.internalComplete(null, ex);
777 >    static final class DelayedExceptionally<T> extends UniCompletion<T> {
778 >        Function<? super Throwable, ? extends T> fn;
779 >        DelayedExceptionally(CompletableFuture<T> dep, CompletableFuture<?> src,
780 >                             Function<? super Throwable, ? extends T> fn) {
781 >            super(null, dep, src); this.fn = fn;
782 >        }
783 >        final CompletableFuture<?> tryAct() {
784 >            CompletableFuture<T> d; CompletableFuture<?> a; Object r;
785 >            if ((d = dep) != null && (a = src) != null &&
786 >                (r = a.result) != null && claim(d)) {
787 >                nowExceptionally(d, r, fn);
788 >                src = null; fn = null;
789 >                if (d.result != null) return d;
790              }
791 <            return dst;
791 >            return null;
792          }
1384        private static final long serialVersionUID = 5232453952276885070L;
793      }
794  
795 <    static final class HandleCompletion<T,U> extends Completion {
796 <        final CompletableFuture<? extends T> src;
797 <        final BiFunction<? super T, Throwable, ? extends U> fn;
798 <        final CompletableFuture<U> dst;
799 <        final Executor executor;
800 <        HandleCompletion(CompletableFuture<? extends T> src,
801 <                         BiFunction<? super T, Throwable, ? extends U> fn,
802 <                         CompletableFuture<U> dst,
803 <                         Executor executor) {
804 <            this.src = src; this.fn = fn; this.dst = dst;
805 <            this.executor = executor;
806 <        }
807 <        public final CompletableFuture<?> tryComplete() {
808 <            final CompletableFuture<? extends T> a;
809 <            final BiFunction<? super T, Throwable, ? extends U> fn;
810 <            final CompletableFuture<U> dst;
811 <            Object r; T t; Throwable ex;
812 <            if ((dst = this.dst) != null &&
813 <                (fn = this.fn) != null &&
814 <                (a = this.src) != null &&
815 <                (r = a.result) != null &&
1408 <                get() == 0 && compareAndSet(0, 1)) {
1409 <                if (r instanceof AltResult) {
1410 <                    ex = ((AltResult)r).ex;
1411 <                    t = null;
1412 <                }
1413 <                else {
1414 <                    ex = null;
1415 <                    @SuppressWarnings("unchecked") T tr = (T) r;
1416 <                    t = tr;
1417 <                }
1418 <                Executor e = executor;
1419 <                U u = null;
1420 <                Throwable dx = null;
1421 <                try {
1422 <                    if (e != null)
1423 <                        execAsync(e, new AsyncCombine<T,Throwable,U>(t, ex, fn, dst));
1424 <                    else
1425 <                        u = fn.apply(t, ex);
1426 <                } catch (Throwable rex) {
1427 <                    dx = rex;
1428 <                }
1429 <                if (e == null || dx != null)
1430 <                    dst.internalComplete(u, dx);
1431 <            }
1432 <            return dst;
795 >    private CompletableFuture<T> doExceptionally(
796 >        Function<Throwable, ? extends T> fn) {
797 >        if (fn == null) throw new NullPointerException();
798 >        CompletableFuture<T> d = new CompletableFuture<T>();
799 >        Object r = result;
800 >        if (r == null)
801 >            unipush(new DelayedExceptionally<T>(d, this, fn));
802 >        else
803 >            nowExceptionally(d, r, fn);
804 >        return d;
805 >    }
806 >
807 >    // Identity function used by nowCompose and anyOf
808 >
809 >    static <T> void nowCopy(CompletableFuture<T> d, Object r) {
810 >        if (d != null && d.result == null) {
811 >            Throwable x;
812 >            d.internalComplete(((r instanceof AltResult) &&
813 >                                (x = ((AltResult)r).ex) != null &&
814 >                                !(x instanceof CompletionException)) ?
815 >                               new AltResult(new CompletionException(x)): r);
816          }
1434        private static final long serialVersionUID = 5232453952276885070L;
817      }
818  
819 <    static final class ThenCompose<T,U> extends Completion {
820 <        final CompletableFuture<? extends T> src;
821 <        final Function<? super T, ? extends CompletionStage<U>> fn;
822 <        final CompletableFuture<U> dst;
823 <        final Executor executor;
824 <        ThenCompose(CompletableFuture<? extends T> src,
825 <                    Function<? super T, ? extends CompletionStage<U>> fn,
826 <                    CompletableFuture<U> dst,
827 <                    Executor executor) {
828 <            this.src = src; this.fn = fn; this.dst = dst;
829 <            this.executor = executor;
1448 <        }
1449 <        public final CompletableFuture<?> tryComplete() {
1450 <            final CompletableFuture<? extends T> a;
1451 <            final Function<? super T, ? extends CompletionStage<U>> fn;
1452 <            final CompletableFuture<U> dst;
1453 <            Object r; T t; Throwable ex; Executor e;
1454 <            if ((dst = this.dst) != null &&
1455 <                (fn = this.fn) != null &&
1456 <                (a = this.src) != null &&
1457 <                (r = a.result) != null &&
1458 <                get() == 0 && compareAndSet(0, 1)) {
1459 <                if (r instanceof AltResult) {
1460 <                    ex = ((AltResult)r).ex;
1461 <                    t = null;
1462 <                }
1463 <                else {
1464 <                    ex = null;
1465 <                    @SuppressWarnings("unchecked") T tr = (T) r;
1466 <                    t = tr;
1467 <                }
1468 <                CompletableFuture<U> c = null;
1469 <                U u = null;
1470 <                boolean complete = false;
1471 <                if (ex == null) {
1472 <                    if ((e = executor) != null)
1473 <                        execAsync(e, new AsyncCompose<T,U>(t, fn, dst));
1474 <                    else {
1475 <                        try {
1476 <                            CompletionStage<U> cs = fn.apply(t);
1477 <                            c = (cs == null) ? null : cs.toCompletableFuture();
1478 <                            if (c == null)
1479 <                                ex = new NullPointerException();
1480 <                        } catch (Throwable rex) {
1481 <                            ex = rex;
1482 <                        }
1483 <                    }
1484 <                }
1485 <                if (c != null) {
1486 <                    Object s;
1487 <                    if ((s = c.result) == null) {
1488 <                        ThenCopy<U> d = new ThenCopy<U>(c, dst);
1489 <                        CompletionNode p = new CompletionNode(d);
1490 <                        while ((s = c.result) == null) {
1491 <                            if (UNSAFE.compareAndSwapObject
1492 <                                (c, COMPLETIONS, p.next = c.completions, p))
1493 <                                break;
1494 <                        }
1495 <                        d.tryComplete();
1496 <                    }
1497 <                    else {
1498 <                        complete = true;
1499 <                        if (s instanceof AltResult) {
1500 <                            ex = ((AltResult)s).ex;  // no rewrap
1501 <                            u = null;
1502 <                        }
1503 <                        else {
1504 <                            @SuppressWarnings("unchecked") U us = (U) s;
1505 <                            u = us;
1506 <                        }
1507 <                    }
1508 <                }
1509 <                if (complete || ex != null)
1510 <                    dst.internalComplete(u, ex);
819 >    static final class DelayedCopy<T> extends UniCompletion<T> {
820 >        DelayedCopy(CompletableFuture<T> dep, CompletableFuture<?> src) {
821 >            super(null, dep, src);
822 >        }
823 >        final CompletableFuture<?> tryAct() {
824 >            CompletableFuture<T> d; CompletableFuture<?> a; Object r;
825 >            if ((d = dep) != null && (a = src) != null &&
826 >                (r = a.result) != null && claim(d)) {
827 >                nowCopy(d, r);
828 >                src = null;
829 >                if (d.result != null) return d;
830              }
831 <            return dst;
831 >            return null;
832          }
1514        private static final long serialVersionUID = 5232453952276885070L;
833      }
834  
835 <    // Implementations of stage methods with (plain, async, Executor) forms
835 >    // Compose
836  
837 <    private <U> CompletableFuture<U> doThenApply
838 <        (Function<? super T,? extends U> fn,
839 <         Executor e) {
840 <        if (fn == null) throw new NullPointerException();
1523 <        CompletableFuture<U> dst = new CompletableFuture<U>();
1524 <        Object r;
1525 <        if ((r = result) == null) {
1526 <            ThenApply<T,U> d = new ThenApply<T,U>(this, fn, dst, e);
1527 <            CompletionNode p = new CompletionNode(d);
1528 <            while (result == null) {
1529 <                if (UNSAFE.compareAndSwapObject
1530 <                    (this, COMPLETIONS, p.next = completions, p))
1531 <                    break;
1532 <            }
1533 <            d.tryComplete();
1534 <        }
1535 <        else {
1536 <            T t; Throwable ex;
837 >    static <T,U> void nowCompose(Executor e, CompletableFuture<U> d, Object r,
838 >                                 Function<? super T, ? extends CompletionStage<U>> f) {
839 >        if (d != null && f != null) {
840 >            T t; Throwable x;
841              if (r instanceof AltResult) {
1538                ex = ((AltResult)r).ex;
842                  t = null;
843 +                x = ((AltResult)r).ex;
844              }
845              else {
846 <                ex = null;
847 <                @SuppressWarnings("unchecked") T tr = (T) r;
1544 <                t = tr;
846 >                @SuppressWarnings("unchecked") T tr = (T) r; t = tr;
847 >                x = null;
848              }
849 <            U u = null;
1547 <            if (ex == null) {
849 >            if (x == null) {
850                  try {
851                      if (e != null)
852 <                        execAsync(e, new AsyncApply<T,U>(t, fn, dst));
853 <                    else
854 <                        u = fn.apply(t);
855 <                } catch (Throwable rex) {
856 <                    ex = rex;
852 >                        e.execute(new AsyncCompose<T,U>(d, t, f));
853 >                    else {
854 >                        CompletableFuture<U> c =
855 >                            f.apply(t).toCompletableFuture();
856 >                        Object s = c.result;
857 >                        if (s == null)
858 >                            c.unipush(new DelayedCopy<U>(d, c));
859 >                        else
860 >                            nowCopy(d, s);
861 >                    }
862 >                    return;
863 >                } catch (Throwable ex) {
864 >                    x = ex;
865                  }
866              }
867 <            if (e == null || ex != null)
1558 <                dst.internalComplete(u, ex);
867 >            d.internalComplete(encodeOutcome(null, x));
868          }
1560        return dst;
869      }
870  
871 <    private CompletableFuture<Void> doThenAccept(Consumer<? super T> fn,
872 <                                                 Executor e) {
873 <        if (fn == null) throw new NullPointerException();
874 <        CompletableFuture<Void> dst = new CompletableFuture<Void>();
875 <        Object r;
876 <        if ((r = result) == null) {
877 <            ThenAccept<T> d = new ThenAccept<T>(this, fn, dst, e);
878 <            CompletionNode p = new CompletionNode(d);
879 <            while (result == null) {
880 <                if (UNSAFE.compareAndSwapObject
881 <                    (this, COMPLETIONS, p.next = completions, p))
882 <                    break;
871 >    static final class AsyncCompose<T,U> extends Async<U> {
872 >        T arg; Function<? super T, ? extends CompletionStage<U>> fn;
873 >        AsyncCompose(CompletableFuture<U> dep, T arg,
874 >                     Function<? super T, ? extends CompletionStage<U>> fn) {
875 >            super(dep); this.arg = arg; this.fn = fn;
876 >        }
877 >        final void compute() { nowCompose(null, dep, arg, fn); }
878 >        private static final long serialVersionUID = 5232453952276885070L;
879 >    }
880 >
881 >    static final class DelayedCompose<T,U> extends UniCompletion<U> {
882 >        Function<? super T, ? extends CompletionStage<U>> fn;
883 >        DelayedCompose(Executor async, CompletableFuture<U> dep,
884 >                       CompletableFuture<?> src,
885 >                       Function<? super T, ? extends CompletionStage<U>> fn) {
886 >            super(async, dep, src); this.fn = fn;
887 >        }
888 >        final CompletableFuture<?> tryAct() {
889 >            CompletableFuture<U> d; CompletableFuture<?> a; Object r;
890 >            if ((d = dep) != null && (a = src) != null &&
891 >                (r = a.result) != null && claim(d)) {
892 >                nowCompose(async, d, r, fn);
893 >                src = null; fn = null;
894 >                if (d.result != null) return d;
895              }
896 <            d.tryComplete();
896 >            return null;
897          }
898 <        else {
899 <            T t; Throwable ex;
898 >    }
899 >
900 >    private <U> CompletableFuture<U> doThenCompose(
901 >        Function<? super T, ? extends CompletionStage<U>> fn, Executor e) {
902 >        if (fn == null) throw new NullPointerException();
903 >        Object r = result;
904 >        if (r == null || e != null) {
905 >            CompletableFuture<U> d = new CompletableFuture<U>();
906 >            if (r == null)
907 >                unipush(new DelayedCompose<T,U>(e, d, this, fn));
908 >            else
909 >                nowCompose(e, d, r, fn);
910 >            return d;
911 >        }
912 >        else { // try to return function result
913 >            T t; Throwable x;
914              if (r instanceof AltResult) {
1581                ex = ((AltResult)r).ex;
915                  t = null;
916 +                x = ((AltResult)r).ex;
917              }
918              else {
919 <                ex = null;
920 <                @SuppressWarnings("unchecked") T tr = (T) r;
1587 <                t = tr;
919 >                @SuppressWarnings("unchecked") T tr = (T) r; t = tr;
920 >                x = null;
921              }
922 <            if (ex == null) {
922 >            if (x == null) {
923                  try {
924 <                    if (e != null)
925 <                        execAsync(e, new AsyncAccept<T>(t, fn, dst));
926 <                    else
1594 <                        fn.accept(t);
1595 <                } catch (Throwable rex) {
1596 <                    ex = rex;
924 >                    return fn.apply(t).toCompletableFuture();
925 >                } catch (Throwable ex) {
926 >                    x = ex;
927                  }
928              }
929 <            if (e == null || ex != null)
930 <                dst.internalComplete(null, ex);
929 >            CompletableFuture<U> d = new CompletableFuture<U>();
930 >            d.result = encodeOutcome(null, x);
931 >            return d;
932          }
1602        return dst;
933      }
934  
935 <    private CompletableFuture<Void> doThenRun(Runnable action,
936 <                                              Executor e) {
937 <        if (action == null) throw new NullPointerException();
938 <        CompletableFuture<Void> dst = new CompletableFuture<Void>();
939 <        Object r;
940 <        if ((r = result) == null) {
941 <            ThenRun d = new ThenRun(this, action, dst, e);
942 <            CompletionNode p = new CompletionNode(d);
1613 <            while (result == null) {
1614 <                if (UNSAFE.compareAndSwapObject
1615 <                    (this, COMPLETIONS, p.next = completions, p))
1616 <                    break;
1617 <            }
1618 <            d.tryComplete();
935 >    /* ------------- Two-source Completions -------------- */
936 >
937 >    /** A Completion with two sources */
938 >    static abstract class BiCompletion<T> extends UniCompletion<T> {
939 >        CompletableFuture<?> snd; // second source for tryAct
940 >        BiCompletion(Executor async, CompletableFuture<T> dep,
941 >                     CompletableFuture<?> src, CompletableFuture<?> snd) {
942 >            super(async, dep, src); this.snd = snd;
943          }
944 <        else {
945 <            Throwable ex;
946 <            if (r instanceof AltResult)
947 <                ex = ((AltResult)r).ex;
948 <            else
949 <                ex = null;
950 <            if (ex == null) {
951 <                try {
952 <                    if (e != null)
953 <                        execAsync(e, new AsyncRun(action, dst));
954 <                    else
1631 <                        action.run();
1632 <                } catch (Throwable rex) {
1633 <                    ex = rex;
1634 <                }
1635 <            }
1636 <            if (e == null || ex != null)
1637 <                dst.internalComplete(null, ex);
944 >    }
945 >
946 >    /** A Completion delegating to a shared BiCompletion */
947 >    static final class CoBiCompletion<T> extends Completion<T> {
948 >        BiCompletion<T> completion;
949 >        CoBiCompletion(BiCompletion<T> completion) {
950 >            this.completion = completion;
951 >        }
952 >        final CompletableFuture<?> tryAct() {
953 >            BiCompletion<T> c;
954 >            return (c = completion) == null ? null : c.tryAct();
955          }
1639        return dst;
956      }
957  
958 <    private <U,V> CompletableFuture<V> doThenCombine
959 <        (CompletableFuture<? extends U> other,
960 <         BiFunction<? super T,? super U,? extends V> fn,
961 <         Executor e) {
962 <        if (other == null || fn == null) throw new NullPointerException();
963 <        CompletableFuture<V> dst = new CompletableFuture<V>();
964 <        Object r = result, s = other.result;
965 <        if (r == null || s == null) {
966 <            ThenCombine<T,U,V> d =
967 <                new ThenCombine<T,U,V>(this, other, fn, dst, e);
968 <            CompletionNode q = null, p = new CompletionNode(d);
969 <            while ((r == null && (r = result) == null) ||
970 <                   (s == null && (s = other.result) == null)) {
971 <                if (q != null) {
1656 <                    if (s != null ||
1657 <                        UNSAFE.compareAndSwapObject
1658 <                        (other, COMPLETIONS, q.next = other.completions, q))
1659 <                        break;
1660 <                }
1661 <                else if (r != null ||
1662 <                         UNSAFE.compareAndSwapObject
1663 <                         (this, COMPLETIONS, p.next = completions, p)) {
1664 <                    if (s != null)
1665 <                        break;
1666 <                    q = new CompletionNode(d);
1667 <                }
958 >    /* ------------- Two-source Anded -------------- */
959 >
960 >    /* Pushes c on to completions and o's completions unless both done. */
961 >    private <U> void bipushAnded(CompletableFuture<?> o, BiCompletion<U> c) {
962 >        if (c != null && o != null) {
963 >            Object r; CompletableFuture<?> d;
964 >            while ((r = result) == null &&
965 >                   !casCompletions(c.next = completions, c))
966 >                c.next = null;
967 >            if (o.result == null) {
968 >                Completion<U> q = (r != null) ? c : new CoBiCompletion<U>(c);
969 >                while (o.result == null &&
970 >                       !o.casCompletions(q.next = o.completions, q))
971 >                    q.next = null;
972              }
973 <            d.tryComplete();
973 >            if ((d = c.tryAct()) != null)
974 >                d.postComplete();
975 >            if (o.result != null)
976 >                o.postComplete();
977 >            if (result != null)
978 >                postComplete();
979          }
980 <        else {
981 <            T t; U u; Throwable ex;
980 >    }
981 >
982 >    // BiFunction/combine
983 >
984 >    static <T,U,V> void nowCombine(Executor e, CompletableFuture<V> d,
985 >                                   Object r, Object s,
986 >                                   BiFunction<? super T,? super U,? extends V> f) {
987 >        if (d != null && f != null) {
988 >            T t; U u; V v; Throwable x;
989              if (r instanceof AltResult) {
1674                ex = ((AltResult)r).ex;
990                  t = null;
991 +                x = ((AltResult)r).ex;
992              }
993              else {
994 <                ex = null;
995 <                @SuppressWarnings("unchecked") T tr = (T) r;
1680 <                t = tr;
994 >                @SuppressWarnings("unchecked") T tr = (T) r; t = tr;
995 >                x = null;
996              }
997 <            if (ex != null)
997 >            if (x != null)
998                  u = null;
999              else if (s instanceof AltResult) {
1000 <                ex = ((AltResult)s).ex;
1000 >                x = ((AltResult)s).ex;
1001                  u = null;
1002              }
1003              else {
1004 <                @SuppressWarnings("unchecked") U us = (U) s;
1690 <                u = us;
1004 >                @SuppressWarnings("unchecked") U us = (U) s; u = us;
1005              }
1006 <            V v = null;
1693 <            if (ex == null) {
1006 >            if (x == null) {
1007                  try {
1008 <                    if (e != null)
1009 <                        execAsync(e, new AsyncCombine<T,U,V>(t, u, fn, dst));
1010 <                    else
1011 <                        v = fn.apply(t, u);
1012 <                } catch (Throwable rex) {
1013 <                    ex = rex;
1008 >                    if (e != null) {
1009 >                        e.execute(new AsyncCombine<T,U,V>(d, t, u, f));
1010 >                        return;
1011 >                    }
1012 >                    v = f.apply(t, u);
1013 >                } catch (Throwable ex) {
1014 >                    x = ex;
1015 >                    v = null;
1016                  }
1017              }
1018 <            if (e == null || ex != null)
1019 <                dst.internalComplete(v, ex);
1018 >            else
1019 >                v = null;
1020 >            d.internalComplete(encodeOutcome(v, x));
1021          }
1706        return dst;
1022      }
1023  
1024 <    private <U> CompletableFuture<Void> doThenAcceptBoth
1025 <        (CompletableFuture<? extends U> other,
1026 <         BiConsumer<? super T,? super U> fn,
1027 <         Executor e) {
1028 <        if (other == null || fn == null) throw new NullPointerException();
1029 <        CompletableFuture<Void> dst = new CompletableFuture<Void>();
1030 <        Object r = result, s = other.result;
1031 <        if (r == null || s == null) {
1032 <            ThenAcceptBoth<T,U> d =
1033 <                new ThenAcceptBoth<T,U>(this, other, fn, dst, e);
1034 <            CompletionNode q = null, p = new CompletionNode(d);
1035 <            while ((r == null && (r = result) == null) ||
1036 <                   (s == null && (s = other.result) == null)) {
1037 <                if (q != null) {
1038 <                    if (s != null ||
1039 <                        UNSAFE.compareAndSwapObject
1040 <                        (other, COMPLETIONS, q.next = other.completions, q))
1041 <                        break;
1042 <                }
1043 <                else if (r != null ||
1044 <                         UNSAFE.compareAndSwapObject
1045 <                         (this, COMPLETIONS, p.next = completions, p)) {
1046 <                    if (s != null)
1047 <                        break;
1733 <                    q = new CompletionNode(d);
1734 <                }
1024 >    static final class AsyncCombine<T,U,V> extends Async<V> {
1025 >        T arg1; U arg2; BiFunction<? super T,? super U,? extends V> fn;
1026 >        AsyncCombine(CompletableFuture<V> dep, T arg1, U arg2,
1027 >                     BiFunction<? super T,? super U,? extends V> fn) {
1028 >            super(dep); this.arg1 = arg1; this.arg2 = arg2; this.fn = fn;
1029 >        }
1030 >        final void compute() { nowCombine(null, dep, arg1, arg2, fn); }
1031 >        private static final long serialVersionUID = 5232453952276885070L;
1032 >    }
1033 >
1034 >    static final class DelayedCombine<T,U,V> extends BiCompletion<V> {
1035 >        BiFunction<? super T,? super U,? extends V> fn;
1036 >        DelayedCombine(Executor async, CompletableFuture<V> dep,
1037 >                       CompletableFuture<?> src, CompletableFuture<?> snd,
1038 >                       BiFunction<? super T,? super U,? extends V> fn) {
1039 >            super(async, dep, src, snd); this.fn = fn;
1040 >        }
1041 >        final CompletableFuture<?> tryAct() {
1042 >            CompletableFuture<V> d; CompletableFuture<?> a, b; Object r, s;
1043 >            if ((d = dep) != null && (a = src) != null && (b = snd) != null &&
1044 >                (r = a.result) != null && (s = b.result) != null && claim(d)) {
1045 >                nowCombine(async, d, r, s, fn);
1046 >                src = null; snd = null; fn = null;
1047 >                if (d.result != null) return d;
1048              }
1049 <            d.tryComplete();
1049 >            return null;
1050          }
1051 <        else {
1052 <            T t; U u; Throwable ex;
1051 >    }
1052 >
1053 >    private <U,V> CompletableFuture<V> doThenCombine(
1054 >        CompletableFuture<? extends U> o,
1055 >        BiFunction<? super T,? super U,? extends V> fn,
1056 >        Executor e) {
1057 >        if (o == null || fn == null) throw new NullPointerException();
1058 >        CompletableFuture<V> d = new CompletableFuture<V>();
1059 >        Object r = result, s = o.result;
1060 >        if (r == null || s == null)
1061 >            bipushAnded(o, new DelayedCombine<T,U,V>(e, d, this, o, fn));
1062 >        else
1063 >            nowCombine(e, d, r, s, fn);
1064 >        return d;
1065 >    }
1066 >
1067 >    // BiConsumer/AcceptBoth
1068 >
1069 >    static <T,U,V> void nowAcceptBoth(Executor e, CompletableFuture<V> d,
1070 >                                      Object r, Object s,
1071 >                                      BiConsumer<? super T,? super U> f) {
1072 >        if (d != null && f != null) {
1073 >            T t; U u; Throwable x;
1074              if (r instanceof AltResult) {
1741                ex = ((AltResult)r).ex;
1075                  t = null;
1076 +                x = ((AltResult)r).ex;
1077              }
1078              else {
1079 <                ex = null;
1080 <                @SuppressWarnings("unchecked") T tr = (T) r;
1747 <                t = tr;
1079 >                @SuppressWarnings("unchecked") T tr = (T) r; t = tr;
1080 >                x = null;
1081              }
1082 <            if (ex != null)
1082 >            if (x != null)
1083                  u = null;
1084              else if (s instanceof AltResult) {
1085 <                ex = ((AltResult)s).ex;
1085 >                x = ((AltResult)s).ex;
1086                  u = null;
1087              }
1088              else {
1089 <                @SuppressWarnings("unchecked") U us = (U) s;
1757 <                u = us;
1089 >                @SuppressWarnings("unchecked") U us = (U) s; u = us;
1090              }
1091 <            if (ex == null) {
1091 >            if (x == null) {
1092                  try {
1093 <                    if (e != null)
1094 <                        execAsync(e, new AsyncAcceptBoth<T,U>(t, u, fn, dst));
1095 <                    else
1096 <                        fn.accept(t, u);
1097 <                } catch (Throwable rex) {
1098 <                    ex = rex;
1093 >                    if (e != null) {
1094 >                        e.execute(new AsyncAcceptBoth<T,U,V>(d, t, u, f));
1095 >                        return;
1096 >                    }
1097 >                    f.accept(t, u);
1098 >                } catch (Throwable ex) {
1099 >                    x = ex;
1100                  }
1101              }
1102 <            if (e == null || ex != null)
1770 <                dst.internalComplete(null, ex);
1102 >            d.internalComplete(encodeOutcome(null, x));
1103          }
1772        return dst;
1104      }
1105  
1106 <    private CompletableFuture<Void> doRunAfterBoth(CompletableFuture<?> other,
1107 <                                                   Runnable action,
1108 <                                                   Executor e) {
1109 <        if (other == null || action == null) throw new NullPointerException();
1110 <        CompletableFuture<Void> dst = new CompletableFuture<Void>();
1780 <        Object r = result, s = other.result;
1781 <        if (r == null || s == null) {
1782 <            RunAfterBoth d = new RunAfterBoth(this, other, action, dst, e);
1783 <            CompletionNode q = null, p = new CompletionNode(d);
1784 <            while ((r == null && (r = result) == null) ||
1785 <                   (s == null && (s = other.result) == null)) {
1786 <                if (q != null) {
1787 <                    if (s != null ||
1788 <                        UNSAFE.compareAndSwapObject
1789 <                        (other, COMPLETIONS, q.next = other.completions, q))
1790 <                        break;
1791 <                }
1792 <                else if (r != null ||
1793 <                         UNSAFE.compareAndSwapObject
1794 <                         (this, COMPLETIONS, p.next = completions, p)) {
1795 <                    if (s != null)
1796 <                        break;
1797 <                    q = new CompletionNode(d);
1798 <                }
1799 <            }
1800 <            d.tryComplete();
1106 >    static final class AsyncAcceptBoth<T,U,V> extends Async<V> {
1107 >        T arg1; U arg2; BiConsumer<? super T,? super U> fn;
1108 >        AsyncAcceptBoth(CompletableFuture<V> dep, T arg1, U arg2,
1109 >                        BiConsumer<? super T,? super U> fn) {
1110 >            super(dep); this.arg1 = arg1; this.arg2 = arg2; this.fn = fn;
1111          }
1112 <        else {
1113 <            Throwable ex;
1114 <            if (r instanceof AltResult)
1115 <                ex = ((AltResult)r).ex;
1116 <            else
1117 <                ex = null;
1118 <            if (ex == null && (s instanceof AltResult))
1119 <                ex = ((AltResult)s).ex;
1120 <            if (ex == null) {
1121 <                try {
1122 <                    if (e != null)
1123 <                        execAsync(e, new AsyncRun(action, dst));
1124 <                    else
1125 <                        action.run();
1126 <                } catch (Throwable rex) {
1127 <                    ex = rex;
1128 <                }
1112 >        final void compute() { nowAcceptBoth(null, dep, arg1, arg2, fn); }
1113 >        private static final long serialVersionUID = 5232453952276885070L;
1114 >    }
1115 >
1116 >    static final class DelayedAcceptBoth<T,U> extends BiCompletion<Void> {
1117 >        BiConsumer<? super T,? super U> fn;
1118 >        DelayedAcceptBoth(Executor async, CompletableFuture<Void> dep,
1119 >                          CompletableFuture<?> src, CompletableFuture<?> snd,
1120 >                          BiConsumer<? super T,? super U> fn) {
1121 >            super(async, dep, src, snd); this.fn = fn;
1122 >        }
1123 >        final CompletableFuture<?> tryAct() {
1124 >            CompletableFuture<Void> d; CompletableFuture<?> a, b; Object r, s;
1125 >            if ((d = dep) != null && (a = src) != null && (b = snd) != null &&
1126 >                (r = a.result) != null && (s = b.result) != null && claim(d)) {
1127 >                nowAcceptBoth(async, d, r, s, fn);
1128 >                src = null; snd = null; fn = null;
1129 >                if (d.result != null) return d;
1130              }
1131 <            if (e == null || ex != null)
1821 <                dst.internalComplete(null, ex);
1131 >            return null;
1132          }
1823        return dst;
1133      }
1134  
1135 <    private <U> CompletableFuture<U> doApplyToEither
1136 <        (CompletableFuture<? extends T> other,
1137 <         Function<? super T, U> fn,
1138 <         Executor e) {
1139 <        if (other == null || fn == null) throw new NullPointerException();
1140 <        CompletableFuture<U> dst = new CompletableFuture<U>();
1141 <        Object r;
1142 <        if ((r = result) == null && (r = other.result) == null) {
1143 <            ApplyToEither<T,U> d =
1144 <                new ApplyToEither<T,U>(this, other, fn, dst, e);
1145 <            CompletionNode q = null, p = new CompletionNode(d);
1146 <            while (result == null && other.result == null) {
1147 <                if (q != null) {
1148 <                    if (UNSAFE.compareAndSwapObject
1149 <                        (other, COMPLETIONS, q.next = other.completions, q))
1150 <                        break;
1151 <                }
1152 <                else if (UNSAFE.compareAndSwapObject
1153 <                         (this, COMPLETIONS, p.next = completions, p))
1154 <                    q = new CompletionNode(d);
1135 >    private <U> CompletableFuture<Void> doThenAcceptBoth(
1136 >        CompletableFuture<? extends U> o,
1137 >        BiConsumer<? super T, ? super U> fn,
1138 >        Executor e) {
1139 >        if (o == null || fn == null) throw new NullPointerException();
1140 >        CompletableFuture<Void> d = new CompletableFuture<Void>();
1141 >        Object r = result, s = o.result;
1142 >        if (r == null || s == null)
1143 >            bipushAnded(o, new DelayedAcceptBoth<T,U>(e, d, this, o, fn));
1144 >        else
1145 >            nowAcceptBoth(e, d, r, s, fn);
1146 >        return d;
1147 >    }
1148 >
1149 >    // Runnable/both
1150 >
1151 >    static final class DelayedRunAfterBoth extends BiCompletion<Void> {
1152 >        Runnable fn;
1153 >        DelayedRunAfterBoth(Executor async, CompletableFuture<Void> dep,
1154 >                            CompletableFuture<?> src, CompletableFuture<?> snd,
1155 >                            Runnable fn) {
1156 >            super(async, dep, src, snd); this.fn = fn;
1157 >        }
1158 >        final CompletableFuture<?> tryAct() {
1159 >            CompletableFuture<Void> d; CompletableFuture<?> a, b; Object r, s;
1160 >            if ((d = dep) != null && (a = src) != null && (b = snd) != null &&
1161 >                (r = a.result) != null && (s = b.result) != null && claim(d)) {
1162 >                Throwable x = (r instanceof AltResult) ?
1163 >                    ((AltResult)r).ex : null;
1164 >                nowRun(async, d, (x == null) ? s : r, fn);
1165 >                src = null; snd = null; fn = null;
1166 >                if (d.result != null) return d;
1167              }
1168 <            d.tryComplete();
1168 >            return null;
1169          }
1170 +    }
1171 +
1172 +    private CompletableFuture<Void> doRunAfterBoth(
1173 +        CompletableFuture<?> o, Runnable fn, Executor e) {
1174 +        if (o == null || fn == null) throw new NullPointerException();
1175 +        CompletableFuture<Void> d = new CompletableFuture<Void>();
1176 +        Object r = result, s = o.result;
1177 +        if (r == null || s == null)
1178 +            bipushAnded(o, new DelayedRunAfterBoth(e, d, this, o, fn));
1179          else {
1180 <            T t; Throwable ex;
1181 <            if (r instanceof AltResult) {
1852 <                ex = ((AltResult)r).ex;
1853 <                t = null;
1854 <            }
1855 <            else {
1856 <                ex = null;
1857 <                @SuppressWarnings("unchecked") T tr = (T) r;
1858 <                t = tr;
1859 <            }
1860 <            U u = null;
1861 <            if (ex == null) {
1862 <                try {
1863 <                    if (e != null)
1864 <                        execAsync(e, new AsyncApply<T,U>(t, fn, dst));
1865 <                    else
1866 <                        u = fn.apply(t);
1867 <                } catch (Throwable rex) {
1868 <                    ex = rex;
1869 <                }
1870 <            }
1871 <            if (e == null || ex != null)
1872 <                dst.internalComplete(u, ex);
1180 >            Throwable x = (r instanceof AltResult) ? ((AltResult)r).ex : null;
1181 >            nowRun(e, d, (x == null) ? s : r, fn);
1182          }
1183 <        return dst;
1183 >        return d;
1184      }
1185  
1186 <    private CompletableFuture<Void> doAcceptEither
1187 <        (CompletableFuture<? extends T> other,
1188 <         Consumer<? super T> fn,
1189 <         Executor e) {
1190 <        if (other == null || fn == null) throw new NullPointerException();
1191 <        CompletableFuture<Void> dst = new CompletableFuture<Void>();
1192 <        Object r;
1193 <        if ((r = result) == null && (r = other.result) == null) {
1194 <            AcceptEither<T> d =
1195 <                new AcceptEither<T>(this, other, fn, dst, e);
1196 <            CompletionNode q = null, p = new CompletionNode(d);
1197 <            while (result == null && other.result == null) {
1198 <                if (q != null) {
1199 <                    if (UNSAFE.compareAndSwapObject
1200 <                        (other, COMPLETIONS, q.next = other.completions, q))
1201 <                        break;
1202 <                }
1203 <                else if (UNSAFE.compareAndSwapObject
1204 <                         (this, COMPLETIONS, p.next = completions, p))
1205 <                    q = new CompletionNode(d);
1186 >    // allOf
1187 >
1188 >    static <T> void nowAnd(CompletableFuture<T> d, Object r, Object s) {
1189 >        if (d != null) {
1190 >            Throwable x = (r instanceof AltResult) ? ((AltResult)r).ex : null;
1191 >            if (x == null && (s instanceof AltResult))
1192 >                x = ((AltResult)s).ex;
1193 >            d.internalComplete(encodeOutcome(null, x));
1194 >        }
1195 >    }
1196 >
1197 >    static final class DelayedAnd extends BiCompletion<Void> {
1198 >        DelayedAnd(CompletableFuture<Void> dep,
1199 >                   CompletableFuture<?> src, CompletableFuture<?> snd) {
1200 >            super(null, dep, src, snd);
1201 >        }
1202 >        final CompletableFuture<?> tryAct() {
1203 >            CompletableFuture<Void> d; CompletableFuture<?> a, b; Object r, s;
1204 >            if ((d = dep) != null && (a = src) != null && (b = snd) != null &&
1205 >                (r = a.result) != null && (s = b.result) != null && claim(d)) {
1206 >                nowAnd(d, r, s);
1207 >                src = null; snd = null;
1208 >                if (d.result != null) return d;
1209              }
1210 <            d.tryComplete();
1210 >            return null;
1211          }
1212 +    }
1213 +
1214 +    /** Recursively constructs a tree of And completions */
1215 +    private static CompletableFuture<Void> doAllOf(CompletableFuture<?>[] cfs,
1216 +                                                   int lo, int hi) {
1217 +        CompletableFuture<Void> d = new CompletableFuture<Void>();
1218 +        if (lo > hi) // empty
1219 +            d.result = NIL;
1220          else {
1221 <            T t; Throwable ex;
1222 <            if (r instanceof AltResult) {
1223 <                ex = ((AltResult)r).ex;
1224 <                t = null;
1225 <            }
1226 <            else {
1227 <                ex = null;
1228 <                @SuppressWarnings("unchecked") T tr = (T) r;
1229 <                t = tr;
1221 >            int mid = (lo + hi) >>> 1;
1222 >            CompletableFuture<?> fst = (lo == mid ? cfs[lo] :
1223 >                                        doAllOf(cfs, lo,    mid));
1224 >            CompletableFuture<?> snd = (lo == hi ? fst : // and fst with self
1225 >                                        (hi == mid+1) ? cfs[hi] :
1226 >                                        doAllOf(cfs, mid+1, hi));
1227 >            Object r = fst.result, s = snd.result; // throw NPE if null elements
1228 >            if (r == null || s == null) {
1229 >                DelayedAnd a = new DelayedAnd(d, fst, snd);
1230 >                if (fst == snd)
1231 >                    fst.unipush(a);
1232 >                else
1233 >                    fst.bipushAnded(snd, a);
1234              }
1235 <            if (ex == null) {
1236 <                try {
1237 <                    if (e != null)
1238 <                        execAsync(e, new AsyncAccept<T>(t, fn, dst));
1239 <                    else
1240 <                        fn.accept(t);
1241 <                } catch (Throwable rex) {
1242 <                    ex = rex;
1235 >            else
1236 >                nowAnd(d, r, s);
1237 >        }
1238 >        return d;
1239 >    }
1240 >
1241 >    /* ------------- Two-source Ored -------------- */
1242 >
1243 >    /* Pushes c on to completions and o's completions unless either done. */
1244 >    private <U> void bipushOred(CompletableFuture<?> o, BiCompletion<U> c) {
1245 >        if (c != null && o != null) {
1246 >            CompletableFuture<?> d;
1247 >            while (o.result == null && result == null) {
1248 >                if (casCompletions(c.next = completions, c)) {
1249 >                    CoBiCompletion<U> q = new CoBiCompletion<U>(c);
1250 >                    while (result == null && o.result == null &&
1251 >                           !o.casCompletions(q.next = o.completions, q))
1252 >                        q.next = null;
1253 >                    break;
1254                  }
1255 +                c.next = null;
1256              }
1257 <            if (e == null || ex != null)
1258 <                dst.internalComplete(null, ex);
1257 >            if ((d = c.tryAct()) != null)
1258 >                d.postComplete();
1259 >            if (o.result != null)
1260 >                o.postComplete();
1261 >            if (result != null)
1262 >                postComplete();
1263          }
1924        return dst;
1264      }
1265  
1266 <    private CompletableFuture<Void> doRunAfterEither
1267 <        (CompletableFuture<?> other,
1268 <         Runnable action,
1269 <         Executor e) {
1270 <        if (other == null || action == null) throw new NullPointerException();
1271 <        CompletableFuture<Void> dst = new CompletableFuture<Void>();
1272 <        Object r;
1273 <        if ((r = result) == null && (r = other.result) == null) {
1274 <            RunAfterEither d =
1275 <                new RunAfterEither(this, other, action, dst, e);
1276 <            CompletionNode q = null, p = new CompletionNode(d);
1277 <            while (result == null && other.result == null) {
1278 <                if (q != null) {
1279 <                    if (UNSAFE.compareAndSwapObject
1280 <                        (other, COMPLETIONS, q.next = other.completions, q))
1281 <                        break;
1282 <                }
1944 <                else if (UNSAFE.compareAndSwapObject
1945 <                         (this, COMPLETIONS, p.next = completions, p))
1946 <                    q = new CompletionNode(d);
1266 >    // Function/applyEither
1267 >
1268 >    static final class DelayedApplyToEither<T,U> extends BiCompletion<U> {
1269 >        Function<? super T,? extends U> fn;
1270 >        DelayedApplyToEither(Executor async, CompletableFuture<U> dep,
1271 >                             CompletableFuture<?> src, CompletableFuture<?> snd,
1272 >                             Function<? super T,? extends U> fn) {
1273 >            super(async, dep, src, snd); this.fn = fn;
1274 >        }
1275 >        final CompletableFuture<?> tryAct() {
1276 >            CompletableFuture<U> d; CompletableFuture<?> a, b; Object r;
1277 >            if ((d = dep) != null && (a = src) != null && (b = snd) != null &&
1278 >                ((r = a.result) != null || (r = b.result) != null) &&
1279 >                claim(d)) {
1280 >                nowApply(async, d, r, fn);
1281 >                src = null; snd = null; fn = null;
1282 >                if (d.result != null) return d;
1283              }
1284 <            d.tryComplete();
1284 >            return null;
1285          }
1286 <        else {
1287 <            Throwable ex;
1288 <            if (r instanceof AltResult)
1289 <                ex = ((AltResult)r).ex;
1290 <            else
1291 <                ex = null;
1292 <            if (ex == null) {
1293 <                try {
1294 <                    if (e != null)
1295 <                        execAsync(e, new AsyncRun(action, dst));
1296 <                    else
1297 <                        action.run();
1298 <                } catch (Throwable rex) {
1299 <                    ex = rex;
1300 <                }
1286 >    }
1287 >
1288 >    private <U> CompletableFuture<U> doApplyToEither(
1289 >        CompletableFuture<? extends T> o,
1290 >        Function<? super T, U> fn, Executor e) {
1291 >        if (o == null || fn == null) throw new NullPointerException();
1292 >        CompletableFuture<U> d = new CompletableFuture<U>();
1293 >        Object r = result;
1294 >        if (r == null && (r = o.result) == null)
1295 >            bipushOred(o, new DelayedApplyToEither<T,U>(e, d, this, o, fn));
1296 >        else
1297 >            nowApply(e, d, r, fn);
1298 >        return d;
1299 >    }
1300 >
1301 >    // Consumer/acceptEither
1302 >
1303 >    static final class DelayedAcceptEither<T> extends BiCompletion<Void> {
1304 >        Consumer<? super T> fn;
1305 >        DelayedAcceptEither(Executor async, CompletableFuture<Void> dep,
1306 >                            CompletableFuture<?> src, CompletableFuture<?> snd,
1307 >                            Consumer<? super T> fn) {
1308 >            super(async, dep, src, snd); this.fn = fn;
1309 >        }
1310 >        final CompletableFuture<?> tryAct() {
1311 >            CompletableFuture<Void> d; CompletableFuture<?> a, b; Object r;
1312 >            if ((d = dep) != null && (a = src) != null && (b = snd) != null &&
1313 >                ((r = a.result) != null || (r = b.result) != null) &&
1314 >                claim(d)) {
1315 >                nowAccept(async, d, r, fn);
1316 >                src = null; snd = null; fn = null;
1317 >                if (d.result != null) return d;
1318              }
1319 <            if (e == null || ex != null)
1967 <                dst.internalComplete(null, ex);
1319 >            return null;
1320          }
1969        return dst;
1321      }
1322  
1323 <    private <U> CompletableFuture<U> doThenCompose
1324 <        (Function<? super T, ? extends CompletionStage<U>> fn,
1325 <         Executor e) {
1326 <        if (fn == null) throw new NullPointerException();
1327 <        CompletableFuture<U> dst = null;
1328 <        Object r;
1329 <        if ((r = result) == null) {
1330 <            dst = new CompletableFuture<U>();
1331 <            ThenCompose<T,U> d = new ThenCompose<T,U>(this, fn, dst, e);
1332 <            CompletionNode p = new CompletionNode(d);
1333 <            while (result == null) {
1334 <                if (UNSAFE.compareAndSwapObject
1335 <                    (this, COMPLETIONS, p.next = completions, p))
1336 <                    break;
1323 >    private CompletableFuture<Void> doAcceptEither(
1324 >        CompletableFuture<? extends T> o,
1325 >        Consumer<? super T> fn, Executor e) {
1326 >        if (o == null || fn == null) throw new NullPointerException();
1327 >        CompletableFuture<Void> d = new CompletableFuture<Void>();
1328 >        Object r = result;
1329 >        if (r == null && (r = o.result) == null)
1330 >            bipushOred(o, new DelayedAcceptEither<T>(e, d, this, o, fn));
1331 >        else
1332 >            nowAccept(e, d, r, fn);
1333 >        return d;
1334 >    }
1335 >
1336 >    // Runnable/runEither
1337 >
1338 >    static final class DelayedRunAfterEither extends BiCompletion<Void> {
1339 >        Runnable fn;
1340 >        DelayedRunAfterEither(Executor async, CompletableFuture<Void> dep,
1341 >                              CompletableFuture<?> src,
1342 >                              CompletableFuture<?> snd, Runnable fn) {
1343 >            super(async, dep, src, snd); this.fn = fn;
1344 >        }
1345 >        final CompletableFuture<?> tryAct() {
1346 >            CompletableFuture<Void> d; CompletableFuture<?> a, b; Object r;
1347 >            if ((d = dep) != null && (a = src) != null && (b = snd) != null &&
1348 >                ((r = a.result) != null || (r = b.result) != null) &&
1349 >                claim(d)) {
1350 >                nowRun(async, d, r, fn);
1351 >                src = null; snd = null; fn = null;
1352 >                if (d.result != null) return d;
1353              }
1354 <            d.tryComplete();
1354 >            return null;
1355          }
1356 <        else {
1357 <            T t; Throwable ex;
1358 <            if (r instanceof AltResult) {
1359 <                ex = ((AltResult)r).ex;
1360 <                t = null;
1356 >    }
1357 >
1358 >    private CompletableFuture<Void> doRunAfterEither(
1359 >        CompletableFuture<?> o, Runnable fn, Executor e) {
1360 >        if (o == null || fn == null) throw new NullPointerException();
1361 >        CompletableFuture<Void> d = new CompletableFuture<Void>();
1362 >        Object r = result;
1363 >        if (r == null && (r = o.result) == null)
1364 >            bipushOred(o, new DelayedRunAfterEither(e, d, this, o, fn));
1365 >        else
1366 >            nowRun(e, d, r, fn);
1367 >        return d;
1368 >    }
1369 >
1370 >    /* ------------- Signallers -------------- */
1371 >
1372 >    /**
1373 >     * Heuristic spin value for waitingGet() before blocking on
1374 >     * multiprocessors
1375 >     */
1376 >    static final int SPINS = (Runtime.getRuntime().availableProcessors() > 1 ?
1377 >                              1 << 8 : 0);
1378 >
1379 >    /**
1380 >     * Completion for recording and releasing a waiting thread.  See
1381 >     * other classes such as Phaser and SynchronousQueue for more
1382 >     * detailed explanation. This class implements ManagedBlocker to
1383 >     * avoid starvation when blocking actions pile up in
1384 >     * ForkJoinPools.
1385 >     */
1386 >    static final class Signaller extends Completion<Void>
1387 >        implements ForkJoinPool.ManagedBlocker {
1388 >        long nanos;          // wait time if timed
1389 >        final long deadline; // non-zero if timed
1390 >        volatile int interruptControl; // > 0: interruptible, < 0: interrupted
1391 >        volatile Thread thread;
1392 >        Signaller(boolean interruptible, long nanos, long deadline) {
1393 >            this.thread = Thread.currentThread();
1394 >            this.interruptControl = interruptible ? 1 : 0;
1395 >            this.nanos = nanos;
1396 >            this.deadline = deadline;
1397 >        }
1398 >        final CompletableFuture<?> tryAct() {
1399 >            Thread w = thread;
1400 >            if (w != null) {
1401 >                thread = null; // no need to CAS
1402 >                LockSupport.unpark(w);
1403              }
1404 <            else {
1405 <                ex = null;
1406 <                @SuppressWarnings("unchecked") T tr = (T) r;
1407 <                t = tr;
1404 >            return null;
1405 >        }
1406 >        public boolean isReleasable() {
1407 >            if (thread == null)
1408 >                return true;
1409 >            if (Thread.interrupted()) {
1410 >                int i = interruptControl;
1411 >                interruptControl = -1;
1412 >                if (i > 0)
1413 >                    return true;
1414              }
1415 <            if (ex == null) {
1416 <                if (e != null) {
1417 <                    if (dst == null)
1418 <                        dst = new CompletableFuture<U>();
2004 <                    execAsync(e, new AsyncCompose<T,U>(t, fn, dst));
2005 <                }
2006 <                else {
2007 <                    try {
2008 <                        CompletionStage<U> cs = fn.apply(t);
2009 <                        if (cs == null ||
2010 <                            (dst = cs.toCompletableFuture()) == null)
2011 <                            ex = new NullPointerException();
2012 <                    } catch (Throwable rex) {
2013 <                        ex = rex;
2014 <                    }
2015 <                }
1415 >            if (deadline != 0L &&
1416 >                (nanos <= 0L || (nanos = deadline - System.nanoTime()) <= 0L)) {
1417 >                thread = null;
1418 >                return true;
1419              }
1420 <            if (dst == null)
1421 <                dst = new CompletableFuture<U>();
1422 <            if (ex != null)
1423 <                dst.internalComplete(null, ex);
1420 >            return false;
1421 >        }
1422 >        public boolean block() {
1423 >            if (isReleasable())
1424 >                return true;
1425 >            else if (deadline == 0L)
1426 >                LockSupport.park(this);
1427 >            else if (nanos > 0L)
1428 >                LockSupport.parkNanos(this, nanos);
1429 >            return isReleasable();
1430          }
2022        return dst;
1431      }
1432  
1433 <    private CompletableFuture<T> doWhenComplete
1434 <        (BiConsumer<? super T, ? super Throwable> fn,
1435 <         Executor e) {
1436 <        if (fn == null) throw new NullPointerException();
1437 <        CompletableFuture<T> dst = new CompletableFuture<T>();
1433 >    /**
1434 >     * Returns raw result after waiting, or null if interruptible and
1435 >     * interrupted.
1436 >     */
1437 >    private Object waitingGet(boolean interruptible) {
1438 >        Signaller q = null;
1439 >        boolean queued = false;
1440 >        int spins = SPINS;
1441          Object r;
1442 <        if ((r = result) == null) {
1443 <            WhenCompleteCompletion<T> d =
1444 <                new WhenCompleteCompletion<T>(this, fn, dst, e);
1445 <            CompletionNode p = new CompletionNode(d);
2035 <            while (result == null) {
2036 <                if (UNSAFE.compareAndSwapObject(this, COMPLETIONS,
2037 <                                                p.next = completions, p))
2038 <                    break;
1442 >        while ((r = result) == null) {
1443 >            if (spins > 0) {
1444 >                if (ThreadLocalRandom.nextSecondarySeed() >= 0)
1445 >                    --spins;
1446              }
1447 <            d.tryComplete();
1448 <        }
1449 <        else {
1450 <            T t; Throwable ex;
1451 <            if (r instanceof AltResult) {
1452 <                ex = ((AltResult)r).ex;
1453 <                t = null;
1447 >            else if (q == null)
1448 >                q = new Signaller(interruptible, 0L, 0L);
1449 >            else if (!queued)
1450 >                queued = casCompletions(q.next = completions, q);
1451 >            else if (interruptible && q.interruptControl < 0) {
1452 >                q.thread = null;
1453 >                removeCancelledSignallers();
1454 >                return null;
1455              }
1456 <            else {
1457 <                ex = null;
1458 <                @SuppressWarnings("unchecked") T tr = (T) r;
1459 <                t = tr;
1456 >            else if (q.thread != null && result == null) {
1457 >                try {
1458 >                    ForkJoinPool.managedBlock(q);
1459 >                } catch (InterruptedException ie) {
1460 >                    q.interruptControl = -1;
1461 >                }
1462              }
1463 <            Throwable dx = null;
1464 <            try {
1465 <                if (e != null)
1466 <                    execAsync(e, new AsyncWhenComplete<T>(t, ex, fn, dst));
1463 >        }
1464 >        if (q != null) {
1465 >            q.thread = null;
1466 >            if (q.interruptControl < 0) {
1467 >                if (interruptible)
1468 >                    r = null; // report interruption
1469                  else
1470 <                    fn.accept(t, ex);
2059 <            } catch (Throwable rex) {
2060 <                dx = rex;
1470 >                    Thread.currentThread().interrupt();
1471              }
2062            if (e == null || dx != null)
2063                dst.internalComplete(t, ex != null ? ex : dx);
1472          }
1473 <        return dst;
1473 >        postComplete();
1474 >        return r;
1475      }
1476  
1477 <    private <U> CompletableFuture<U> doHandle
1478 <        (BiFunction<? super T, Throwable, ? extends U> fn,
1479 <         Executor e) {
1480 <        if (fn == null) throw new NullPointerException();
1481 <        CompletableFuture<U> dst = new CompletableFuture<U>();
1477 >    /**
1478 >     * Returns raw result after waiting, or null if interrupted, or
1479 >     * throws TimeoutException on timeout.
1480 >     */
1481 >    private Object timedGet(long nanos) throws TimeoutException {
1482 >        if (Thread.interrupted())
1483 >            return null;
1484 >        if (nanos <= 0L)
1485 >            throw new TimeoutException();
1486 >        long d = System.nanoTime() + nanos;
1487 >        Signaller q = new Signaller(true, nanos, d == 0L ? 1L : d); // avoid 0
1488 >        boolean queued = false;
1489          Object r;
1490 <        if ((r = result) == null) {
1491 <            HandleCompletion<T,U> d =
1492 <                new HandleCompletion<T,U>(this, fn, dst, e);
1493 <            CompletionNode p = new CompletionNode(d);
1494 <            while (result == null) {
1495 <                if (UNSAFE.compareAndSwapObject(this, COMPLETIONS,
1496 <                                                p.next = completions, p))
1497 <                    break;
1490 >        while ((r = result) == null) {
1491 >            if (!queued)
1492 >                queued = casCompletions(q.next = completions, q);
1493 >            else if (q.interruptControl < 0 || q.nanos <= 0L) {
1494 >                q.thread = null;
1495 >                removeCancelledSignallers();
1496 >                if (q.interruptControl < 0)
1497 >                    return null;
1498 >                throw new TimeoutException();
1499 >            }
1500 >            else if (q.thread != null && result == null) {
1501 >                try {
1502 >                    ForkJoinPool.managedBlock(q);
1503 >                } catch (InterruptedException ie) {
1504 >                    q.interruptControl = -1;
1505 >                }
1506              }
2083            d.tryComplete();
1507          }
1508 <        else {
1509 <            T t; Throwable ex;
1510 <            if (r instanceof AltResult) {
1511 <                ex = ((AltResult)r).ex;
1512 <                t = null;
1508 >        q.thread = null;
1509 >        postComplete();
1510 >        return (q.interruptControl < 0) ? null : r;
1511 >    }
1512 >
1513 >    /**
1514 >     * Unlinks cancelled Signallers to avoid accumulating garbage.
1515 >     * Internal nodes are simply unspliced without CAS since it is
1516 >     * harmless if they are traversed anyway.  To avoid effects of
1517 >     * unsplicing from already removed nodes, the list is retraversed
1518 >     * in case of an apparent race.
1519 >     */
1520 >    private void removeCancelledSignallers() {
1521 >        for (Completion<?> p = null, q = completions; q != null;) {
1522 >            Completion<?> s = q.next;
1523 >            if ((q instanceof Signaller) && ((Signaller)q).thread == null) {
1524 >                if (p != null) {
1525 >                    p.next = s;
1526 >                    if (!(p instanceof Signaller) ||
1527 >                        ((Signaller)p).thread != null)
1528 >                        break;
1529 >                }
1530 >                else if (casCompletions(q, s))
1531 >                    break;
1532 >                p = null; // restart
1533 >                q = completions;
1534              }
1535              else {
1536 <                ex = null;
1537 <                @SuppressWarnings("unchecked") T tr = (T) r;
2094 <                t = tr;
1536 >                p = q;
1537 >                q = s;
1538              }
2096            U u = null;
2097            Throwable dx = null;
2098            try {
2099                if (e != null)
2100                    execAsync(e, new AsyncCombine<T,Throwable,U>(t, ex, fn, dst));
2101                else {
2102                    u = fn.apply(t, ex);
2103                    dx = null;
2104                }
2105            } catch (Throwable rex) {
2106                dx = rex;
2107                u = null;
2108            }
2109            if (e == null || dx != null)
2110                dst.internalComplete(u, dx);
1539          }
2112        return dst;
1540      }
1541  
1542 <    // public methods
1542 >    /* ------------- public methods -------------- */
1543  
1544      /**
1545       * Creates a new incomplete CompletableFuture.
# Line 2132 | Line 1559 | public class CompletableFuture<T> implem
1559       */
1560      public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) {
1561          if (supplier == null) throw new NullPointerException();
1562 <        CompletableFuture<U> f = new CompletableFuture<U>();
1563 <        execAsync(ForkJoinPool.commonPool(), new AsyncSupply<U>(supplier, f));
1564 <        return f;
1562 >        CompletableFuture<U> d = new CompletableFuture<U>();
1563 >        asyncPool.execute(new AsyncSupply<U>(d, supplier));
1564 >        return d;
1565      }
1566  
1567      /**
# Line 2152 | Line 1579 | public class CompletableFuture<T> implem
1579                                                         Executor executor) {
1580          if (executor == null || supplier == null)
1581              throw new NullPointerException();
1582 <        CompletableFuture<U> f = new CompletableFuture<U>();
1583 <        execAsync(executor, new AsyncSupply<U>(supplier, f));
1584 <        return f;
1582 >        CompletableFuture<U> d = new CompletableFuture<U>();
1583 >        executor.execute(new AsyncSupply<U>(d, supplier));
1584 >        return d;
1585      }
1586  
1587      /**
# Line 2168 | Line 1595 | public class CompletableFuture<T> implem
1595       */
1596      public static CompletableFuture<Void> runAsync(Runnable runnable) {
1597          if (runnable == null) throw new NullPointerException();
1598 <        CompletableFuture<Void> f = new CompletableFuture<Void>();
1599 <        execAsync(ForkJoinPool.commonPool(), new AsyncRun(runnable, f));
1600 <        return f;
1598 >        CompletableFuture<Void> d = new CompletableFuture<Void>();
1599 >        asyncPool.execute(new AsyncRun<Void>(d, runnable));
1600 >        return d;
1601      }
1602  
1603      /**
# Line 2187 | Line 1614 | public class CompletableFuture<T> implem
1614                                                     Executor executor) {
1615          if (executor == null || runnable == null)
1616              throw new NullPointerException();
1617 <        CompletableFuture<Void> f = new CompletableFuture<Void>();
1618 <        execAsync(executor, new AsyncRun(runnable, f));
1619 <        return f;
1617 >        CompletableFuture<Void> d = new CompletableFuture<Void>();
1618 >        executor.execute(new AsyncRun<Void>(d, runnable));
1619 >        return d;
1620      }
1621  
1622      /**
# Line 2201 | Line 1628 | public class CompletableFuture<T> implem
1628       * @return the completed CompletableFuture
1629       */
1630      public static <U> CompletableFuture<U> completedFuture(U value) {
1631 <        CompletableFuture<U> f = new CompletableFuture<U>();
1632 <        f.result = (value == null) ? NIL : value;
1633 <        return f;
1631 >        CompletableFuture<U> d = new CompletableFuture<U>();
1632 >        d.result = (value == null) ? NIL : value;
1633 >        return d;
1634      }
1635  
1636      /**
# Line 2227 | Line 1654 | public class CompletableFuture<T> implem
1654       * while waiting
1655       */
1656      public T get() throws InterruptedException, ExecutionException {
1657 <        Object r; Throwable ex, cause;
1658 <        if ((r = result) == null && (r = waitingGet(true)) == null)
2232 <            throw new InterruptedException();
2233 <        if (!(r instanceof AltResult)) {
2234 <            @SuppressWarnings("unchecked") T tr = (T) r;
2235 <            return tr;
2236 <        }
2237 <        if ((ex = ((AltResult)r).ex) == null)
2238 <            return null;
2239 <        if (ex instanceof CancellationException)
2240 <            throw (CancellationException)ex;
2241 <        if ((ex instanceof CompletionException) &&
2242 <            (cause = ex.getCause()) != null)
2243 <            ex = cause;
2244 <        throw new ExecutionException(ex);
1657 >        Object r;
1658 >        return reportGet((r = result) == null ?  waitingGet(true) : r);
1659      }
1660  
1661      /**
# Line 2259 | Line 1673 | public class CompletableFuture<T> implem
1673       */
1674      public T get(long timeout, TimeUnit unit)
1675          throws InterruptedException, ExecutionException, TimeoutException {
1676 <        Object r; Throwable ex, cause;
1676 >        Object r;
1677          long nanos = unit.toNanos(timeout);
1678 <        if (Thread.interrupted())
2265 <            throw new InterruptedException();
2266 <        if ((r = result) == null)
2267 <            r = timedAwaitDone(nanos);
2268 <        if (!(r instanceof AltResult)) {
2269 <            @SuppressWarnings("unchecked") T tr = (T) r;
2270 <            return tr;
2271 <        }
2272 <        if ((ex = ((AltResult)r).ex) == null)
2273 <            return null;
2274 <        if (ex instanceof CancellationException)
2275 <            throw (CancellationException)ex;
2276 <        if ((ex instanceof CompletionException) &&
2277 <            (cause = ex.getCause()) != null)
2278 <            ex = cause;
2279 <        throw new ExecutionException(ex);
1678 >        return reportGet((r = result) == null ?  timedGet(nanos) : r);
1679      }
1680  
1681      /**
# Line 2294 | Line 1693 | public class CompletableFuture<T> implem
1693       * exceptionally or a completion computation threw an exception
1694       */
1695      public T join() {
1696 <        Object r; Throwable ex;
1697 <        if ((r = result) == null)
2299 <            r = waitingGet(false);
2300 <        if (!(r instanceof AltResult)) {
2301 <            @SuppressWarnings("unchecked") T tr = (T) r;
2302 <            return tr;
2303 <        }
2304 <        if ((ex = ((AltResult)r).ex) == null)
2305 <            return null;
2306 <        if (ex instanceof CancellationException)
2307 <            throw (CancellationException)ex;
2308 <        if (ex instanceof CompletionException)
2309 <            throw (CompletionException)ex;
2310 <        throw new CompletionException(ex);
1696 >        Object r;
1697 >        return reportJoin((r = result) == null ? waitingGet(false) : r);
1698      }
1699  
1700      /**
# Line 2321 | Line 1708 | public class CompletableFuture<T> implem
1708       * exceptionally or a completion computation threw an exception
1709       */
1710      public T getNow(T valueIfAbsent) {
1711 <        Object r; Throwable ex;
1712 <        if ((r = result) == null)
2326 <            return valueIfAbsent;
2327 <        if (!(r instanceof AltResult)) {
2328 <            @SuppressWarnings("unchecked") T tr = (T) r;
2329 <            return tr;
2330 <        }
2331 <        if ((ex = ((AltResult)r).ex) == null)
2332 <            return null;
2333 <        if (ex instanceof CancellationException)
2334 <            throw (CancellationException)ex;
2335 <        if (ex instanceof CompletionException)
2336 <            throw (CompletionException)ex;
2337 <        throw new CompletionException(ex);
1711 >        Object r;
1712 >        return (r = result) == null? valueIfAbsent : reportJoin(r);
1713      }
1714  
1715      /**
# Line 2346 | Line 1721 | public class CompletableFuture<T> implem
1721       * to transition to a completed state, else {@code false}
1722       */
1723      public boolean complete(T value) {
1724 <        boolean triggered = result == null &&
1725 <            UNSAFE.compareAndSwapObject(this, RESULT, null,
2351 <                                        value == null ? NIL : value);
2352 <        postComplete(this);
1724 >        boolean triggered = internalComplete(value == null ? NIL : value);
1725 >        postComplete();
1726          return triggered;
1727      }
1728  
# Line 2363 | Line 1736 | public class CompletableFuture<T> implem
1736       */
1737      public boolean completeExceptionally(Throwable ex) {
1738          if (ex == null) throw new NullPointerException();
1739 <        boolean triggered = result == null &&
1740 <            UNSAFE.compareAndSwapObject(this, RESULT, null, new AltResult(ex));
2368 <        postComplete(this);
1739 >        boolean triggered = internalComplete(new AltResult(ex));
1740 >        postComplete();
1741          return triggered;
1742      }
1743  
1744 <    // CompletionStage methods
1745 <
2374 <    public <U> CompletableFuture<U> thenApply
2375 <        (Function<? super T,? extends U> fn) {
1744 >    public <U> CompletableFuture<U> thenApply(
1745 >        Function<? super T,? extends U> fn) {
1746          return doThenApply(fn, null);
1747      }
1748  
1749 <    public <U> CompletableFuture<U> thenApplyAsync
1750 <        (Function<? super T,? extends U> fn) {
1751 <        return doThenApply(fn, ForkJoinPool.commonPool());
1749 >    public <U> CompletableFuture<U> thenApplyAsync(
1750 >        Function<? super T,? extends U> fn) {
1751 >        return doThenApply(fn, asyncPool);
1752      }
1753  
1754 <    public <U> CompletableFuture<U> thenApplyAsync
1755 <        (Function<? super T,? extends U> fn,
2386 <         Executor executor) {
1754 >    public <U> CompletableFuture<U> thenApplyAsync(
1755 >        Function<? super T,? extends U> fn, Executor executor) {
1756          if (executor == null) throw new NullPointerException();
1757          return doThenApply(fn, executor);
1758      }
1759  
1760 <    public CompletableFuture<Void> thenAccept
2392 <        (Consumer<? super T> action) {
1760 >    public CompletableFuture<Void> thenAccept(Consumer<? super T> action) {
1761          return doThenAccept(action, null);
1762      }
1763  
1764 <    public CompletableFuture<Void> thenAcceptAsync
1765 <        (Consumer<? super T> action) {
2398 <        return doThenAccept(action, ForkJoinPool.commonPool());
1764 >    public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action) {
1765 >        return doThenAccept(action, asyncPool);
1766      }
1767  
1768 <    public CompletableFuture<Void> thenAcceptAsync
1769 <        (Consumer<? super T> action,
2403 <         Executor executor) {
1768 >    public CompletableFuture<Void> thenAcceptAsync(
1769 >        Consumer<? super T> action, Executor executor) {
1770          if (executor == null) throw new NullPointerException();
1771          return doThenAccept(action, executor);
1772      }
1773  
1774 <    public CompletableFuture<Void> thenRun
2409 <        (Runnable action) {
1774 >    public CompletableFuture<Void> thenRun(Runnable action) {
1775          return doThenRun(action, null);
1776      }
1777  
1778 <    public CompletableFuture<Void> thenRunAsync
1779 <        (Runnable action) {
2415 <        return doThenRun(action, ForkJoinPool.commonPool());
1778 >    public CompletableFuture<Void> thenRunAsync(Runnable action) {
1779 >        return doThenRun(action, asyncPool);
1780      }
1781  
1782 <    public CompletableFuture<Void> thenRunAsync
1783 <        (Runnable action,
2420 <         Executor executor) {
1782 >    public CompletableFuture<Void> thenRunAsync(
1783 >        Runnable action, Executor executor) {
1784          if (executor == null) throw new NullPointerException();
1785          return doThenRun(action, executor);
1786      }
1787  
1788 <    public <U,V> CompletableFuture<V> thenCombine
1789 <        (CompletionStage<? extends U> other,
1790 <         BiFunction<? super T,? super U,? extends V> fn) {
1788 >    public <U,V> CompletableFuture<V> thenCombine(
1789 >        CompletionStage<? extends U> other,
1790 >        BiFunction<? super T,? super U,? extends V> fn) {
1791          return doThenCombine(other.toCompletableFuture(), fn, null);
1792      }
1793  
1794 <    public <U,V> CompletableFuture<V> thenCombineAsync
1795 <        (CompletionStage<? extends U> other,
1796 <         BiFunction<? super T,? super U,? extends V> fn) {
1797 <        return doThenCombine(other.toCompletableFuture(), fn,
2435 <                             ForkJoinPool.commonPool());
1794 >    public <U,V> CompletableFuture<V> thenCombineAsync(
1795 >        CompletionStage<? extends U> other,
1796 >        BiFunction<? super T,? super U,? extends V> fn) {
1797 >        return doThenCombine(other.toCompletableFuture(), fn, asyncPool);
1798      }
1799  
1800 <    public <U,V> CompletableFuture<V> thenCombineAsync
1801 <        (CompletionStage<? extends U> other,
1802 <         BiFunction<? super T,? super U,? extends V> fn,
1803 <         Executor executor) {
1800 >    public <U,V> CompletableFuture<V> thenCombineAsync(
1801 >        CompletionStage<? extends U> other,
1802 >        BiFunction<? super T,? super U,? extends V> fn,
1803 >        Executor executor) {
1804          if (executor == null) throw new NullPointerException();
1805          return doThenCombine(other.toCompletableFuture(), fn, executor);
1806      }
1807  
1808 <    public <U> CompletableFuture<Void> thenAcceptBoth
1809 <        (CompletionStage<? extends U> other,
1810 <         BiConsumer<? super T, ? super U> action) {
1808 >    public <U> CompletableFuture<Void> thenAcceptBoth(
1809 >        CompletionStage<? extends U> other,
1810 >        BiConsumer<? super T, ? super U> action) {
1811          return doThenAcceptBoth(other.toCompletableFuture(), action, null);
1812      }
1813  
1814 <    public <U> CompletableFuture<Void> thenAcceptBothAsync
1815 <        (CompletionStage<? extends U> other,
1816 <         BiConsumer<? super T, ? super U> action) {
1817 <        return doThenAcceptBoth(other.toCompletableFuture(), action,
2456 <                                ForkJoinPool.commonPool());
1814 >    public <U> CompletableFuture<Void> thenAcceptBothAsync(
1815 >        CompletionStage<? extends U> other,
1816 >        BiConsumer<? super T, ? super U> action) {
1817 >        return doThenAcceptBoth(other.toCompletableFuture(), action, asyncPool);
1818      }
1819  
1820 <    public <U> CompletableFuture<Void> thenAcceptBothAsync
1821 <        (CompletionStage<? extends U> other,
1822 <         BiConsumer<? super T, ? super U> action,
1823 <         Executor executor) {
1820 >    public <U> CompletableFuture<Void> thenAcceptBothAsync(
1821 >        CompletionStage<? extends U> other,
1822 >        BiConsumer<? super T, ? super U> action,
1823 >        Executor executor) {
1824          if (executor == null) throw new NullPointerException();
1825          return doThenAcceptBoth(other.toCompletableFuture(), action, executor);
1826      }
1827  
1828 <    public CompletableFuture<Void> runAfterBoth
1829 <        (CompletionStage<?> other,
2469 <         Runnable action) {
1828 >    public CompletableFuture<Void> runAfterBoth(
1829 >        CompletionStage<?> other, Runnable action) {
1830          return doRunAfterBoth(other.toCompletableFuture(), action, null);
1831      }
1832  
1833 <    public CompletableFuture<Void> runAfterBothAsync
1834 <        (CompletionStage<?> other,
1835 <         Runnable action) {
2476 <        return doRunAfterBoth(other.toCompletableFuture(), action,
2477 <                              ForkJoinPool.commonPool());
1833 >    public CompletableFuture<Void> runAfterBothAsync(
1834 >        CompletionStage<?> other, Runnable action) {
1835 >        return doRunAfterBoth(other.toCompletableFuture(), action, asyncPool);
1836      }
1837  
1838 <    public CompletableFuture<Void> runAfterBothAsync
1839 <        (CompletionStage<?> other,
2482 <         Runnable action,
2483 <         Executor executor) {
1838 >    public CompletableFuture<Void> runAfterBothAsync(
1839 >        CompletionStage<?> other, Runnable action, Executor executor) {
1840          if (executor == null) throw new NullPointerException();
1841          return doRunAfterBoth(other.toCompletableFuture(), action, executor);
1842      }
1843  
1844 <
1845 <    public <U> CompletableFuture<U> applyToEither
2490 <        (CompletionStage<? extends T> other,
2491 <         Function<? super T, U> fn) {
1844 >    public <U> CompletableFuture<U> applyToEither(
1845 >        CompletionStage<? extends T> other, Function<? super T, U> fn) {
1846          return doApplyToEither(other.toCompletableFuture(), fn, null);
1847      }
1848  
1849 <    public <U> CompletableFuture<U> applyToEitherAsync
1850 <        (CompletionStage<? extends T> other,
1851 <         Function<? super T, U> fn) {
2498 <        return doApplyToEither(other.toCompletableFuture(), fn,
2499 <                               ForkJoinPool.commonPool());
1849 >    public <U> CompletableFuture<U> applyToEitherAsync(
1850 >        CompletionStage<? extends T> other, Function<? super T, U> fn) {
1851 >        return doApplyToEither(other.toCompletableFuture(), fn, asyncPool);
1852      }
1853  
1854      public <U> CompletableFuture<U> applyToEitherAsync
1855 <        (CompletionStage<? extends T> other,
2504 <         Function<? super T, U> fn,
1855 >        (CompletionStage<? extends T> other, Function<? super T, U> fn,
1856           Executor executor) {
1857          if (executor == null) throw new NullPointerException();
1858          return doApplyToEither(other.toCompletableFuture(), fn, executor);
1859      }
1860  
1861 <    public CompletableFuture<Void> acceptEither
1862 <        (CompletionStage<? extends T> other,
2512 <         Consumer<? super T> action) {
1861 >    public CompletableFuture<Void> acceptEither(
1862 >        CompletionStage<? extends T> other, Consumer<? super T> action) {
1863          return doAcceptEither(other.toCompletableFuture(), action, null);
1864      }
1865  
1866      public CompletableFuture<Void> acceptEitherAsync
1867 <        (CompletionStage<? extends T> other,
1868 <         Consumer<? super T> action) {
2519 <        return doAcceptEither(other.toCompletableFuture(), action,
2520 <                              ForkJoinPool.commonPool());
1867 >        (CompletionStage<? extends T> other, Consumer<? super T> action) {
1868 >        return doAcceptEither(other.toCompletableFuture(), action, asyncPool);
1869      }
1870  
1871 <    public CompletableFuture<Void> acceptEitherAsync
1872 <        (CompletionStage<? extends T> other,
1873 <         Consumer<? super T> action,
2526 <         Executor executor) {
1871 >    public CompletableFuture<Void> acceptEitherAsync(
1872 >        CompletionStage<? extends T> other, Consumer<? super T> action,
1873 >        Executor executor) {
1874          if (executor == null) throw new NullPointerException();
1875          return doAcceptEither(other.toCompletableFuture(), action, executor);
1876      }
1877  
1878 <    public CompletableFuture<Void> runAfterEither(CompletionStage<?> other,
1879 <                                                  Runnable action) {
1878 >    public CompletableFuture<Void> runAfterEither(
1879 >        CompletionStage<?> other, Runnable action) {
1880          return doRunAfterEither(other.toCompletableFuture(), action, null);
1881      }
1882  
1883 <    public CompletableFuture<Void> runAfterEitherAsync
1884 <        (CompletionStage<?> other,
1885 <         Runnable action) {
2539 <        return doRunAfterEither(other.toCompletableFuture(), action,
2540 <                                ForkJoinPool.commonPool());
1883 >    public CompletableFuture<Void> runAfterEitherAsync(
1884 >        CompletionStage<?> other, Runnable action) {
1885 >        return doRunAfterEither(other.toCompletableFuture(), action, asyncPool);
1886      }
1887  
1888 <    public CompletableFuture<Void> runAfterEitherAsync
1889 <        (CompletionStage<?> other,
2545 <         Runnable action,
2546 <         Executor executor) {
1888 >    public CompletableFuture<Void> runAfterEitherAsync(
1889 >        CompletionStage<?> other, Runnable action, Executor executor) {
1890          if (executor == null) throw new NullPointerException();
1891          return doRunAfterEither(other.toCompletableFuture(), action, executor);
1892      }
# Line 2553 | Line 1896 | public class CompletableFuture<T> implem
1896          return doThenCompose(fn, null);
1897      }
1898  
1899 <    public <U> CompletableFuture<U> thenComposeAsync
1900 <        (Function<? super T, ? extends CompletionStage<U>> fn) {
1901 <        return doThenCompose(fn, ForkJoinPool.commonPool());
1899 >    public <U> CompletableFuture<U> thenComposeAsync(
1900 >        Function<? super T, ? extends CompletionStage<U>> fn) {
1901 >        return doThenCompose(fn, asyncPool);
1902      }
1903  
1904 <    public <U> CompletableFuture<U> thenComposeAsync
1905 <        (Function<? super T, ? extends CompletionStage<U>> fn,
1906 <         Executor executor) {
1904 >    public <U> CompletableFuture<U> thenComposeAsync(
1905 >        Function<? super T, ? extends CompletionStage<U>> fn,
1906 >        Executor executor) {
1907          if (executor == null) throw new NullPointerException();
1908          return doThenCompose(fn, executor);
1909      }
1910  
1911 <    public CompletableFuture<T> whenComplete
1912 <        (BiConsumer<? super T, ? super Throwable> action) {
1911 >    public CompletableFuture<T> whenComplete(
1912 >        BiConsumer<? super T, ? super Throwable> action) {
1913          return doWhenComplete(action, null);
1914      }
1915  
1916 <    public CompletableFuture<T> whenCompleteAsync
1917 <        (BiConsumer<? super T, ? super Throwable> action) {
1918 <        return doWhenComplete(action, ForkJoinPool.commonPool());
1916 >    public CompletableFuture<T> whenCompleteAsync(
1917 >        BiConsumer<? super T, ? super Throwable> action) {
1918 >        return doWhenComplete(action, asyncPool);
1919      }
1920  
1921 <    public CompletableFuture<T> whenCompleteAsync
1922 <        (BiConsumer<? super T, ? super Throwable> action,
2580 <         Executor executor) {
1921 >    public CompletableFuture<T> whenCompleteAsync(
1922 >        BiConsumer<? super T, ? super Throwable> action, Executor executor) {
1923          if (executor == null) throw new NullPointerException();
1924          return doWhenComplete(action, executor);
1925      }
1926  
1927 <    public <U> CompletableFuture<U> handle
1928 <        (BiFunction<? super T, Throwable, ? extends U> fn) {
1927 >    public <U> CompletableFuture<U> handle(
1928 >        BiFunction<? super T, Throwable, ? extends U> fn) {
1929          return doHandle(fn, null);
1930      }
1931  
1932 <    public <U> CompletableFuture<U> handleAsync
1933 <        (BiFunction<? super T, Throwable, ? extends U> fn) {
1934 <        return doHandle(fn, ForkJoinPool.commonPool());
1932 >    public <U> CompletableFuture<U> handleAsync(
1933 >        BiFunction<? super T, Throwable, ? extends U> fn) {
1934 >        return doHandle(fn, asyncPool);
1935      }
1936  
1937 <    public <U> CompletableFuture<U> handleAsync
1938 <        (BiFunction<? super T, Throwable, ? extends U> fn,
2597 <         Executor executor) {
1937 >    public <U> CompletableFuture<U> handleAsync(
1938 >        BiFunction<? super T, Throwable, ? extends U> fn, Executor executor) {
1939          if (executor == null) throw new NullPointerException();
1940          return doHandle(fn, executor);
1941      }
# Line 2625 | Line 1966 | public class CompletableFuture<T> implem
1966       * exceptionally
1967       * @return the new CompletableFuture
1968       */
1969 <    public CompletableFuture<T> exceptionally
1970 <        (Function<Throwable, ? extends T> fn) {
1971 <        if (fn == null) throw new NullPointerException();
2631 <        CompletableFuture<T> dst = new CompletableFuture<T>();
2632 <        Object r;
2633 <        if ((r = result) == null) {
2634 <            ExceptionCompletion<T> d =
2635 <                new ExceptionCompletion<T>(this, fn, dst);
2636 <            CompletionNode p = new CompletionNode(d);
2637 <            while (result == null) {
2638 <                if (UNSAFE.compareAndSwapObject(this, COMPLETIONS,
2639 <                                                p.next = completions, p))
2640 <                    break;
2641 <            }
2642 <            d.tryComplete();
2643 <        }
2644 <        else {
2645 <            T t = null; Throwable ex, dx = null;
2646 <            if (r instanceof AltResult) {
2647 <                if ((ex = ((AltResult)r).ex) != null) {
2648 <                    try {
2649 <                        t = fn.apply(ex);
2650 <                    } catch (Throwable rex) {
2651 <                        dx = rex;
2652 <                    }
2653 <                }
2654 <            }
2655 <            else {
2656 <                @SuppressWarnings("unchecked") T tr = (T) r;
2657 <                t = tr;
2658 <            }
2659 <            dst.internalComplete(t, dx);
2660 <        }
2661 <        return dst;
1969 >    public CompletableFuture<T> exceptionally(
1970 >        Function<Throwable, ? extends T> fn) {
1971 >        return doExceptionally(fn);
1972      }
1973  
1974      /* ------------- Arbitrary-arity constructions -------------- */
1975  
2666    /*
2667     * The basic plan of attack is to recursively form binary
2668     * completion trees of elements. This can be overkill for small
2669     * sets, but scales nicely. The And/All vs Or/Any forms use the
2670     * same idea, but details differ.
2671     */
2672
1976      /**
1977       * Returns a new CompletableFuture that is completed when all of
1978       * the given CompletableFutures complete.  If any of the given
# Line 2694 | Line 1997 | public class CompletableFuture<T> implem
1997       * {@code null}
1998       */
1999      public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs) {
2000 <        int len = cfs.length; // Directly handle empty and singleton cases
2698 <        if (len > 1)
2699 <            return allTree(cfs, 0, len - 1);
2700 <        else {
2701 <            CompletableFuture<Void> dst = new CompletableFuture<Void>();
2702 <            CompletableFuture<?> f;
2703 <            if (len == 0)
2704 <                dst.result = NIL;
2705 <            else if ((f = cfs[0]) == null)
2706 <                throw new NullPointerException();
2707 <            else {
2708 <                ThenPropagate d = null;
2709 <                CompletionNode p = null;
2710 <                Object r;
2711 <                while ((r = f.result) == null) {
2712 <                    if (d == null)
2713 <                        d = new ThenPropagate(f, dst);
2714 <                    else if (p == null)
2715 <                        p = new CompletionNode(d);
2716 <                    else if (UNSAFE.compareAndSwapObject
2717 <                             (f, COMPLETIONS, p.next = f.completions, p))
2718 <                        break;
2719 <                }
2720 <                if (d != null)
2721 <                    d.tryComplete();
2722 <                else
2723 <                    dst.internalComplete(null, (r instanceof AltResult) ?
2724 <                                         ((AltResult)r).ex : null);
2725 <            }
2726 <            return dst;
2727 <        }
2728 <    }
2729 <
2730 <    /**
2731 <     * Recursively constructs an And'ed tree of CompletableFutures.
2732 <     * Called only when array known to have at least two elements.
2733 <     */
2734 <    private static CompletableFuture<Void> allTree(CompletableFuture<?>[] cfs,
2735 <                                                   int lo, int hi) {
2736 <        CompletableFuture<?> fst, snd;
2737 <        int mid = (lo + hi) >>> 1;
2738 <        if ((fst = (lo == mid   ? cfs[lo] : allTree(cfs, lo,    mid))) == null ||
2739 <            (snd = (hi == mid+1 ? cfs[hi] : allTree(cfs, mid+1, hi))) == null)
2740 <            throw new NullPointerException();
2741 <        CompletableFuture<Void> dst = new CompletableFuture<Void>();
2742 <        AndCompletion d = null;
2743 <        CompletionNode p = null, q = null;
2744 <        Object r = null, s = null;
2745 <        while ((r = fst.result) == null || (s = snd.result) == null) {
2746 <            if (d == null)
2747 <                d = new AndCompletion(fst, snd, dst);
2748 <            else if (p == null)
2749 <                p = new CompletionNode(d);
2750 <            else if (q == null) {
2751 <                if (UNSAFE.compareAndSwapObject
2752 <                    (fst, COMPLETIONS, p.next = fst.completions, p))
2753 <                    q = new CompletionNode(d);
2754 <            }
2755 <            else if (UNSAFE.compareAndSwapObject
2756 <                     (snd, COMPLETIONS, q.next = snd.completions, q))
2757 <                break;
2758 <        }
2759 <        if (d != null)
2760 <            d.tryComplete();
2761 <        else {
2762 <            Throwable ex;
2763 <            if (r instanceof AltResult)
2764 <                ex = ((AltResult)r).ex;
2765 <            else
2766 <                ex = null;
2767 <            if (ex == null && (s instanceof AltResult))
2768 <                ex = ((AltResult)s).ex;
2769 <            dst.internalComplete(null, ex);
2770 <        }
2771 <        return dst;
2000 >        return doAllOf(cfs, 0, cfs.length - 1);
2001      }
2002  
2003      /**
# Line 2787 | Line 2016 | public class CompletableFuture<T> implem
2016       * {@code null}
2017       */
2018      public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs) {
2019 <        int len = cfs.length; // Same idea as allOf
2020 <        if (len > 1)
2021 <            return anyTree(cfs, 0, len - 1);
2022 <        else {
2023 <            CompletableFuture<Object> dst = new CompletableFuture<Object>();
2024 <            CompletableFuture<?> f;
2025 <            if (len == 0)
2026 <                ; // skip
2027 <            else if ((f = cfs[0]) == null)
2799 <                throw new NullPointerException();
2800 <            else {
2801 <                ThenCopy<Object> d = null;
2802 <                CompletionNode p = null;
2803 <                Object r;
2804 <                while ((r = f.result) == null) {
2805 <                    if (d == null)
2806 <                        d = new ThenCopy<Object>(f, dst);
2807 <                    else if (p == null)
2808 <                        p = new CompletionNode(d);
2809 <                    else if (UNSAFE.compareAndSwapObject
2810 <                             (f, COMPLETIONS, p.next = f.completions, p))
2811 <                        break;
2812 <                }
2813 <                if (d != null)
2814 <                    d.tryComplete();
2815 <                else {
2816 <                    Throwable ex; Object t;
2817 <                    if (r instanceof AltResult) {
2818 <                        ex = ((AltResult)r).ex;
2819 <                        t = null;
2820 <                    }
2821 <                    else {
2822 <                        ex = null;
2823 <                        t = r;
2824 <                    }
2825 <                    dst.internalComplete(t, ex);
2826 <                }
2827 <            }
2828 <            return dst;
2829 <        }
2830 <    }
2831 <
2832 <    /**
2833 <     * Recursively constructs an Or'ed tree of CompletableFutures.
2834 <     */
2835 <    private static CompletableFuture<Object> anyTree(CompletableFuture<?>[] cfs,
2836 <                                                     int lo, int hi) {
2837 <        CompletableFuture<?> fst, snd;
2838 <        int mid = (lo + hi) >>> 1;
2839 <        if ((fst = (lo == mid   ? cfs[lo] : anyTree(cfs, lo,    mid))) == null ||
2840 <            (snd = (hi == mid+1 ? cfs[hi] : anyTree(cfs, mid+1, hi))) == null)
2841 <            throw new NullPointerException();
2842 <        CompletableFuture<Object> dst = new CompletableFuture<Object>();
2843 <        OrCompletion d = null;
2844 <        CompletionNode p = null, q = null;
2845 <        Object r;
2846 <        while ((r = fst.result) == null && (r = snd.result) == null) {
2847 <            if (d == null)
2848 <                d = new OrCompletion(fst, snd, dst);
2849 <            else if (p == null)
2850 <                p = new CompletionNode(d);
2851 <            else if (q == null) {
2852 <                if (UNSAFE.compareAndSwapObject
2853 <                    (fst, COMPLETIONS, p.next = fst.completions, p))
2854 <                    q = new CompletionNode(d);
2855 <            }
2856 <            else if (UNSAFE.compareAndSwapObject
2857 <                     (snd, COMPLETIONS, q.next = snd.completions, q))
2858 <                break;
2859 <        }
2860 <        if (d != null)
2861 <            d.tryComplete();
2862 <        else {
2863 <            Throwable ex; Object t;
2864 <            if (r instanceof AltResult) {
2865 <                ex = ((AltResult)r).ex;
2866 <                t = null;
2867 <            }
2868 <            else {
2869 <                ex = null;
2870 <                t = r;
2019 >        CompletableFuture<Object> d = new CompletableFuture<Object>();
2020 >        for (int i = 0; i < cfs.length; ++i) {
2021 >            CompletableFuture<?> c = cfs[i];
2022 >            Object r = c.result; // throw NPE if null element
2023 >            if (d.result == null) {
2024 >                if (r == null)
2025 >                    c.unipush(new DelayedCopy<Object>(d, c));
2026 >                else
2027 >                    nowCopy(d, r);
2028              }
2872            dst.internalComplete(t, ex);
2029          }
2030 <        return dst;
2030 >        return d;
2031      }
2032  
2033      /* ------------- Control and status methods -------------- */
# Line 2891 | Line 2047 | public class CompletableFuture<T> implem
2047       */
2048      public boolean cancel(boolean mayInterruptIfRunning) {
2049          boolean cancelled = (result == null) &&
2050 <            UNSAFE.compareAndSwapObject
2051 <            (this, RESULT, null, new AltResult(new CancellationException()));
2896 <        postComplete(this);
2050 >            internalComplete(new AltResult(new CancellationException()));
2051 >        postComplete();
2052          return cancelled || isCancelled();
2053      }
2054  
# Line 2937 | Line 2092 | public class CompletableFuture<T> implem
2092       */
2093      public void obtrudeValue(T value) {
2094          result = (value == null) ? NIL : value;
2095 <        postComplete(this);
2095 >        postComplete();
2096      }
2097  
2098      /**
# Line 2953 | Line 2108 | public class CompletableFuture<T> implem
2108      public void obtrudeException(Throwable ex) {
2109          if (ex == null) throw new NullPointerException();
2110          result = new AltResult(ex);
2111 <        postComplete(this);
2111 >        postComplete();
2112      }
2113  
2114      /**
# Line 2966 | Line 2121 | public class CompletableFuture<T> implem
2121       */
2122      public int getNumberOfDependents() {
2123          int count = 0;
2124 <        for (CompletionNode p = completions; p != null; p = p.next)
2124 >        for (Completion<?> p = completions; p != null; p = p.next)
2125              ++count;
2126          return count;
2127      }
# Line 2997 | Line 2152 | public class CompletableFuture<T> implem
2152      // Unsafe mechanics
2153      private static final sun.misc.Unsafe UNSAFE;
2154      private static final long RESULT;
3000    private static final long WAITERS;
2155      private static final long COMPLETIONS;
2156      static {
2157          try {
# Line 3005 | Line 2159 | public class CompletableFuture<T> implem
2159              Class<?> k = CompletableFuture.class;
2160              RESULT = UNSAFE.objectFieldOffset
2161                  (k.getDeclaredField("result"));
3008            WAITERS = UNSAFE.objectFieldOffset
3009                (k.getDeclaredField("waiters"));
2162              COMPLETIONS = UNSAFE.objectFieldOffset
2163                  (k.getDeclaredField("completions"));
2164 <        } catch (Exception e) {
2165 <            throw new Error(e);
2164 >        } catch (Exception x) {
2165 >            throw new Error(x);
2166          }
2167      }
2168   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines