src/java.base/share/classes/java/util/concurrent/CompletableFuture.java
author dl
Fri, 28 Sep 2018 08:45:46 -0700
changeset 51950 a1c24d06e2b5
parent 49565 b5705ade8c8d
child 58893 ec954ef6caf1
permissions -rw-r--r--
8210971: Add exception handling methods to CompletionStage and CompletableFuture Reviewed-by: martin, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
     1
/*
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
     3
 *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
     9
 *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    14
 * accompanied this code).
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    15
 *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    19
 *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    22
 * questions.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    23
 */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    24
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    25
/*
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    29
 * file:
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    30
 *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    34
 */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    35
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    36
package java.util.concurrent;
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    37
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
    38
import java.lang.invoke.MethodHandles;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
    39
import java.lang.invoke.VarHandle;
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    40
import java.util.concurrent.locks.LockSupport;
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    41
import java.util.function.BiConsumer;
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    42
import java.util.function.BiFunction;
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    43
import java.util.function.Consumer;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    44
import java.util.function.Function;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    45
import java.util.function.Supplier;
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    46
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    47
/**
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    48
 * A {@link Future} that may be explicitly completed (setting its
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
    49
 * value and status), and may be used as a {@link CompletionStage},
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
    50
 * supporting dependent functions and actions that trigger upon its
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
    51
 * completion.
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    52
 *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    53
 * <p>When two or more threads attempt to
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    54
 * {@link #complete complete},
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    55
 * {@link #completeExceptionally completeExceptionally}, or
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    56
 * {@link #cancel cancel}
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    57
 * a CompletableFuture, only one of them succeeds.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    58
 *
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
    59
 * <p>In addition to these and related methods for directly
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
    60
 * manipulating status and results, CompletableFuture implements
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
    61
 * interface {@link CompletionStage} with the following policies: <ul>
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    62
 *
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
    63
 * <li>Actions supplied for dependent completions of
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
    64
 * <em>non-async</em> methods may be performed by the thread that
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
    65
 * completes the current CompletableFuture, or by any other caller of
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    66
 * a completion method.
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
    67
 *
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
    68
 * <li>All <em>async</em> methods without an explicit Executor
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
    69
 * argument are performed using the {@link ForkJoinPool#commonPool()}
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
    70
 * (unless it does not support a parallelism level of at least two, in
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    71
 * which case, a new Thread is created to run each task).  This may be
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    72
 * overridden for non-static methods in subclasses by defining method
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    73
 * {@link #defaultExecutor()}. To simplify monitoring, debugging,
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    74
 * and tracking, all generated asynchronous tasks are instances of the
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    75
 * marker interface {@link AsynchronousCompletionTask}.  Operations
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    76
 * with time-delays can use adapter methods defined in this class, for
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    77
 * example: {@code supplyAsync(supplier, delayedExecutor(timeout,
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    78
 * timeUnit))}.  To support methods with delays and timeouts, this
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    79
 * class maintains at most one daemon thread for triggering and
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    80
 * cancelling actions, not for running them.
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    81
 *
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
    82
 * <li>All CompletionStage methods are implemented independently of
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
    83
 * other public methods, so the behavior of one method is not impacted
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    84
 * by overrides of others in subclasses.
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    85
 *
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    86
 * <li>All CompletionStage methods return CompletableFutures.  To
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    87
 * restrict usages to only those methods defined in interface
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    88
 * CompletionStage, use method {@link #minimalCompletionStage}. Or to
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    89
 * ensure only that clients do not themselves modify a future, use
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    90
 * method {@link #copy}.
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
    91
 * </ul>
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    92
 *
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
    93
 * <p>CompletableFuture also implements {@link Future} with the following
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
    94
 * policies: <ul>
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
    95
 *
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
    96
 * <li>Since (unlike {@link FutureTask}) this class has no direct
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
    97
 * control over the computation that causes it to be completed,
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
    98
 * cancellation is treated as just another form of exceptional
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
    99
 * completion.  Method {@link #cancel cancel} has the same effect as
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
   100
 * {@code completeExceptionally(new CancellationException())}. Method
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
   101
 * {@link #isCompletedExceptionally} can be used to determine if a
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   102
 * CompletableFuture completed in any exceptional fashion.
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   103
 *
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
   104
 * <li>In case of exceptional completion with a CompletionException,
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   105
 * methods {@link #get()} and {@link #get(long, TimeUnit)} throw an
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   106
 * {@link ExecutionException} with the same cause as held in the
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
   107
 * corresponding CompletionException.  To simplify usage in most
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
   108
 * contexts, this class also defines methods {@link #join()} and
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
   109
 * {@link #getNow} that instead throw the CompletionException directly
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   110
 * in these cases.
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   111
 * </ul>
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   112
 *
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   113
 * <p>Arguments used to pass a completion result (that is, for
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   114
 * parameters of type {@code T}) for methods accepting them may be
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   115
 * null, but passing a null value for any other parameter will result
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   116
 * in a {@link NullPointerException} being thrown.
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   117
 *
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   118
 * <p>Subclasses of this class should normally override the "virtual
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   119
 * constructor" method {@link #newIncompleteFuture}, which establishes
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   120
 * the concrete type returned by CompletionStage methods. For example,
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   121
 * here is a class that substitutes a different default Executor and
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   122
 * disables the {@code obtrude} methods:
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   123
 *
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   124
 * <pre> {@code
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   125
 * class MyCompletableFuture<T> extends CompletableFuture<T> {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   126
 *   static final Executor myExecutor = ...;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   127
 *   public MyCompletableFuture() { }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   128
 *   public <U> CompletableFuture<U> newIncompleteFuture() {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   129
 *     return new MyCompletableFuture<U>(); }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   130
 *   public Executor defaultExecutor() {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   131
 *     return myExecutor; }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   132
 *   public void obtrudeValue(T value) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   133
 *     throw new UnsupportedOperationException(); }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   134
 *   public void obtrudeException(Throwable ex) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   135
 *     throw new UnsupportedOperationException(); }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   136
 * }}</pre>
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   137
 *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   138
 * @author Doug Lea
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   139
 * @param <T> The result type returned by this future's {@code join}
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   140
 * and {@code get} methods
40817
4f5fb115676d 8164169: Miscellaneous changes imported from jsr166 CVS 2016-09
dl
parents: 40734
diff changeset
   141
 * @since 1.8
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   142
 */
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
   143
public class CompletableFuture<T> implements Future<T>, CompletionStage<T> {
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   144
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   145
    /*
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   146
     * Overview:
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   147
     *
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   148
     * A CompletableFuture may have dependent completion actions,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   149
     * collected in a linked stack. It atomically completes by CASing
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   150
     * a result field, and then pops off and runs those actions. This
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   151
     * applies across normal vs exceptional outcomes, sync vs async
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   152
     * actions, binary triggers, and various forms of completions.
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   153
     *
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   154
     * Non-nullness of volatile field "result" indicates done.  It may
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   155
     * be set directly if known to be thread-confined, else via CAS.
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   156
     * An AltResult is used to box null as a result, as well as to
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   157
     * hold exceptions.  Using a single field makes completion simple
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   158
     * to detect and trigger.  Result encoding and decoding is
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   159
     * straightforward but tedious and adds to the sprawl of trapping
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   160
     * and associating exceptions with targets.  Minor simplifications
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   161
     * rely on (static) NIL (to box null results) being the only
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   162
     * AltResult with a null exception field, so we don't usually need
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   163
     * explicit comparisons.  Even though some of the generics casts
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   164
     * are unchecked (see SuppressWarnings annotations), they are
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   165
     * placed to be appropriate even if checked.
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   166
     *
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   167
     * Dependent actions are represented by Completion objects linked
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   168
     * as Treiber stacks headed by field "stack". There are Completion
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   169
     * classes for each kind of action, grouped into:
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   170
     * - single-input (UniCompletion),
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   171
     * - two-input (BiCompletion),
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   172
     * - projected (BiCompletions using exactly one of two inputs),
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   173
     * - shared (CoCompletion, used by the second of two sources),
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   174
     * - zero-input source actions,
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   175
     * - Signallers that unblock waiters.
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   176
     * Class Completion extends ForkJoinTask to enable async execution
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   177
     * (adding no space overhead because we exploit its "tag" methods
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   178
     * to maintain claims). It is also declared as Runnable to allow
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   179
     * usage with arbitrary executors.
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   180
     *
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   181
     * Support for each kind of CompletionStage relies on a separate
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   182
     * class, along with two CompletableFuture methods:
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   183
     *
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   184
     * * A Completion class with name X corresponding to function,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   185
     *   prefaced with "Uni", "Bi", or "Or". Each class contains
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   186
     *   fields for source(s), actions, and dependent. They are
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   187
     *   boringly similar, differing from others only with respect to
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   188
     *   underlying functional forms. We do this so that users don't
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   189
     *   encounter layers of adapters in common usages.
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   190
     *
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   191
     * * Boolean CompletableFuture method x(...) (for example
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   192
     *   biApply) takes all of the arguments needed to check that an
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   193
     *   action is triggerable, and then either runs the action or
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   194
     *   arranges its async execution by executing its Completion
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   195
     *   argument, if present. The method returns true if known to be
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   196
     *   complete.
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   197
     *
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   198
     * * Completion method tryFire(int mode) invokes the associated x
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   199
     *   method with its held arguments, and on success cleans up.
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   200
     *   The mode argument allows tryFire to be called twice (SYNC,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   201
     *   then ASYNC); the first to screen and trap exceptions while
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   202
     *   arranging to execute, and the second when called from a task.
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   203
     *   (A few classes are not used async so take slightly different
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   204
     *   forms.)  The claim() callback suppresses function invocation
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   205
     *   if already claimed by another thread.
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   206
     *
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   207
     * * Some classes (for example UniApply) have separate handling
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   208
     *   code for when known to be thread-confined ("now" methods) and
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   209
     *   for when shared (in tryFire), for efficiency.
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   210
     *
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   211
     * * CompletableFuture method xStage(...) is called from a public
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   212
     *   stage method of CompletableFuture f. It screens user
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   213
     *   arguments and invokes and/or creates the stage object.  If
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   214
     *   not async and already triggerable, the action is run
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   215
     *   immediately.  Otherwise a Completion c is created, and
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   216
     *   submitted to the executor if triggerable, or pushed onto f's
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   217
     *   stack if not.  Completion actions are started via c.tryFire.
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   218
     *   We recheck after pushing to a source future's stack to cover
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   219
     *   possible races if the source completes while pushing.
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   220
     *   Classes with two inputs (for example BiApply) deal with races
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   221
     *   across both while pushing actions.  The second completion is
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   222
     *   a CoCompletion pointing to the first, shared so that at most
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   223
     *   one performs the action.  The multiple-arity methods allOf
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
   224
     *   does this pairwise to form trees of completions.  Method
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
   225
     *   anyOf is handled differently from allOf because completion of
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
   226
     *   any source should trigger a cleanStack of other sources.
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
   227
     *   Each AnyOf completion can reach others via a shared array.
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   228
     *
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   229
     * Note that the generic type parameters of methods vary according
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   230
     * to whether "this" is a source, dependent, or completion.
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   231
     *
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   232
     * Method postComplete is called upon completion unless the target
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   233
     * is guaranteed not to be observable (i.e., not yet returned or
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   234
     * linked). Multiple threads can call postComplete, which
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   235
     * atomically pops each dependent action, and tries to trigger it
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   236
     * via method tryFire, in NESTED mode.  Triggering can propagate
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   237
     * recursively, so NESTED mode returns its completed dependent (if
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   238
     * one exists) for further processing by its caller (see method
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   239
     * postFire).
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   240
     *
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   241
     * Blocking methods get() and join() rely on Signaller Completions
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   242
     * that wake up waiting threads.  The mechanics are similar to
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   243
     * Treiber stack wait-nodes used in FutureTask, Phaser, and
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   244
     * SynchronousQueue. See their internal documentation for
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   245
     * algorithmic details.
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   246
     *
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   247
     * Without precautions, CompletableFutures would be prone to
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   248
     * garbage accumulation as chains of Completions build up, each
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   249
     * pointing back to its sources. So we null out fields as soon as
35980
1fc60a70d779 8139927: Improve documentation for CompletableFuture composition
dl
parents: 35302
diff changeset
   250
     * possible.  The screening checks needed anyway harmlessly ignore
1fc60a70d779 8139927: Improve documentation for CompletableFuture composition
dl
parents: 35302
diff changeset
   251
     * null arguments that may have been obtained during races with
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   252
     * threads nulling out fields.  We also try to unlink non-isLive
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   253
     * (fired or cancelled) Completions from stacks that might
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   254
     * otherwise never be popped: Method cleanStack always unlinks non
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   255
     * isLive completions from the head of stack; others may
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   256
     * occasionally remain if racing with other cancellations or
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   257
     * removals.
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   258
     *
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   259
     * Completion fields need not be declared as final or volatile
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   260
     * because they are only visible to other threads upon safe
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   261
     * publication.
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   262
     */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   263
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   264
    volatile Object result;       // Either the result or boxed AltResult
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   265
    volatile Completion stack;    // Top of Treiber stack of dependent actions
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   266
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   267
    final boolean internalComplete(Object r) { // CAS from null to r
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   268
        return RESULT.compareAndSet(this, null, r);
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   269
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   270
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   271
    /** Returns true if successfully pushed c onto stack. */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   272
    final boolean tryPushStack(Completion c) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   273
        Completion h = stack;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   274
        NEXT.set(c, h);         // CAS piggyback
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   275
        return STACK.compareAndSet(this, h, c);
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   276
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   277
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   278
    /** Unconditionally pushes c onto stack, retrying if necessary. */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   279
    final void pushStack(Completion c) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   280
        do {} while (!tryPushStack(c));
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   281
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   282
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   283
    /* ------------- Encoding and decoding outcomes -------------- */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   284
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   285
    static final class AltResult { // See above
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   286
        final Throwable ex;        // null only for NIL
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   287
        AltResult(Throwable x) { this.ex = x; }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   288
    }
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   289
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   290
    /** The encoding of the null value. */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   291
    static final AltResult NIL = new AltResult(null);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   292
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   293
    /** Completes with the null value, unless already completed. */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   294
    final boolean completeNull() {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   295
        return RESULT.compareAndSet(this, null, NIL);
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   296
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   297
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   298
    /** Returns the encoding of the given non-exceptional value. */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   299
    final Object encodeValue(T t) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   300
        return (t == null) ? NIL : t;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   301
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   302
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   303
    /** Completes with a non-exceptional result, unless already completed. */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   304
    final boolean completeValue(T t) {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   305
        return RESULT.compareAndSet(this, null, (t == null) ? NIL : t);
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   306
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   307
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   308
    /**
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   309
     * Returns the encoding of the given (non-null) exception as a
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   310
     * wrapped CompletionException unless it is one already.
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   311
     */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   312
    static AltResult encodeThrowable(Throwable x) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   313
        return new AltResult((x instanceof CompletionException) ? x :
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   314
                             new CompletionException(x));
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   315
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   316
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   317
    /** Completes with an exceptional result, unless already completed. */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   318
    final boolean completeThrowable(Throwable x) {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   319
        return RESULT.compareAndSet(this, null, encodeThrowable(x));
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   320
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   321
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   322
    /**
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   323
     * Returns the encoding of the given (non-null) exception as a
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   324
     * wrapped CompletionException unless it is one already.  May
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   325
     * return the given Object r (which must have been the result of a
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   326
     * source future) if it is equivalent, i.e. if this is a simple
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   327
     * relay of an existing CompletionException.
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   328
     */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   329
    static Object encodeThrowable(Throwable x, Object r) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   330
        if (!(x instanceof CompletionException))
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   331
            x = new CompletionException(x);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   332
        else if (r instanceof AltResult && x == ((AltResult)r).ex)
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   333
            return r;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   334
        return new AltResult(x);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   335
    }
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   336
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   337
    /**
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   338
     * Completes with the given (non-null) exceptional result as a
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   339
     * wrapped CompletionException unless it is one already, unless
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   340
     * already completed.  May complete with the given Object r
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   341
     * (which must have been the result of a source future) if it is
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   342
     * equivalent, i.e. if this is a simple propagation of an
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   343
     * existing CompletionException.
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   344
     */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   345
    final boolean completeThrowable(Throwable x, Object r) {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   346
        return RESULT.compareAndSet(this, null, encodeThrowable(x, r));
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   347
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   348
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   349
    /**
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   350
     * Returns the encoding of the given arguments: if the exception
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   351
     * is non-null, encodes as AltResult.  Otherwise uses the given
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   352
     * value, boxed as NIL if null.
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   353
     */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   354
    Object encodeOutcome(T t, Throwable x) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   355
        return (x == null) ? (t == null) ? NIL : t : encodeThrowable(x);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   356
    }
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   357
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   358
    /**
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   359
     * Returns the encoding of a copied outcome; if exceptional,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   360
     * rewraps as a CompletionException, else returns argument.
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   361
     */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   362
    static Object encodeRelay(Object r) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   363
        Throwable x;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   364
        if (r instanceof AltResult
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   365
            && (x = ((AltResult)r).ex) != null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   366
            && !(x instanceof CompletionException))
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   367
            r = new AltResult(new CompletionException(x));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   368
        return r;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   369
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   370
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   371
    /**
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   372
     * Completes with r or a copy of r, unless already completed.
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   373
     * If exceptional, r is first coerced to a CompletionException.
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   374
     */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   375
    final boolean completeRelay(Object r) {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   376
        return RESULT.compareAndSet(this, null, encodeRelay(r));
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   377
    }
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   378
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   379
    /**
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   380
     * Reports result using Future.get conventions.
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   381
     */
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   382
    private static Object reportGet(Object r)
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   383
        throws InterruptedException, ExecutionException {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   384
        if (r == null) // by convention below, null means interrupted
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   385
            throw new InterruptedException();
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   386
        if (r instanceof AltResult) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   387
            Throwable x, cause;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   388
            if ((x = ((AltResult)r).ex) == null)
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   389
                return null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   390
            if (x instanceof CancellationException)
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   391
                throw (CancellationException)x;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   392
            if ((x instanceof CompletionException) &&
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   393
                (cause = x.getCause()) != null)
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   394
                x = cause;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   395
            throw new ExecutionException(x);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   396
        }
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   397
        return r;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   398
    }
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   399
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   400
    /**
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   401
     * Decodes outcome to return result or throw unchecked exception.
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   402
     */
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   403
    private static Object reportJoin(Object r) {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   404
        if (r instanceof AltResult) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   405
            Throwable x;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   406
            if ((x = ((AltResult)r).ex) == null)
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   407
                return null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   408
            if (x instanceof CancellationException)
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   409
                throw (CancellationException)x;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   410
            if (x instanceof CompletionException)
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   411
                throw (CompletionException)x;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   412
            throw new CompletionException(x);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   413
        }
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   414
        return r;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   415
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   416
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   417
    /* ------------- Async task preliminaries -------------- */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   418
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   419
    /**
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   420
     * A marker interface identifying asynchronous tasks produced by
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   421
     * {@code async} methods. This may be useful for monitoring,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   422
     * debugging, and tracking asynchronous activities.
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   423
     *
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   424
     * @since 1.8
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   425
     */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   426
    public static interface AsynchronousCompletionTask {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   427
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   428
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   429
    private static final boolean USE_COMMON_POOL =
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   430
        (ForkJoinPool.getCommonPoolParallelism() > 1);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   431
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   432
    /**
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   433
     * Default executor -- ForkJoinPool.commonPool() unless it cannot
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   434
     * support parallelism.
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   435
     */
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   436
    private static final Executor ASYNC_POOL = USE_COMMON_POOL ?
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   437
        ForkJoinPool.commonPool() : new ThreadPerTaskExecutor();
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   438
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   439
    /** Fallback if ForkJoinPool.commonPool() cannot support parallelism */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   440
    static final class ThreadPerTaskExecutor implements Executor {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   441
        public void execute(Runnable r) { new Thread(r).start(); }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   442
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   443
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   444
    /**
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   445
     * Null-checks user executor argument, and translates uses of
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   446
     * commonPool to ASYNC_POOL in case parallelism disabled.
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   447
     */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   448
    static Executor screenExecutor(Executor e) {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   449
        if (!USE_COMMON_POOL && e == ForkJoinPool.commonPool())
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   450
            return ASYNC_POOL;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   451
        if (e == null) throw new NullPointerException();
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   452
        return e;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   453
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   454
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   455
    // Modes for Completion.tryFire. Signedness matters.
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   456
    static final int SYNC   =  0;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   457
    static final int ASYNC  =  1;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   458
    static final int NESTED = -1;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   459
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   460
    /* ------------- Base Completion classes and operations -------------- */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   461
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   462
    @SuppressWarnings("serial")
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   463
    abstract static class Completion extends ForkJoinTask<Void>
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   464
        implements Runnable, AsynchronousCompletionTask {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   465
        volatile Completion next;      // Treiber stack link
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   466
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   467
        /**
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   468
         * Performs completion action if triggered, returning a
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   469
         * dependent that may need propagation, if one exists.
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   470
         *
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   471
         * @param mode SYNC, ASYNC, or NESTED
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   472
         */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   473
        abstract CompletableFuture<?> tryFire(int mode);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   474
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   475
        /** Returns true if possibly still triggerable. Used by cleanStack. */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   476
        abstract boolean isLive();
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   477
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   478
        public final void run()                { tryFire(ASYNC); }
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   479
        public final boolean exec()            { tryFire(ASYNC); return false; }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   480
        public final Void getRawResult()       { return null; }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   481
        public final void setRawResult(Void v) {}
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   482
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   483
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   484
    /**
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   485
     * Pops and tries to trigger all reachable dependents.  Call only
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   486
     * when known to be done.
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   487
     */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   488
    final void postComplete() {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   489
        /*
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   490
         * On each step, variable f holds current dependents to pop
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   491
         * and run.  It is extended along only one path at a time,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   492
         * pushing others to avoid unbounded recursion.
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   493
         */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   494
        CompletableFuture<?> f = this; Completion h;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   495
        while ((h = f.stack) != null ||
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   496
               (f != this && (h = (f = this).stack) != null)) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   497
            CompletableFuture<?> d; Completion t;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   498
            if (STACK.compareAndSet(f, h, t = h.next)) {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   499
                if (t != null) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   500
                    if (f != this) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   501
                        pushStack(h);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   502
                        continue;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   503
                    }
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   504
                    NEXT.compareAndSet(h, t, null); // try to detach
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   505
                }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   506
                f = (d = h.tryFire(NESTED)) == null ? this : d;
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   507
            }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   508
        }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   509
    }
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   510
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   511
    /** Traverses stack and unlinks one or more dead Completions, if found. */
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   512
    final void cleanStack() {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   513
        Completion p = stack;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   514
        // ensure head of stack live
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   515
        for (boolean unlinked = false;;) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   516
            if (p == null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   517
                return;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   518
            else if (p.isLive()) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   519
                if (unlinked)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   520
                    return;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   521
                else
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   522
                    break;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   523
            }
40734
48879ea67e2a 8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents: 39778
diff changeset
   524
            else if (STACK.weakCompareAndSet(this, p, (p = p.next)))
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   525
                unlinked = true;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   526
            else
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   527
                p = stack;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   528
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   529
        // try to unlink first non-live
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   530
        for (Completion q = p.next; q != null;) {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   531
            Completion s = q.next;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   532
            if (q.isLive()) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   533
                p = q;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   534
                q = s;
40734
48879ea67e2a 8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents: 39778
diff changeset
   535
            } else if (NEXT.weakCompareAndSet(p, q, s))
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   536
                break;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   537
            else
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   538
                q = p.next;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   539
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   540
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   541
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   542
    /* ------------- One-input Completions -------------- */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   543
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   544
    /** A Completion with a source, dependent, and executor. */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   545
    @SuppressWarnings("serial")
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   546
    abstract static class UniCompletion<T,V> extends Completion {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   547
        Executor executor;                 // executor to use (null if none)
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   548
        CompletableFuture<V> dep;          // the dependent to complete
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   549
        CompletableFuture<T> src;          // source for action
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   550
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   551
        UniCompletion(Executor executor, CompletableFuture<V> dep,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   552
                      CompletableFuture<T> src) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   553
            this.executor = executor; this.dep = dep; this.src = src;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   554
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   555
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   556
        /**
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   557
         * Returns true if action can be run. Call only when known to
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   558
         * be triggerable. Uses FJ tag bit to ensure that only one
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   559
         * thread claims ownership.  If async, starts as task -- a
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   560
         * later call to tryFire will run action.
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   561
         */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   562
        final boolean claim() {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   563
            Executor e = executor;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   564
            if (compareAndSetForkJoinTaskTag((short)0, (short)1)) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   565
                if (e == null)
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   566
                    return true;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   567
                executor = null; // disable
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   568
                e.execute(this);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   569
            }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   570
            return false;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   571
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   572
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   573
        final boolean isLive() { return dep != null; }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   574
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   575
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   576
    /**
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   577
     * Pushes the given completion unless it completes while trying.
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   578
     * Caller should first check that result is null.
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   579
     */
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   580
    final void unipush(Completion c) {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   581
        if (c != null) {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   582
            while (!tryPushStack(c)) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   583
                if (result != null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   584
                    NEXT.set(c, null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   585
                    break;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   586
                }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   587
            }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   588
            if (result != null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   589
                c.tryFire(SYNC);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   590
        }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   591
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   592
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   593
    /**
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
   594
     * Post-processing by dependent after successful UniCompletion tryFire.
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
   595
     * Tries to clean stack of source a, and then either runs postComplete
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
   596
     * or returns this to caller, depending on mode.
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   597
     */
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   598
    final CompletableFuture<T> postFire(CompletableFuture<?> a, int mode) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   599
        if (a != null && a.stack != null) {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   600
            Object r;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   601
            if ((r = a.result) == null)
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   602
                a.cleanStack();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   603
            if (mode >= 0 && (r != null || a.result != null))
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   604
                a.postComplete();
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   605
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   606
        if (result != null && stack != null) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   607
            if (mode < 0)
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   608
                return this;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   609
            else
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   610
                postComplete();
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   611
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   612
        return null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   613
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   614
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   615
    @SuppressWarnings("serial")
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   616
    static final class UniApply<T,V> extends UniCompletion<T,V> {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   617
        Function<? super T,? extends V> fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   618
        UniApply(Executor executor, CompletableFuture<V> dep,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   619
                 CompletableFuture<T> src,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   620
                 Function<? super T,? extends V> fn) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   621
            super(executor, dep, src); this.fn = fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   622
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   623
        final CompletableFuture<V> tryFire(int mode) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   624
            CompletableFuture<V> d; CompletableFuture<T> a;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   625
            Object r; Throwable x; Function<? super T,? extends V> f;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   626
            if ((d = dep) == null || (f = fn) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   627
                || (a = src) == null || (r = a.result) == null)
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   628
                return null;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   629
            tryComplete: if (d.result == null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   630
                if (r instanceof AltResult) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   631
                    if ((x = ((AltResult)r).ex) != null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   632
                        d.completeThrowable(x, r);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   633
                        break tryComplete;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   634
                    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   635
                    r = null;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   636
                }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   637
                try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   638
                    if (mode <= 0 && !claim())
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   639
                        return null;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   640
                    else {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   641
                        @SuppressWarnings("unchecked") T t = (T) r;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   642
                        d.completeValue(f.apply(t));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   643
                    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   644
                } catch (Throwable ex) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   645
                    d.completeThrowable(ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   646
                }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   647
            }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   648
            dep = null; src = null; fn = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   649
            return d.postFire(a, mode);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   650
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   651
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   652
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   653
    private <V> CompletableFuture<V> uniApplyStage(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   654
        Executor e, Function<? super T,? extends V> f) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   655
        if (f == null) throw new NullPointerException();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   656
        Object r;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   657
        if ((r = result) != null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   658
            return uniApplyNow(r, e, f);
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   659
        CompletableFuture<V> d = newIncompleteFuture();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   660
        unipush(new UniApply<T,V>(e, d, this, f));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   661
        return d;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   662
    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   663
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   664
    private <V> CompletableFuture<V> uniApplyNow(
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   665
        Object r, Executor e, Function<? super T,? extends V> f) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   666
        Throwable x;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   667
        CompletableFuture<V> d = newIncompleteFuture();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   668
        if (r instanceof AltResult) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   669
            if ((x = ((AltResult)r).ex) != null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   670
                d.result = encodeThrowable(x, r);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   671
                return d;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   672
            }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   673
            r = null;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   674
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   675
        try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   676
            if (e != null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   677
                e.execute(new UniApply<T,V>(null, d, this, f));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   678
            } else {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   679
                @SuppressWarnings("unchecked") T t = (T) r;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   680
                d.result = d.encodeValue(f.apply(t));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   681
            }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   682
        } catch (Throwable ex) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   683
            d.result = encodeThrowable(ex);
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   684
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   685
        return d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   686
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   687
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   688
    @SuppressWarnings("serial")
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   689
    static final class UniAccept<T> extends UniCompletion<T,Void> {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   690
        Consumer<? super T> fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   691
        UniAccept(Executor executor, CompletableFuture<Void> dep,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   692
                  CompletableFuture<T> src, Consumer<? super T> fn) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   693
            super(executor, dep, src); this.fn = fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   694
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   695
        final CompletableFuture<Void> tryFire(int mode) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   696
            CompletableFuture<Void> d; CompletableFuture<T> a;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   697
            Object r; Throwable x; Consumer<? super T> f;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   698
            if ((d = dep) == null || (f = fn) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   699
                || (a = src) == null || (r = a.result) == null)
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   700
                return null;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   701
            tryComplete: if (d.result == null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   702
                if (r instanceof AltResult) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   703
                    if ((x = ((AltResult)r).ex) != null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   704
                        d.completeThrowable(x, r);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   705
                        break tryComplete;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   706
                    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   707
                    r = null;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   708
                }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   709
                try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   710
                    if (mode <= 0 && !claim())
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   711
                        return null;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   712
                    else {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   713
                        @SuppressWarnings("unchecked") T t = (T) r;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   714
                        f.accept(t);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   715
                        d.completeNull();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   716
                    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   717
                } catch (Throwable ex) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   718
                    d.completeThrowable(ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   719
                }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   720
            }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   721
            dep = null; src = null; fn = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   722
            return d.postFire(a, mode);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   723
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   724
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   725
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   726
    private CompletableFuture<Void> uniAcceptStage(Executor e,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   727
                                                   Consumer<? super T> f) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   728
        if (f == null) throw new NullPointerException();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   729
        Object r;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   730
        if ((r = result) != null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   731
            return uniAcceptNow(r, e, f);
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   732
        CompletableFuture<Void> d = newIncompleteFuture();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   733
        unipush(new UniAccept<T>(e, d, this, f));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   734
        return d;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   735
    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   736
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   737
    private CompletableFuture<Void> uniAcceptNow(
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   738
        Object r, Executor e, Consumer<? super T> f) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   739
        Throwable x;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   740
        CompletableFuture<Void> d = newIncompleteFuture();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   741
        if (r instanceof AltResult) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   742
            if ((x = ((AltResult)r).ex) != null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   743
                d.result = encodeThrowable(x, r);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   744
                return d;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   745
            }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   746
            r = null;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   747
        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   748
        try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   749
            if (e != null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   750
                e.execute(new UniAccept<T>(null, d, this, f));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   751
            } else {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   752
                @SuppressWarnings("unchecked") T t = (T) r;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   753
                f.accept(t);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   754
                d.result = NIL;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   755
            }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   756
        } catch (Throwable ex) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   757
            d.result = encodeThrowable(ex);
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   758
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   759
        return d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   760
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   761
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   762
    @SuppressWarnings("serial")
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   763
    static final class UniRun<T> extends UniCompletion<T,Void> {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   764
        Runnable fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   765
        UniRun(Executor executor, CompletableFuture<Void> dep,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   766
               CompletableFuture<T> src, Runnable fn) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   767
            super(executor, dep, src); this.fn = fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   768
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   769
        final CompletableFuture<Void> tryFire(int mode) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   770
            CompletableFuture<Void> d; CompletableFuture<T> a;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   771
            Object r; Throwable x; Runnable f;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   772
            if ((d = dep) == null || (f = fn) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   773
                || (a = src) == null || (r = a.result) == null)
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   774
                return null;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   775
            if (d.result == null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   776
                if (r instanceof AltResult && (x = ((AltResult)r).ex) != null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   777
                    d.completeThrowable(x, r);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   778
                else
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   779
                    try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   780
                        if (mode <= 0 && !claim())
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   781
                            return null;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   782
                        else {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   783
                            f.run();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   784
                            d.completeNull();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   785
                        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   786
                    } catch (Throwable ex) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   787
                        d.completeThrowable(ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   788
                    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   789
            }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   790
            dep = null; src = null; fn = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   791
            return d.postFire(a, mode);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   792
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   793
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   794
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   795
    private CompletableFuture<Void> uniRunStage(Executor e, Runnable f) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   796
        if (f == null) throw new NullPointerException();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   797
        Object r;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   798
        if ((r = result) != null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   799
            return uniRunNow(r, e, f);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   800
        CompletableFuture<Void> d = newIncompleteFuture();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   801
        unipush(new UniRun<T>(e, d, this, f));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   802
        return d;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   803
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   804
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   805
    private CompletableFuture<Void> uniRunNow(Object r, Executor e, Runnable f) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   806
        Throwable x;
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   807
        CompletableFuture<Void> d = newIncompleteFuture();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   808
        if (r instanceof AltResult && (x = ((AltResult)r).ex) != null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   809
            d.result = encodeThrowable(x, r);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   810
        else
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   811
            try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   812
                if (e != null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   813
                    e.execute(new UniRun<T>(null, d, this, f));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   814
                } else {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   815
                    f.run();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   816
                    d.result = NIL;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   817
                }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   818
            } catch (Throwable ex) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   819
                d.result = encodeThrowable(ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   820
            }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   821
        return d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   822
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   823
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   824
    @SuppressWarnings("serial")
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   825
    static final class UniWhenComplete<T> extends UniCompletion<T,T> {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   826
        BiConsumer<? super T, ? super Throwable> fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   827
        UniWhenComplete(Executor executor, CompletableFuture<T> dep,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   828
                        CompletableFuture<T> src,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   829
                        BiConsumer<? super T, ? super Throwable> fn) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   830
            super(executor, dep, src); this.fn = fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   831
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   832
        final CompletableFuture<T> tryFire(int mode) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   833
            CompletableFuture<T> d; CompletableFuture<T> a;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   834
            Object r; BiConsumer<? super T, ? super Throwable> f;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   835
            if ((d = dep) == null || (f = fn) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   836
                || (a = src) == null || (r = a.result) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   837
                || !d.uniWhenComplete(r, f, mode > 0 ? null : this))
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   838
                return null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   839
            dep = null; src = null; fn = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   840
            return d.postFire(a, mode);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   841
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   842
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   843
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   844
    final boolean uniWhenComplete(Object r,
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   845
                                  BiConsumer<? super T,? super Throwable> f,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   846
                                  UniWhenComplete<T> c) {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   847
        T t; Throwable x = null;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   848
        if (result == null) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   849
            try {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   850
                if (c != null && !c.claim())
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   851
                    return false;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   852
                if (r instanceof AltResult) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   853
                    x = ((AltResult)r).ex;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   854
                    t = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   855
                } else {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   856
                    @SuppressWarnings("unchecked") T tr = (T) r;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   857
                    t = tr;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   858
                }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   859
                f.accept(t, x);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   860
                if (x == null) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   861
                    internalComplete(r);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   862
                    return true;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   863
                }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   864
            } catch (Throwable ex) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   865
                if (x == null)
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   866
                    x = ex;
35980
1fc60a70d779 8139927: Improve documentation for CompletableFuture composition
dl
parents: 35302
diff changeset
   867
                else if (x != ex)
1fc60a70d779 8139927: Improve documentation for CompletableFuture composition
dl
parents: 35302
diff changeset
   868
                    x.addSuppressed(ex);
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   869
            }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   870
            completeThrowable(x, r);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   871
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   872
        return true;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   873
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   874
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   875
    private CompletableFuture<T> uniWhenCompleteStage(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   876
        Executor e, BiConsumer<? super T, ? super Throwable> f) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   877
        if (f == null) throw new NullPointerException();
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   878
        CompletableFuture<T> d = newIncompleteFuture();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   879
        Object r;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   880
        if ((r = result) == null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   881
            unipush(new UniWhenComplete<T>(e, d, this, f));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   882
        else if (e == null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   883
            d.uniWhenComplete(r, f, null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   884
        else {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   885
            try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   886
                e.execute(new UniWhenComplete<T>(null, d, this, f));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   887
            } catch (Throwable ex) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   888
                d.result = encodeThrowable(ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   889
            }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   890
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   891
        return d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   892
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   893
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   894
    @SuppressWarnings("serial")
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   895
    static final class UniHandle<T,V> extends UniCompletion<T,V> {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   896
        BiFunction<? super T, Throwable, ? extends V> fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   897
        UniHandle(Executor executor, CompletableFuture<V> dep,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   898
                  CompletableFuture<T> src,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   899
                  BiFunction<? super T, Throwable, ? extends V> fn) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   900
            super(executor, dep, src); this.fn = fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   901
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   902
        final CompletableFuture<V> tryFire(int mode) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   903
            CompletableFuture<V> d; CompletableFuture<T> a;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   904
            Object r; BiFunction<? super T, Throwable, ? extends V> f;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   905
            if ((d = dep) == null || (f = fn) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   906
                || (a = src) == null || (r = a.result) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   907
                || !d.uniHandle(r, f, mode > 0 ? null : this))
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   908
                return null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   909
            dep = null; src = null; fn = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   910
            return d.postFire(a, mode);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   911
        }
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   912
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
   913
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   914
    final <S> boolean uniHandle(Object r,
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   915
                                BiFunction<? super S, Throwable, ? extends T> f,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   916
                                UniHandle<S,T> c) {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   917
        S s; Throwable x;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   918
        if (result == null) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   919
            try {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   920
                if (c != null && !c.claim())
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   921
                    return false;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   922
                if (r instanceof AltResult) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   923
                    x = ((AltResult)r).ex;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   924
                    s = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   925
                } else {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   926
                    x = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   927
                    @SuppressWarnings("unchecked") S ss = (S) r;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   928
                    s = ss;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   929
                }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   930
                completeValue(f.apply(s, x));
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   931
            } catch (Throwable ex) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   932
                completeThrowable(ex);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   933
            }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   934
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   935
        return true;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   936
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   937
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   938
    private <V> CompletableFuture<V> uniHandleStage(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   939
        Executor e, BiFunction<? super T, Throwable, ? extends V> f) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   940
        if (f == null) throw new NullPointerException();
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   941
        CompletableFuture<V> d = newIncompleteFuture();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   942
        Object r;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   943
        if ((r = result) == null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   944
            unipush(new UniHandle<T,V>(e, d, this, f));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   945
        else if (e == null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   946
            d.uniHandle(r, f, null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   947
        else {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   948
            try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   949
                e.execute(new UniHandle<T,V>(null, d, this, f));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   950
            } catch (Throwable ex) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   951
                d.result = encodeThrowable(ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   952
            }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   953
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   954
        return d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   955
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   956
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   957
    @SuppressWarnings("serial")
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   958
    static final class UniExceptionally<T> extends UniCompletion<T,T> {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   959
        Function<? super Throwable, ? extends T> fn;
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
   960
        UniExceptionally(Executor executor,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
   961
                         CompletableFuture<T> dep, CompletableFuture<T> src,
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   962
                         Function<? super Throwable, ? extends T> fn) {
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
   963
            super(executor, dep, src); this.fn = fn;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   964
        }
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
   965
        final CompletableFuture<T> tryFire(int mode) {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   966
            CompletableFuture<T> d; CompletableFuture<T> a;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   967
            Object r; Function<? super Throwable, ? extends T> f;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   968
            if ((d = dep) == null || (f = fn) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   969
                || (a = src) == null || (r = a.result) == null
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
   970
                || !d.uniExceptionally(r, f, mode > 0 ? null : this))
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   971
                return null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   972
            dep = null; src = null; fn = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   973
            return d.postFire(a, mode);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   974
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   975
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   976
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   977
    final boolean uniExceptionally(Object r,
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   978
                                   Function<? super Throwable, ? extends T> f,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   979
                                   UniExceptionally<T> c) {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
   980
        Throwable x;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   981
        if (result == null) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   982
            try {
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
   983
                if (c != null && !c.claim())
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
   984
                    return false;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
   985
                if (r instanceof AltResult && (x = ((AltResult)r).ex) != null)
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   986
                    completeValue(f.apply(x));
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
   987
                else
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   988
                    internalComplete(r);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   989
            } catch (Throwable ex) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   990
                completeThrowable(ex);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   991
            }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   992
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   993
        return true;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   994
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   995
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   996
    private CompletableFuture<T> uniExceptionallyStage(
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
   997
        Executor e, Function<Throwable, ? extends T> f) {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
   998
        if (f == null) throw new NullPointerException();
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
   999
        CompletableFuture<T> d = newIncompleteFuture();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1000
        Object r;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1001
        if ((r = result) == null)
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1002
            unipush(new UniExceptionally<T>(e, d, this, f));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1003
        else if (e == null)
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1004
            d.uniExceptionally(r, f, null);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1005
        else {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1006
            try {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1007
                e.execute(new UniExceptionally<T>(null, d, this, f));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1008
            } catch (Throwable ex) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1009
                d.result = encodeThrowable(ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1010
            }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1011
        }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1012
        return d;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1013
    }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1014
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1015
    @SuppressWarnings("serial")
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1016
    static final class UniComposeExceptionally<T> extends UniCompletion<T,T> {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1017
        Function<Throwable, ? extends CompletionStage<T>> fn;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1018
        UniComposeExceptionally(Executor executor, CompletableFuture<T> dep,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1019
                                CompletableFuture<T> src,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1020
                                Function<Throwable, ? extends CompletionStage<T>> fn) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1021
            super(executor, dep, src); this.fn = fn;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1022
        }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1023
        final CompletableFuture<T> tryFire(int mode) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1024
            CompletableFuture<T> d; CompletableFuture<T> a;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1025
            Function<Throwable, ? extends CompletionStage<T>> f;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1026
            Object r; Throwable x;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1027
            if ((d = dep) == null || (f = fn) == null
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1028
                || (a = src) == null || (r = a.result) == null)
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1029
                return null;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1030
            if (d.result == null) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1031
                if ((r instanceof AltResult) &&
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1032
                    (x = ((AltResult)r).ex) != null) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1033
                    try {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1034
                        if (mode <= 0 && !claim())
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1035
                            return null;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1036
                        CompletableFuture<T> g = f.apply(x).toCompletableFuture();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1037
                        if ((r = g.result) != null)
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1038
                            d.completeRelay(r);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1039
                        else {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1040
                            g.unipush(new UniRelay<T,T>(d, g));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1041
                            if (d.result == null)
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1042
                                return null;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1043
                        }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1044
                    } catch (Throwable ex) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1045
                        d.completeThrowable(ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1046
                    }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1047
                }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1048
                else
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1049
                    d.internalComplete(r);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1050
            }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1051
            dep = null; src = null; fn = null;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1052
            return d.postFire(a, mode);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1053
        }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1054
    }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1055
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1056
    private CompletableFuture<T> uniComposeExceptionallyStage(
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1057
        Executor e, Function<Throwable, ? extends CompletionStage<T>> f) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1058
        if (f == null) throw new NullPointerException();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1059
        CompletableFuture<T> d = newIncompleteFuture();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1060
        Object r, s; Throwable x;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1061
        if ((r = result) == null)
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1062
            unipush(new UniComposeExceptionally<T>(e, d, this, f));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1063
        else if (!(r instanceof AltResult) || (x = ((AltResult)r).ex) == null)
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1064
            d.internalComplete(r);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1065
        else
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1066
            try {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1067
                if (e != null)
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1068
                    e.execute(new UniComposeExceptionally<T>(null, d, this, f));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1069
                else {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1070
                    CompletableFuture<T> g = f.apply(x).toCompletableFuture();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1071
                    if ((s = g.result) != null)
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1072
                        d.result = encodeRelay(s);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1073
                    else
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1074
                        g.unipush(new UniRelay<T,T>(d, g));
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1075
                }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1076
            } catch (Throwable ex) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1077
                d.result = encodeThrowable(ex);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1078
            }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1079
        return d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1080
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1081
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1082
    @SuppressWarnings("serial")
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1083
    static final class UniRelay<U, T extends U> extends UniCompletion<T,U> {
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1084
        UniRelay(CompletableFuture<U> dep, CompletableFuture<T> src) {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1085
            super(null, dep, src);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1086
        }
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1087
        final CompletableFuture<U> tryFire(int mode) {
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1088
            CompletableFuture<U> d; CompletableFuture<T> a; Object r;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1089
            if ((d = dep) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1090
                || (a = src) == null || (r = a.result) == null)
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1091
                return null;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1092
            if (d.result == null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1093
                d.completeRelay(r);
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1094
            src = null; dep = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1095
            return d.postFire(a, mode);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1096
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1097
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1098
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1099
    private static <U, T extends U> CompletableFuture<U> uniCopyStage(
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1100
        CompletableFuture<T> src) {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1101
        Object r;
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1102
        CompletableFuture<U> d = src.newIncompleteFuture();
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1103
        if ((r = src.result) != null)
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1104
            d.result = encodeRelay(r);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1105
        else
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1106
            src.unipush(new UniRelay<U,T>(d, src));
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1107
        return d;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1108
    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1109
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1110
    private MinimalStage<T> uniAsMinimalStage() {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1111
        Object r;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1112
        if ((r = result) != null)
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1113
            return new MinimalStage<T>(encodeRelay(r));
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1114
        MinimalStage<T> d = new MinimalStage<T>();
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1115
        unipush(new UniRelay<T,T>(d, this));
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1116
        return d;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1117
    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1118
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1119
    @SuppressWarnings("serial")
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1120
    static final class UniCompose<T,V> extends UniCompletion<T,V> {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1121
        Function<? super T, ? extends CompletionStage<V>> fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1122
        UniCompose(Executor executor, CompletableFuture<V> dep,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1123
                   CompletableFuture<T> src,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1124
                   Function<? super T, ? extends CompletionStage<V>> fn) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1125
            super(executor, dep, src); this.fn = fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1126
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1127
        final CompletableFuture<V> tryFire(int mode) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1128
            CompletableFuture<V> d; CompletableFuture<T> a;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1129
            Function<? super T, ? extends CompletionStage<V>> f;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1130
            Object r; Throwable x;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1131
            if ((d = dep) == null || (f = fn) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1132
                || (a = src) == null || (r = a.result) == null)
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1133
                return null;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1134
            tryComplete: if (d.result == null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1135
                if (r instanceof AltResult) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1136
                    if ((x = ((AltResult)r).ex) != null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1137
                        d.completeThrowable(x, r);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1138
                        break tryComplete;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1139
                    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1140
                    r = null;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1141
                }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1142
                try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1143
                    if (mode <= 0 && !claim())
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1144
                        return null;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1145
                    @SuppressWarnings("unchecked") T t = (T) r;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1146
                    CompletableFuture<V> g = f.apply(t).toCompletableFuture();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1147
                    if ((r = g.result) != null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1148
                        d.completeRelay(r);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1149
                    else {
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1150
                        g.unipush(new UniRelay<V,V>(d, g));
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1151
                        if (d.result == null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1152
                            return null;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1153
                    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1154
                } catch (Throwable ex) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1155
                    d.completeThrowable(ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1156
                }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1157
            }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1158
            dep = null; src = null; fn = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1159
            return d.postFire(a, mode);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1160
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1161
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1162
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1163
    private <V> CompletableFuture<V> uniComposeStage(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1164
        Executor e, Function<? super T, ? extends CompletionStage<V>> f) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1165
        if (f == null) throw new NullPointerException();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1166
        CompletableFuture<V> d = newIncompleteFuture();
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1167
        Object r, s; Throwable x;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1168
        if ((r = result) == null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1169
            unipush(new UniCompose<T,V>(e, d, this, f));
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1170
        else {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1171
            if (r instanceof AltResult) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1172
                if ((x = ((AltResult)r).ex) != null) {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1173
                    d.result = encodeThrowable(x, r);
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1174
                    return d;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1175
                }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1176
                r = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1177
            }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1178
            try {
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1179
                if (e != null)
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1180
                    e.execute(new UniCompose<T,V>(null, d, this, f));
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1181
                else {
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1182
                    @SuppressWarnings("unchecked") T t = (T) r;
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1183
                    CompletableFuture<V> g = f.apply(t).toCompletableFuture();
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1184
                    if ((s = g.result) != null)
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1185
                        d.result = encodeRelay(s);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1186
                    else
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1187
                        g.unipush(new UniRelay<V,V>(d, g));
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1188
                }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1189
            } catch (Throwable ex) {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1190
                d.result = encodeThrowable(ex);
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1191
            }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1192
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1193
        return d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1194
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1195
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1196
    /* ------------- Two-input Completions -------------- */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1197
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1198
    /** A Completion for an action with two sources */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1199
    @SuppressWarnings("serial")
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1200
    abstract static class BiCompletion<T,U,V> extends UniCompletion<T,V> {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1201
        CompletableFuture<U> snd; // second source for action
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1202
        BiCompletion(Executor executor, CompletableFuture<V> dep,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1203
                     CompletableFuture<T> src, CompletableFuture<U> snd) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1204
            super(executor, dep, src); this.snd = snd;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1205
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1206
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1207
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1208
    /** A Completion delegating to a BiCompletion */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1209
    @SuppressWarnings("serial")
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1210
    static final class CoCompletion extends Completion {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1211
        BiCompletion<?,?,?> base;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1212
        CoCompletion(BiCompletion<?,?,?> base) { this.base = base; }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1213
        final CompletableFuture<?> tryFire(int mode) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1214
            BiCompletion<?,?,?> c; CompletableFuture<?> d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1215
            if ((c = base) == null || (d = c.tryFire(mode)) == null)
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1216
                return null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1217
            base = null; // detach
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1218
            return d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1219
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1220
        final boolean isLive() {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1221
            BiCompletion<?,?,?> c;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1222
            return (c = base) != null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1223
                // && c.isLive()
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1224
                && c.dep != null;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1225
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1226
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1227
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1228
    /**
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1229
     * Pushes completion to this and b unless both done.
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1230
     * Caller should first check that either result or b.result is null.
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1231
     */
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1232
    final void bipush(CompletableFuture<?> b, BiCompletion<?,?,?> c) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1233
        if (c != null) {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1234
            while (result == null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1235
                if (tryPushStack(c)) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1236
                    if (b.result == null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1237
                        b.unipush(new CoCompletion(c));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1238
                    else if (result != null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1239
                        c.tryFire(SYNC);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1240
                    return;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1241
                }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1242
            }
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1243
            b.unipush(c);
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1244
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1245
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1246
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1247
    /** Post-processing after successful BiCompletion tryFire. */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1248
    final CompletableFuture<T> postFire(CompletableFuture<?> a,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1249
                                        CompletableFuture<?> b, int mode) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1250
        if (b != null && b.stack != null) { // clean second source
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1251
            Object r;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1252
            if ((r = b.result) == null)
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1253
                b.cleanStack();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1254
            if (mode >= 0 && (r != null || b.result != null))
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1255
                b.postComplete();
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1256
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1257
        return postFire(a, mode);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1258
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1259
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1260
    @SuppressWarnings("serial")
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1261
    static final class BiApply<T,U,V> extends BiCompletion<T,U,V> {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1262
        BiFunction<? super T,? super U,? extends V> fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1263
        BiApply(Executor executor, CompletableFuture<V> dep,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1264
                CompletableFuture<T> src, CompletableFuture<U> snd,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1265
                BiFunction<? super T,? super U,? extends V> fn) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1266
            super(executor, dep, src, snd); this.fn = fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1267
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1268
        final CompletableFuture<V> tryFire(int mode) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1269
            CompletableFuture<V> d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1270
            CompletableFuture<T> a;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1271
            CompletableFuture<U> b;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1272
            Object r, s; BiFunction<? super T,? super U,? extends V> f;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1273
            if ((d = dep) == null || (f = fn) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1274
                || (a = src) == null || (r = a.result) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1275
                || (b = snd) == null || (s = b.result) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1276
                || !d.biApply(r, s, f, mode > 0 ? null : this))
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1277
                return null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1278
            dep = null; src = null; snd = null; fn = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1279
            return d.postFire(a, b, mode);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1280
        }
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1281
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1282
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1283
    final <R,S> boolean biApply(Object r, Object s,
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1284
                                BiFunction<? super R,? super S,? extends T> f,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1285
                                BiApply<R,S,T> c) {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1286
        Throwable x;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1287
        tryComplete: if (result == null) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1288
            if (r instanceof AltResult) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1289
                if ((x = ((AltResult)r).ex) != null) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1290
                    completeThrowable(x, r);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1291
                    break tryComplete;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1292
                }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1293
                r = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1294
            }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1295
            if (s instanceof AltResult) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1296
                if ((x = ((AltResult)s).ex) != null) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1297
                    completeThrowable(x, s);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1298
                    break tryComplete;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1299
                }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1300
                s = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1301
            }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1302
            try {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1303
                if (c != null && !c.claim())
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1304
                    return false;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1305
                @SuppressWarnings("unchecked") R rr = (R) r;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1306
                @SuppressWarnings("unchecked") S ss = (S) s;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1307
                completeValue(f.apply(rr, ss));
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1308
            } catch (Throwable ex) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1309
                completeThrowable(ex);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1310
            }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1311
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1312
        return true;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1313
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1314
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1315
    private <U,V> CompletableFuture<V> biApplyStage(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1316
        Executor e, CompletionStage<U> o,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1317
        BiFunction<? super T,? super U,? extends V> f) {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1318
        CompletableFuture<U> b; Object r, s;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1319
        if (f == null || (b = o.toCompletableFuture()) == null)
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1320
            throw new NullPointerException();
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1321
        CompletableFuture<V> d = newIncompleteFuture();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1322
        if ((r = result) == null || (s = b.result) == null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1323
            bipush(b, new BiApply<T,U,V>(e, d, this, b, f));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1324
        else if (e == null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1325
            d.biApply(r, s, f, null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1326
        else
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1327
            try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1328
                e.execute(new BiApply<T,U,V>(null, d, this, b, f));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1329
            } catch (Throwable ex) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1330
                d.result = encodeThrowable(ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1331
            }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1332
        return d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1333
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1334
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1335
    @SuppressWarnings("serial")
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1336
    static final class BiAccept<T,U> extends BiCompletion<T,U,Void> {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1337
        BiConsumer<? super T,? super U> fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1338
        BiAccept(Executor executor, CompletableFuture<Void> dep,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1339
                 CompletableFuture<T> src, CompletableFuture<U> snd,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1340
                 BiConsumer<? super T,? super U> fn) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1341
            super(executor, dep, src, snd); this.fn = fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1342
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1343
        final CompletableFuture<Void> tryFire(int mode) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1344
            CompletableFuture<Void> d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1345
            CompletableFuture<T> a;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1346
            CompletableFuture<U> b;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1347
            Object r, s; BiConsumer<? super T,? super U> f;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1348
            if ((d = dep) == null || (f = fn) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1349
                || (a = src) == null || (r = a.result) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1350
                || (b = snd) == null || (s = b.result) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1351
                || !d.biAccept(r, s, f, mode > 0 ? null : this))
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1352
                return null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1353
            dep = null; src = null; snd = null; fn = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1354
            return d.postFire(a, b, mode);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1355
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1356
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1357
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1358
    final <R,S> boolean biAccept(Object r, Object s,
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1359
                                 BiConsumer<? super R,? super S> f,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1360
                                 BiAccept<R,S> c) {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1361
        Throwable x;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1362
        tryComplete: if (result == null) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1363
            if (r instanceof AltResult) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1364
                if ((x = ((AltResult)r).ex) != null) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1365
                    completeThrowable(x, r);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1366
                    break tryComplete;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1367
                }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1368
                r = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1369
            }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1370
            if (s instanceof AltResult) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1371
                if ((x = ((AltResult)s).ex) != null) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1372
                    completeThrowable(x, s);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1373
                    break tryComplete;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1374
                }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1375
                s = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1376
            }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1377
            try {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1378
                if (c != null && !c.claim())
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1379
                    return false;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1380
                @SuppressWarnings("unchecked") R rr = (R) r;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1381
                @SuppressWarnings("unchecked") S ss = (S) s;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1382
                f.accept(rr, ss);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1383
                completeNull();
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1384
            } catch (Throwable ex) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1385
                completeThrowable(ex);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1386
            }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1387
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1388
        return true;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1389
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1390
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1391
    private <U> CompletableFuture<Void> biAcceptStage(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1392
        Executor e, CompletionStage<U> o,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1393
        BiConsumer<? super T,? super U> f) {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1394
        CompletableFuture<U> b; Object r, s;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1395
        if (f == null || (b = o.toCompletableFuture()) == null)
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1396
            throw new NullPointerException();
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1397
        CompletableFuture<Void> d = newIncompleteFuture();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1398
        if ((r = result) == null || (s = b.result) == null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1399
            bipush(b, new BiAccept<T,U>(e, d, this, b, f));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1400
        else if (e == null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1401
            d.biAccept(r, s, f, null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1402
        else
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1403
            try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1404
                e.execute(new BiAccept<T,U>(null, d, this, b, f));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1405
            } catch (Throwable ex) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1406
                d.result = encodeThrowable(ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1407
            }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1408
        return d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1409
    }
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1410
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1411
    @SuppressWarnings("serial")
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1412
    static final class BiRun<T,U> extends BiCompletion<T,U,Void> {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1413
        Runnable fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1414
        BiRun(Executor executor, CompletableFuture<Void> dep,
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1415
              CompletableFuture<T> src, CompletableFuture<U> snd,
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1416
              Runnable fn) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1417
            super(executor, dep, src, snd); this.fn = fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1418
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1419
        final CompletableFuture<Void> tryFire(int mode) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1420
            CompletableFuture<Void> d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1421
            CompletableFuture<T> a;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1422
            CompletableFuture<U> b;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1423
            Object r, s; Runnable f;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1424
            if ((d = dep) == null || (f = fn) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1425
                || (a = src) == null || (r = a.result) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1426
                || (b = snd) == null || (s = b.result) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1427
                || !d.biRun(r, s, f, mode > 0 ? null : this))
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1428
                return null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1429
            dep = null; src = null; snd = null; fn = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1430
            return d.postFire(a, b, mode);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1431
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1432
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1433
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1434
    final boolean biRun(Object r, Object s, Runnable f, BiRun<?,?> c) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1435
        Throwable x; Object z;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1436
        if (result == null) {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1437
            if ((r instanceof AltResult
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1438
                 && (x = ((AltResult)(z = r)).ex) != null) ||
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1439
                (s instanceof AltResult
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1440
                 && (x = ((AltResult)(z = s)).ex) != null))
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1441
                completeThrowable(x, z);
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1442
            else
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1443
                try {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1444
                    if (c != null && !c.claim())
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1445
                        return false;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1446
                    f.run();
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1447
                    completeNull();
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1448
                } catch (Throwable ex) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1449
                    completeThrowable(ex);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1450
                }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1451
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1452
        return true;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1453
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1454
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1455
    private CompletableFuture<Void> biRunStage(Executor e, CompletionStage<?> o,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1456
                                               Runnable f) {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1457
        CompletableFuture<?> b; Object r, s;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1458
        if (f == null || (b = o.toCompletableFuture()) == null)
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1459
            throw new NullPointerException();
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1460
        CompletableFuture<Void> d = newIncompleteFuture();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1461
        if ((r = result) == null || (s = b.result) == null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1462
            bipush(b, new BiRun<>(e, d, this, b, f));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1463
        else if (e == null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1464
            d.biRun(r, s, f, null);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1465
        else
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1466
            try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1467
                e.execute(new BiRun<>(null, d, this, b, f));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1468
            } catch (Throwable ex) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1469
                d.result = encodeThrowable(ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1470
            }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1471
        return d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1472
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1473
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1474
    @SuppressWarnings("serial")
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1475
    static final class BiRelay<T,U> extends BiCompletion<T,U,Void> { // for And
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1476
        BiRelay(CompletableFuture<Void> dep,
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1477
                CompletableFuture<T> src, CompletableFuture<U> snd) {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1478
            super(null, dep, src, snd);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1479
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1480
        final CompletableFuture<Void> tryFire(int mode) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1481
            CompletableFuture<Void> d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1482
            CompletableFuture<T> a;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1483
            CompletableFuture<U> b;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1484
            Object r, s, z; Throwable x;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1485
            if ((d = dep) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1486
                || (a = src) == null || (r = a.result) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1487
                || (b = snd) == null || (s = b.result) == null)
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1488
                return null;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1489
            if (d.result == null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1490
                if ((r instanceof AltResult
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1491
                     && (x = ((AltResult)(z = r)).ex) != null) ||
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1492
                    (s instanceof AltResult
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1493
                     && (x = ((AltResult)(z = s)).ex) != null))
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1494
                    d.completeThrowable(x, z);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1495
                else
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1496
                    d.completeNull();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1497
            }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1498
            src = null; snd = null; dep = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1499
            return d.postFire(a, b, mode);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1500
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1501
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1502
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1503
    /** Recursively constructs a tree of completions. */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1504
    static CompletableFuture<Void> andTree(CompletableFuture<?>[] cfs,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1505
                                           int lo, int hi) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1506
        CompletableFuture<Void> d = new CompletableFuture<Void>();
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1507
        if (lo > hi) // empty
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1508
            d.result = NIL;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1509
        else {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1510
            CompletableFuture<?> a, b; Object r, s, z; Throwable x;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1511
            int mid = (lo + hi) >>> 1;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1512
            if ((a = (lo == mid ? cfs[lo] :
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1513
                      andTree(cfs, lo, mid))) == null ||
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1514
                (b = (lo == hi ? a : (hi == mid+1) ? cfs[hi] :
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1515
                      andTree(cfs, mid+1, hi))) == null)
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1516
                throw new NullPointerException();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1517
            if ((r = a.result) == null || (s = b.result) == null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1518
                a.bipush(b, new BiRelay<>(d, a, b));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1519
            else if ((r instanceof AltResult
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1520
                      && (x = ((AltResult)(z = r)).ex) != null) ||
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1521
                     (s instanceof AltResult
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1522
                      && (x = ((AltResult)(z = s)).ex) != null))
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1523
                d.result = encodeThrowable(x, z);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1524
            else
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1525
                d.result = NIL;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1526
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1527
        return d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1528
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1529
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1530
    /* ------------- Projected (Ored) BiCompletions -------------- */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1531
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1532
    /**
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1533
     * Pushes completion to this and b unless either done.
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1534
     * Caller should first check that result and b.result are both null.
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1535
     */
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1536
    final void orpush(CompletableFuture<?> b, BiCompletion<?,?,?> c) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1537
        if (c != null) {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1538
            while (!tryPushStack(c)) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1539
                if (result != null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1540
                    NEXT.set(c, null);
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1541
                    break;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1542
                }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1543
            }
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1544
            if (result != null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1545
                c.tryFire(SYNC);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1546
            else
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1547
                b.unipush(new CoCompletion(c));
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1548
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1549
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1550
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1551
    @SuppressWarnings("serial")
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1552
    static final class OrApply<T,U extends T,V> extends BiCompletion<T,U,V> {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1553
        Function<? super T,? extends V> fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1554
        OrApply(Executor executor, CompletableFuture<V> dep,
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1555
                CompletableFuture<T> src, CompletableFuture<U> snd,
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1556
                Function<? super T,? extends V> fn) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1557
            super(executor, dep, src, snd); this.fn = fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1558
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1559
        final CompletableFuture<V> tryFire(int mode) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1560
            CompletableFuture<V> d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1561
            CompletableFuture<T> a;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1562
            CompletableFuture<U> b;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1563
            Object r; Throwable x; Function<? super T,? extends V> f;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1564
            if ((d = dep) == null || (f = fn) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1565
                || (a = src) == null || (b = snd) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1566
                || ((r = a.result) == null && (r = b.result) == null))
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1567
                return null;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1568
            tryComplete: if (d.result == null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1569
                try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1570
                    if (mode <= 0 && !claim())
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1571
                        return null;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1572
                    if (r instanceof AltResult) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1573
                        if ((x = ((AltResult)r).ex) != null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1574
                            d.completeThrowable(x, r);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1575
                            break tryComplete;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1576
                        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1577
                        r = null;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1578
                    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1579
                    @SuppressWarnings("unchecked") T t = (T) r;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1580
                    d.completeValue(f.apply(t));
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1581
                } catch (Throwable ex) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1582
                    d.completeThrowable(ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1583
                }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1584
            }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1585
            dep = null; src = null; snd = null; fn = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1586
            return d.postFire(a, b, mode);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1587
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1588
    }
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1589
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1590
    private <U extends T,V> CompletableFuture<V> orApplyStage(
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1591
        Executor e, CompletionStage<U> o, Function<? super T, ? extends V> f) {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1592
        CompletableFuture<U> b;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1593
        if (f == null || (b = o.toCompletableFuture()) == null)
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1594
            throw new NullPointerException();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1595
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1596
        Object r; CompletableFuture<? extends T> z;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1597
        if ((r = (z = this).result) != null ||
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1598
            (r = (z = b).result) != null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1599
            return z.uniApplyNow(r, e, f);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1600
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1601
        CompletableFuture<V> d = newIncompleteFuture();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1602
        orpush(b, new OrApply<T,U,V>(e, d, this, b, f));
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1603
        return d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1604
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1605
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1606
    @SuppressWarnings("serial")
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1607
    static final class OrAccept<T,U extends T> extends BiCompletion<T,U,Void> {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1608
        Consumer<? super T> fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1609
        OrAccept(Executor executor, CompletableFuture<Void> dep,
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1610
                 CompletableFuture<T> src, CompletableFuture<U> snd,
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1611
                 Consumer<? super T> fn) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1612
            super(executor, dep, src, snd); this.fn = fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1613
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1614
        final CompletableFuture<Void> tryFire(int mode) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1615
            CompletableFuture<Void> d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1616
            CompletableFuture<T> a;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1617
            CompletableFuture<U> b;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1618
            Object r; Throwable x; Consumer<? super T> f;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1619
            if ((d = dep) == null || (f = fn) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1620
                || (a = src) == null || (b = snd) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1621
                || ((r = a.result) == null && (r = b.result) == null))
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1622
                return null;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1623
            tryComplete: if (d.result == null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1624
                try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1625
                    if (mode <= 0 && !claim())
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1626
                        return null;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1627
                    if (r instanceof AltResult) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1628
                        if ((x = ((AltResult)r).ex) != null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1629
                            d.completeThrowable(x, r);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1630
                            break tryComplete;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1631
                        }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1632
                        r = null;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1633
                    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1634
                    @SuppressWarnings("unchecked") T t = (T) r;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1635
                    f.accept(t);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1636
                    d.completeNull();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1637
                } catch (Throwable ex) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1638
                    d.completeThrowable(ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1639
                }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1640
            }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1641
            dep = null; src = null; snd = null; fn = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1642
            return d.postFire(a, b, mode);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1643
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1644
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1645
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1646
    private <U extends T> CompletableFuture<Void> orAcceptStage(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1647
        Executor e, CompletionStage<U> o, Consumer<? super T> f) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1648
        CompletableFuture<U> b;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1649
        if (f == null || (b = o.toCompletableFuture()) == null)
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1650
            throw new NullPointerException();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1651
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1652
        Object r; CompletableFuture<? extends T> z;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1653
        if ((r = (z = this).result) != null ||
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1654
            (r = (z = b).result) != null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1655
            return z.uniAcceptNow(r, e, f);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1656
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1657
        CompletableFuture<Void> d = newIncompleteFuture();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1658
        orpush(b, new OrAccept<T,U>(e, d, this, b, f));
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1659
        return d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1660
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1661
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1662
    @SuppressWarnings("serial")
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1663
    static final class OrRun<T,U> extends BiCompletion<T,U,Void> {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1664
        Runnable fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1665
        OrRun(Executor executor, CompletableFuture<Void> dep,
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1666
              CompletableFuture<T> src, CompletableFuture<U> snd,
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1667
              Runnable fn) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1668
            super(executor, dep, src, snd); this.fn = fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1669
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1670
        final CompletableFuture<Void> tryFire(int mode) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1671
            CompletableFuture<Void> d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1672
            CompletableFuture<T> a;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1673
            CompletableFuture<U> b;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1674
            Object r; Throwable x; Runnable f;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1675
            if ((d = dep) == null || (f = fn) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1676
                || (a = src) == null || (b = snd) == null
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1677
                || ((r = a.result) == null && (r = b.result) == null))
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1678
                return null;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1679
            if (d.result == null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1680
                try {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1681
                    if (mode <= 0 && !claim())
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1682
                        return null;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1683
                    else if (r instanceof AltResult
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1684
                        && (x = ((AltResult)r).ex) != null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1685
                        d.completeThrowable(x, r);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1686
                    else {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1687
                        f.run();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1688
                        d.completeNull();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1689
                    }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1690
                } catch (Throwable ex) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1691
                    d.completeThrowable(ex);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1692
                }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1693
            }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1694
            dep = null; src = null; snd = null; fn = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1695
            return d.postFire(a, b, mode);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1696
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1697
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1698
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1699
    private CompletableFuture<Void> orRunStage(Executor e, CompletionStage<?> o,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1700
                                               Runnable f) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1701
        CompletableFuture<?> b;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1702
        if (f == null || (b = o.toCompletableFuture()) == null)
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1703
            throw new NullPointerException();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1704
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1705
        Object r; CompletableFuture<?> z;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1706
        if ((r = (z = this).result) != null ||
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1707
            (r = (z = b).result) != null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1708
            return z.uniRunNow(r, e, f);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1709
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1710
        CompletableFuture<Void> d = newIncompleteFuture();
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1711
        orpush(b, new OrRun<>(e, d, this, b, f));
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1712
        return d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1713
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1714
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1715
    /** Completion for an anyOf input future. */
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1716
    @SuppressWarnings("serial")
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1717
    static class AnyOf extends Completion {
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1718
        CompletableFuture<Object> dep; CompletableFuture<?> src;
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1719
        CompletableFuture<?>[] srcs;
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1720
        AnyOf(CompletableFuture<Object> dep, CompletableFuture<?> src,
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1721
              CompletableFuture<?>[] srcs) {
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1722
            this.dep = dep; this.src = src; this.srcs = srcs;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1723
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1724
        final CompletableFuture<Object> tryFire(int mode) {
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1725
            // assert mode != ASYNC;
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1726
            CompletableFuture<Object> d; CompletableFuture<?> a;
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1727
            CompletableFuture<?>[] as;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1728
            Object r;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1729
            if ((d = dep) == null
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1730
                || (a = src) == null || (r = a.result) == null
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1731
                || (as = srcs) == null)
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1732
                return null;
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1733
            dep = null; src = null; srcs = null;
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1734
            if (d.completeRelay(r)) {
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1735
                for (CompletableFuture<?> b : as)
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1736
                    if (b != a)
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1737
                        b.cleanStack();
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1738
                if (mode < 0)
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1739
                    return d;
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1740
                else
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1741
                    d.postComplete();
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1742
            }
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1743
            return null;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1744
        }
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1745
        final boolean isLive() {
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1746
            CompletableFuture<Object> d;
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  1747
            return (d = dep) != null && d.result == null;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1748
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1749
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1750
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1751
    /* ------------- Zero-input Async forms -------------- */
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1752
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1753
    @SuppressWarnings("serial")
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1754
    static final class AsyncSupply<T> extends ForkJoinTask<Void>
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1755
        implements Runnable, AsynchronousCompletionTask {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1756
        CompletableFuture<T> dep; Supplier<? extends T> fn;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1757
        AsyncSupply(CompletableFuture<T> dep, Supplier<? extends T> fn) {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1758
            this.dep = dep; this.fn = fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1759
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1760
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1761
        public final Void getRawResult() { return null; }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1762
        public final void setRawResult(Void v) {}
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1763
        public final boolean exec() { run(); return false; }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1764
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1765
        public void run() {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1766
            CompletableFuture<T> d; Supplier<? extends T> f;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1767
            if ((d = dep) != null && (f = fn) != null) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1768
                dep = null; fn = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1769
                if (d.result == null) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1770
                    try {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1771
                        d.completeValue(f.get());
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1772
                    } catch (Throwable ex) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1773
                        d.completeThrowable(ex);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1774
                    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1775
                }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1776
                d.postComplete();
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1777
            }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1778
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1779
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1780
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1781
    static <U> CompletableFuture<U> asyncSupplyStage(Executor e,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1782
                                                     Supplier<U> f) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1783
        if (f == null) throw new NullPointerException();
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1784
        CompletableFuture<U> d = new CompletableFuture<U>();
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1785
        e.execute(new AsyncSupply<U>(d, f));
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1786
        return d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1787
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1788
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1789
    @SuppressWarnings("serial")
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1790
    static final class AsyncRun extends ForkJoinTask<Void>
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1791
        implements Runnable, AsynchronousCompletionTask {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1792
        CompletableFuture<Void> dep; Runnable fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1793
        AsyncRun(CompletableFuture<Void> dep, Runnable fn) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1794
            this.dep = dep; this.fn = fn;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1795
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1796
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1797
        public final Void getRawResult() { return null; }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1798
        public final void setRawResult(Void v) {}
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1799
        public final boolean exec() { run(); return false; }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1800
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1801
        public void run() {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1802
            CompletableFuture<Void> d; Runnable f;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1803
            if ((d = dep) != null && (f = fn) != null) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1804
                dep = null; fn = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1805
                if (d.result == null) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1806
                    try {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1807
                        f.run();
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1808
                        d.completeNull();
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1809
                    } catch (Throwable ex) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1810
                        d.completeThrowable(ex);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1811
                    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1812
                }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1813
                d.postComplete();
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1814
            }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1815
        }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1816
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1817
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1818
    static CompletableFuture<Void> asyncRunStage(Executor e, Runnable f) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1819
        if (f == null) throw new NullPointerException();
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1820
        CompletableFuture<Void> d = new CompletableFuture<Void>();
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1821
        e.execute(new AsyncRun(d, f));
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1822
        return d;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1823
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1824
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1825
    /* ------------- Signallers -------------- */
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1826
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1827
    /**
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1828
     * Completion for recording and releasing a waiting thread.  This
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1829
     * class implements ManagedBlocker to avoid starvation when
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1830
     * blocking actions pile up in ForkJoinPools.
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1831
     */
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1832
    @SuppressWarnings("serial")
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1833
    static final class Signaller extends Completion
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1834
        implements ForkJoinPool.ManagedBlocker {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1835
        long nanos;                    // remaining wait time if timed
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1836
        final long deadline;           // non-zero if timed
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1837
        final boolean interruptible;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1838
        boolean interrupted;
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1839
        volatile Thread thread;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1840
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1841
        Signaller(boolean interruptible, long nanos, long deadline) {
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1842
            this.thread = Thread.currentThread();
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1843
            this.interruptible = interruptible;
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1844
            this.nanos = nanos;
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1845
            this.deadline = deadline;
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1846
        }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1847
        final CompletableFuture<?> tryFire(int ignore) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1848
            Thread w; // no need to atomically claim
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1849
            if ((w = thread) != null) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1850
                thread = null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1851
                LockSupport.unpark(w);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1852
            }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1853
            return null;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1854
        }
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1855
        public boolean isReleasable() {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1856
            if (Thread.interrupted())
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1857
                interrupted = true;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1858
            return ((interrupted && interruptible) ||
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1859
                    (deadline != 0L &&
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1860
                     (nanos <= 0L ||
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1861
                      (nanos = deadline - System.nanoTime()) <= 0L)) ||
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1862
                    thread == null);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1863
        }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1864
        public boolean block() {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1865
            while (!isReleasable()) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1866
                if (deadline == 0L)
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1867
                    LockSupport.park(this);
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1868
                else
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1869
                    LockSupport.parkNanos(this, nanos);
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1870
            }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1871
            return true;
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1872
        }
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1873
        final boolean isLive() { return thread != null; }
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1874
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1875
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1876
    /**
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1877
     * Returns raw result after waiting, or null if interruptible and
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1878
     * interrupted.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1879
     */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1880
    private Object waitingGet(boolean interruptible) {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1881
        Signaller q = null;
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1882
        boolean queued = false;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1883
        Object r;
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1884
        while ((r = result) == null) {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1885
            if (q == null) {
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1886
                q = new Signaller(interruptible, 0L, 0L);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1887
                if (Thread.currentThread() instanceof ForkJoinWorkerThread)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1888
                    ForkJoinPool.helpAsyncBlocker(defaultExecutor(), q);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1889
            }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1890
            else if (!queued)
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1891
                queued = tryPushStack(q);
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1892
            else {
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1893
                try {
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1894
                    ForkJoinPool.managedBlock(q);
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1895
                } catch (InterruptedException ie) { // currently cannot happen
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1896
                    q.interrupted = true;
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1897
                }
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1898
                if (q.interrupted && interruptible)
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1899
                    break;
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1900
            }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1901
        }
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1902
        if (q != null && queued) {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1903
            q.thread = null;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1904
            if (!interruptible && q.interrupted)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1905
                Thread.currentThread().interrupt();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1906
            if (r == null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1907
                cleanStack();
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1908
        }
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1909
        if (r != null || (r = result) != null)
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1910
            postComplete();
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1911
        return r;
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1912
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1913
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  1914
    /**
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1915
     * Returns raw result after waiting, or null if interrupted, or
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1916
     * throws TimeoutException on timeout.
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1917
     */
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1918
    private Object timedGet(long nanos) throws TimeoutException {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1919
        if (Thread.interrupted())
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1920
            return null;
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1921
        if (nanos > 0L) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1922
            long d = System.nanoTime() + nanos;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1923
            long deadline = (d == 0L) ? 1L : d; // avoid 0
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1924
            Signaller q = null;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1925
            boolean queued = false;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1926
            Object r;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1927
            while ((r = result) == null) { // similar to untimed
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1928
                if (q == null) {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1929
                    q = new Signaller(true, nanos, deadline);
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1930
                    if (Thread.currentThread() instanceof ForkJoinWorkerThread)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1931
                        ForkJoinPool.helpAsyncBlocker(defaultExecutor(), q);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1932
                }
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1933
                else if (!queued)
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1934
                    queued = tryPushStack(q);
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1935
                else if (q.nanos <= 0L)
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1936
                    break;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1937
                else {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1938
                    try {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1939
                        ForkJoinPool.managedBlock(q);
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1940
                    } catch (InterruptedException ie) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1941
                        q.interrupted = true;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1942
                    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1943
                    if (q.interrupted)
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1944
                        break;
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  1945
                }
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  1946
            }
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1947
            if (q != null && queued) {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1948
                q.thread = null;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1949
                if (r == null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1950
                    cleanStack();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1951
            }
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  1952
            if (r != null || (r = result) != null)
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1953
                postComplete();
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1954
            if (r != null || (q != null && q.interrupted))
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1955
                return r;
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  1956
        }
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1957
        throw new TimeoutException();
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  1958
    }
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  1959
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1960
    /* ------------- public methods -------------- */
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1961
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1962
    /**
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1963
     * Creates a new incomplete CompletableFuture.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1964
     */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1965
    public CompletableFuture() {
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1966
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1967
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1968
    /**
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1969
     * Creates a new complete CompletableFuture with given encoded result.
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1970
     */
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1971
    CompletableFuture(Object r) {
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  1972
        RESULT.setRelease(this, r);
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1973
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1974
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  1975
    /**
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1976
     * Returns a new CompletableFuture that is asynchronously completed
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1977
     * by a task running in the {@link ForkJoinPool#commonPool()} with
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1978
     * the value obtained by calling the given Supplier.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1979
     *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1980
     * @param supplier a function returning the value to be used
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1981
     * to complete the returned CompletableFuture
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  1982
     * @param <U> the function's return type
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1983
     * @return the new CompletableFuture
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1984
     */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1985
    public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  1986
        return asyncSupplyStage(ASYNC_POOL, supplier);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1987
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1988
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1989
    /**
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1990
     * Returns a new CompletableFuture that is asynchronously completed
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1991
     * by a task running in the given executor with the value obtained
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1992
     * by calling the given Supplier.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1993
     *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1994
     * @param supplier a function returning the value to be used
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1995
     * to complete the returned CompletableFuture
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1996
     * @param executor the executor to use for asynchronous execution
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  1997
     * @param <U> the function's return type
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1998
     * @return the new CompletableFuture
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  1999
     */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2000
    public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier,
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2001
                                                       Executor executor) {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2002
        return asyncSupplyStage(screenExecutor(executor), supplier);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2003
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2004
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2005
    /**
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2006
     * Returns a new CompletableFuture that is asynchronously completed
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2007
     * by a task running in the {@link ForkJoinPool#commonPool()} after
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2008
     * it runs the given action.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2009
     *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2010
     * @param runnable the action to run before completing the
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2011
     * returned CompletableFuture
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2012
     * @return the new CompletableFuture
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2013
     */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2014
    public static CompletableFuture<Void> runAsync(Runnable runnable) {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2015
        return asyncRunStage(ASYNC_POOL, runnable);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2016
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2017
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2018
    /**
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2019
     * Returns a new CompletableFuture that is asynchronously completed
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2020
     * by a task running in the given executor after it runs the given
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2021
     * action.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2022
     *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2023
     * @param runnable the action to run before completing the
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2024
     * returned CompletableFuture
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2025
     * @param executor the executor to use for asynchronous execution
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2026
     * @return the new CompletableFuture
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2027
     */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2028
    public static CompletableFuture<Void> runAsync(Runnable runnable,
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2029
                                                   Executor executor) {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2030
        return asyncRunStage(screenExecutor(executor), runnable);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2031
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2032
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2033
    /**
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2034
     * Returns a new CompletableFuture that is already completed with
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2035
     * the given value.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2036
     *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2037
     * @param value the value
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2038
     * @param <U> the type of the value
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2039
     * @return the completed CompletableFuture
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2040
     */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2041
    public static <U> CompletableFuture<U> completedFuture(U value) {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2042
        return new CompletableFuture<U>((value == null) ? NIL : value);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2043
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2044
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2045
    /**
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2046
     * Returns {@code true} if completed in any fashion: normally,
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2047
     * exceptionally, or via cancellation.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2048
     *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2049
     * @return {@code true} if completed
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2050
     */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2051
    public boolean isDone() {
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2052
        return result != null;
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2053
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2054
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2055
    /**
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2056
     * Waits if necessary for this future to complete, and then
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2057
     * returns its result.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2058
     *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2059
     * @return the result value
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2060
     * @throws CancellationException if this future was cancelled
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2061
     * @throws ExecutionException if this future completed exceptionally
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2062
     * @throws InterruptedException if the current thread was interrupted
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2063
     * while waiting
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2064
     */
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2065
    @SuppressWarnings("unchecked")
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2066
    public T get() throws InterruptedException, ExecutionException {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2067
        Object r;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2068
        if ((r = result) == null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2069
            r = waitingGet(true);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2070
        return (T) reportGet(r);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2071
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2072
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2073
    /**
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2074
     * Waits if necessary for at most the given time for this future
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2075
     * to complete, and then returns its result, if available.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2076
     *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2077
     * @param timeout the maximum time to wait
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2078
     * @param unit the time unit of the timeout argument
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2079
     * @return the result value
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2080
     * @throws CancellationException if this future was cancelled
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2081
     * @throws ExecutionException if this future completed exceptionally
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2082
     * @throws InterruptedException if the current thread was interrupted
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2083
     * while waiting
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2084
     * @throws TimeoutException if the wait timed out
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2085
     */
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2086
    @SuppressWarnings("unchecked")
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2087
    public T get(long timeout, TimeUnit unit)
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2088
        throws InterruptedException, ExecutionException, TimeoutException {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2089
        long nanos = unit.toNanos(timeout);
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2090
        Object r;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2091
        if ((r = result) == null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2092
            r = timedGet(nanos);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2093
        return (T) reportGet(r);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2094
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2095
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2096
    /**
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2097
     * Returns the result value when complete, or throws an
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2098
     * (unchecked) exception if completed exceptionally. To better
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2099
     * conform with the use of common functional forms, if a
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2100
     * computation involved in the completion of this
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2101
     * CompletableFuture threw an exception, this method throws an
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2102
     * (unchecked) {@link CompletionException} with the underlying
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2103
     * exception as its cause.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2104
     *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2105
     * @return the result value
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2106
     * @throws CancellationException if the computation was cancelled
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2107
     * @throws CompletionException if this future completed
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2108
     * exceptionally or a completion computation threw an exception
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2109
     */
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2110
    @SuppressWarnings("unchecked")
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2111
    public T join() {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2112
        Object r;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2113
        if ((r = result) == null)
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2114
            r = waitingGet(false);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2115
        return (T) reportJoin(r);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2116
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2117
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2118
    /**
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2119
     * Returns the result value (or throws any encountered exception)
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2120
     * if completed, else returns the given valueIfAbsent.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2121
     *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2122
     * @param valueIfAbsent the value to return if not completed
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2123
     * @return the result value, if completed, else the given valueIfAbsent
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2124
     * @throws CancellationException if the computation was cancelled
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2125
     * @throws CompletionException if this future completed
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2126
     * exceptionally or a completion computation threw an exception
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2127
     */
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2128
    @SuppressWarnings("unchecked")
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2129
    public T getNow(T valueIfAbsent) {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2130
        Object r;
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2131
        return ((r = result) == null) ? valueIfAbsent : (T) reportJoin(r);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2132
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2133
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2134
    /**
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2135
     * If not already completed, sets the value returned by {@link
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2136
     * #get()} and related methods to the given value.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2137
     *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2138
     * @param value the result value
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2139
     * @return {@code true} if this invocation caused this CompletableFuture
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2140
     * to transition to a completed state, else {@code false}
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2141
     */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2142
    public boolean complete(T value) {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2143
        boolean triggered = completeValue(value);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2144
        postComplete();
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2145
        return triggered;
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2146
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2147
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2148
    /**
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2149
     * If not already completed, causes invocations of {@link #get()}
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2150
     * and related methods to throw the given exception.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2151
     *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2152
     * @param ex the exception
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2153
     * @return {@code true} if this invocation caused this CompletableFuture
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2154
     * to transition to a completed state, else {@code false}
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2155
     */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2156
    public boolean completeExceptionally(Throwable ex) {
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2157
        if (ex == null) throw new NullPointerException();
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2158
        boolean triggered = internalComplete(new AltResult(ex));
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2159
        postComplete();
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2160
        return triggered;
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2161
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2162
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2163
    public <U> CompletableFuture<U> thenApply(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2164
        Function<? super T,? extends U> fn) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2165
        return uniApplyStage(null, fn);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2166
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2167
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2168
    public <U> CompletableFuture<U> thenApplyAsync(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2169
        Function<? super T,? extends U> fn) {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2170
        return uniApplyStage(defaultExecutor(), fn);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2171
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2172
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2173
    public <U> CompletableFuture<U> thenApplyAsync(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2174
        Function<? super T,? extends U> fn, Executor executor) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2175
        return uniApplyStage(screenExecutor(executor), fn);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2176
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2177
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2178
    public CompletableFuture<Void> thenAccept(Consumer<? super T> action) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2179
        return uniAcceptStage(null, action);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2180
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2181
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2182
    public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action) {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2183
        return uniAcceptStage(defaultExecutor(), action);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2184
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2185
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2186
    public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2187
                                                   Executor executor) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2188
        return uniAcceptStage(screenExecutor(executor), action);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2189
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2190
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2191
    public CompletableFuture<Void> thenRun(Runnable action) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2192
        return uniRunStage(null, action);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2193
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2194
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2195
    public CompletableFuture<Void> thenRunAsync(Runnable action) {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2196
        return uniRunStage(defaultExecutor(), action);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2197
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2198
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2199
    public CompletableFuture<Void> thenRunAsync(Runnable action,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2200
                                                Executor executor) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2201
        return uniRunStage(screenExecutor(executor), action);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2202
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2203
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2204
    public <U,V> CompletableFuture<V> thenCombine(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2205
        CompletionStage<? extends U> other,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2206
        BiFunction<? super T,? super U,? extends V> fn) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2207
        return biApplyStage(null, other, fn);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2208
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2209
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2210
    public <U,V> CompletableFuture<V> thenCombineAsync(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2211
        CompletionStage<? extends U> other,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2212
        BiFunction<? super T,? super U,? extends V> fn) {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2213
        return biApplyStage(defaultExecutor(), other, fn);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2214
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2215
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2216
    public <U,V> CompletableFuture<V> thenCombineAsync(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2217
        CompletionStage<? extends U> other,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2218
        BiFunction<? super T,? super U,? extends V> fn, Executor executor) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2219
        return biApplyStage(screenExecutor(executor), other, fn);
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2220
    }
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2221
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2222
    public <U> CompletableFuture<Void> thenAcceptBoth(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2223
        CompletionStage<? extends U> other,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2224
        BiConsumer<? super T, ? super U> action) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2225
        return biAcceptStage(null, other, action);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2226
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2227
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2228
    public <U> CompletableFuture<Void> thenAcceptBothAsync(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2229
        CompletionStage<? extends U> other,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2230
        BiConsumer<? super T, ? super U> action) {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2231
        return biAcceptStage(defaultExecutor(), other, action);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2232
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2233
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2234
    public <U> CompletableFuture<Void> thenAcceptBothAsync(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2235
        CompletionStage<? extends U> other,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2236
        BiConsumer<? super T, ? super U> action, Executor executor) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2237
        return biAcceptStage(screenExecutor(executor), other, action);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2238
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2239
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2240
    public CompletableFuture<Void> runAfterBoth(CompletionStage<?> other,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2241
                                                Runnable action) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2242
        return biRunStage(null, other, action);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2243
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2244
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2245
    public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2246
                                                     Runnable action) {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2247
        return biRunStage(defaultExecutor(), other, action);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2248
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2249
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2250
    public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2251
                                                     Runnable action,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2252
                                                     Executor executor) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2253
        return biRunStage(screenExecutor(executor), other, action);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2254
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2255
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2256
    public <U> CompletableFuture<U> applyToEither(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2257
        CompletionStage<? extends T> other, Function<? super T, U> fn) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2258
        return orApplyStage(null, other, fn);
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2259
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2260
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2261
    public <U> CompletableFuture<U> applyToEitherAsync(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2262
        CompletionStage<? extends T> other, Function<? super T, U> fn) {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2263
        return orApplyStage(defaultExecutor(), other, fn);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2264
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2265
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2266
    public <U> CompletableFuture<U> applyToEitherAsync(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2267
        CompletionStage<? extends T> other, Function<? super T, U> fn,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2268
        Executor executor) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2269
        return orApplyStage(screenExecutor(executor), other, fn);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2270
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2271
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2272
    public CompletableFuture<Void> acceptEither(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2273
        CompletionStage<? extends T> other, Consumer<? super T> action) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2274
        return orAcceptStage(null, other, action);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2275
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2276
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2277
    public CompletableFuture<Void> acceptEitherAsync(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2278
        CompletionStage<? extends T> other, Consumer<? super T> action) {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2279
        return orAcceptStage(defaultExecutor(), other, action);
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2280
    }
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2281
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2282
    public CompletableFuture<Void> acceptEitherAsync(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2283
        CompletionStage<? extends T> other, Consumer<? super T> action,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2284
        Executor executor) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2285
        return orAcceptStage(screenExecutor(executor), other, action);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2286
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2287
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2288
    public CompletableFuture<Void> runAfterEither(CompletionStage<?> other,
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2289
                                                  Runnable action) {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2290
        return orRunStage(null, other, action);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2291
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2292
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2293
    public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2294
                                                       Runnable action) {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2295
        return orRunStage(defaultExecutor(), other, action);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2296
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2297
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2298
    public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2299
                                                       Runnable action,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2300
                                                       Executor executor) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2301
        return orRunStage(screenExecutor(executor), other, action);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2302
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2303
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2304
    public <U> CompletableFuture<U> thenCompose(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2305
        Function<? super T, ? extends CompletionStage<U>> fn) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2306
        return uniComposeStage(null, fn);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2307
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2308
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2309
    public <U> CompletableFuture<U> thenComposeAsync(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2310
        Function<? super T, ? extends CompletionStage<U>> fn) {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2311
        return uniComposeStage(defaultExecutor(), fn);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2312
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2313
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2314
    public <U> CompletableFuture<U> thenComposeAsync(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2315
        Function<? super T, ? extends CompletionStage<U>> fn,
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2316
        Executor executor) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2317
        return uniComposeStage(screenExecutor(executor), fn);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2318
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2319
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2320
    public CompletableFuture<T> whenComplete(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2321
        BiConsumer<? super T, ? super Throwable> action) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2322
        return uniWhenCompleteStage(null, action);
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2323
    }
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2324
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2325
    public CompletableFuture<T> whenCompleteAsync(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2326
        BiConsumer<? super T, ? super Throwable> action) {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2327
        return uniWhenCompleteStage(defaultExecutor(), action);
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2328
    }
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2329
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2330
    public CompletableFuture<T> whenCompleteAsync(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2331
        BiConsumer<? super T, ? super Throwable> action, Executor executor) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2332
        return uniWhenCompleteStage(screenExecutor(executor), action);
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2333
    }
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2334
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2335
    public <U> CompletableFuture<U> handle(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2336
        BiFunction<? super T, Throwable, ? extends U> fn) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2337
        return uniHandleStage(null, fn);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2338
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2339
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2340
    public <U> CompletableFuture<U> handleAsync(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2341
        BiFunction<? super T, Throwable, ? extends U> fn) {
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2342
        return uniHandleStage(defaultExecutor(), fn);
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2343
    }
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2344
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2345
    public <U> CompletableFuture<U> handleAsync(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2346
        BiFunction<? super T, Throwable, ? extends U> fn, Executor executor) {
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2347
        return uniHandleStage(screenExecutor(executor), fn);
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2348
    }
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2349
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2350
    /**
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2351
     * Returns this CompletableFuture.
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2352
     *
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2353
     * @return this CompletableFuture
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2354
     */
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2355
    public CompletableFuture<T> toCompletableFuture() {
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2356
        return this;
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2357
    }
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2358
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2359
    public CompletableFuture<T> exceptionally(
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2360
        Function<Throwable, ? extends T> fn) {
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2361
        return uniExceptionallyStage(null, fn);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2362
    }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2363
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2364
    public CompletableFuture<T> exceptionallyAsync(
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2365
        Function<Throwable, ? extends T> fn) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2366
        return uniExceptionallyStage(defaultExecutor(), fn);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2367
    }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2368
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2369
    public CompletableFuture<T> exceptionallyAsync(
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2370
        Function<Throwable, ? extends T> fn, Executor executor) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2371
        return uniExceptionallyStage(screenExecutor(executor), fn);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2372
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2373
51950
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2374
    public CompletableFuture<T> exceptionallyCompose(
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2375
        Function<Throwable, ? extends CompletionStage<T>> fn) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2376
        return uniComposeExceptionallyStage(null, fn);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2377
    }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2378
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2379
    public CompletableFuture<T> exceptionallyComposeAsync(
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2380
        Function<Throwable, ? extends CompletionStage<T>> fn) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2381
        return uniComposeExceptionallyStage(defaultExecutor(), fn);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2382
    }
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2383
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2384
    public CompletableFuture<T> exceptionallyComposeAsync(
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2385
        Function<Throwable, ? extends CompletionStage<T>> fn,
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2386
        Executor executor) {
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2387
        return uniComposeExceptionallyStage(screenExecutor(executor), fn);
a1c24d06e2b5 8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents: 49565
diff changeset
  2388
    }
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2389
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2390
    /* ------------- Arbitrary-arity constructions -------------- */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2391
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2392
    /**
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2393
     * Returns a new CompletableFuture that is completed when all of
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2394
     * the given CompletableFutures complete.  If any of the given
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2395
     * CompletableFutures complete exceptionally, then the returned
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2396
     * CompletableFuture also does so, with a CompletionException
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2397
     * holding this exception as its cause.  Otherwise, the results,
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2398
     * if any, of the given CompletableFutures are not reflected in
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2399
     * the returned CompletableFuture, but may be obtained by
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2400
     * inspecting them individually. If no CompletableFutures are
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2401
     * provided, returns a CompletableFuture completed with the value
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2402
     * {@code null}.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2403
     *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2404
     * <p>Among the applications of this method is to await completion
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2405
     * of a set of independent CompletableFutures before continuing a
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2406
     * program, as in: {@code CompletableFuture.allOf(c1, c2,
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2407
     * c3).join();}.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2408
     *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2409
     * @param cfs the CompletableFutures
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2410
     * @return a new CompletableFuture that is completed when all of the
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2411
     * given CompletableFutures complete
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2412
     * @throws NullPointerException if the array or any of its elements are
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2413
     * {@code null}
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2414
     */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2415
    public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs) {
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2416
        return andTree(cfs, 0, cfs.length - 1);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2417
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2418
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2419
    /**
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2420
     * Returns a new CompletableFuture that is completed when any of
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2421
     * the given CompletableFutures complete, with the same result.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2422
     * Otherwise, if it completed exceptionally, the returned
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2423
     * CompletableFuture also does so, with a CompletionException
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2424
     * holding this exception as its cause.  If no CompletableFutures
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2425
     * are provided, returns an incomplete CompletableFuture.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2426
     *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2427
     * @param cfs the CompletableFutures
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2428
     * @return a new CompletableFuture that is completed with the
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2429
     * result or exception of any of the given CompletableFutures when
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2430
     * one completes
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2431
     * @throws NullPointerException if the array or any of its elements are
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2432
     * {@code null}
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2433
     */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2434
    public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs) {
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2435
        int n; Object r;
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2436
        if ((n = cfs.length) <= 1)
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2437
            return (n == 0)
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2438
                ? new CompletableFuture<Object>()
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2439
                : uniCopyStage(cfs[0]);
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2440
        for (CompletableFuture<?> cf : cfs)
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2441
            if ((r = cf.result) != null)
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2442
                return new CompletableFuture<Object>(encodeRelay(r));
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2443
        cfs = cfs.clone();
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2444
        CompletableFuture<Object> d = new CompletableFuture<>();
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2445
        for (CompletableFuture<?> cf : cfs)
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2446
            cf.unipush(new AnyOf(d, cf, cfs));
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2447
        // If d was completed while we were adding completions, we should
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2448
        // clean the stack of any sources that may have had completions
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2449
        // pushed on their stack after d was completed.
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2450
        if (d.result != null)
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2451
            for (int i = 0, len = cfs.length; i < len; i++)
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2452
                if (cfs[i].result != null)
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2453
                    for (i++; i < len; i++)
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2454
                        if (cfs[i].result == null)
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2455
                            cfs[i].cleanStack();
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2456
        return d;
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2457
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2458
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2459
    /* ------------- Control and status methods -------------- */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2460
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2461
    /**
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2462
     * If not already completed, completes this CompletableFuture with
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2463
     * a {@link CancellationException}. Dependent CompletableFutures
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2464
     * that have not already completed will also complete
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2465
     * exceptionally, with a {@link CompletionException} caused by
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2466
     * this {@code CancellationException}.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2467
     *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2468
     * @param mayInterruptIfRunning this value has no effect in this
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2469
     * implementation because interrupts are not used to control
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2470
     * processing.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2471
     *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2472
     * @return {@code true} if this task is now cancelled
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2473
     */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2474
    public boolean cancel(boolean mayInterruptIfRunning) {
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2475
        boolean cancelled = (result == null) &&
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2476
            internalComplete(new AltResult(new CancellationException()));
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2477
        postComplete();
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2478
        return cancelled || isCancelled();
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2479
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2480
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2481
    /**
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2482
     * Returns {@code true} if this CompletableFuture was cancelled
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2483
     * before it completed normally.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2484
     *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2485
     * @return {@code true} if this CompletableFuture was cancelled
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2486
     * before it completed normally
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2487
     */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2488
    public boolean isCancelled() {
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2489
        Object r;
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2490
        return ((r = result) instanceof AltResult) &&
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2491
            (((AltResult)r).ex instanceof CancellationException);
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2492
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2493
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2494
    /**
19192
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2495
     * Returns {@code true} if this CompletableFuture completed
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2496
     * exceptionally, in any way. Possible causes include
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2497
     * cancellation, explicit invocation of {@code
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2498
     * completeExceptionally}, and abrupt termination of a
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2499
     * CompletionStage action.
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2500
     *
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2501
     * @return {@code true} if this CompletableFuture completed
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2502
     * exceptionally
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2503
     */
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2504
    public boolean isCompletedExceptionally() {
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2505
        Object r;
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2506
        return ((r = result) instanceof AltResult) && r != NIL;
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2507
    }
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2508
4954a5c4da01 8020291: j.u.c.CompletionStage
chegar
parents: 18790
diff changeset
  2509
    /**
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2510
     * Forcibly sets or resets the value subsequently returned by
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2511
     * method {@link #get()} and related methods, whether or not
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2512
     * already completed. This method is designed for use only in
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2513
     * error recovery actions, and even in such situations may result
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2514
     * in ongoing dependent completions using established versus
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2515
     * overwritten outcomes.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2516
     *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2517
     * @param value the completion value
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2518
     */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2519
    public void obtrudeValue(T value) {
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2520
        result = (value == null) ? NIL : value;
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2521
        postComplete();
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2522
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2523
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2524
    /**
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2525
     * Forcibly causes subsequent invocations of method {@link #get()}
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2526
     * and related methods to throw the given exception, whether or
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2527
     * not already completed. This method is designed for use only in
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2528
     * error recovery actions, and even in such situations may result
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2529
     * in ongoing dependent completions using established versus
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2530
     * overwritten outcomes.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2531
     *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2532
     * @param ex the exception
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2533
     * @throws NullPointerException if the exception is null
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2534
     */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2535
    public void obtrudeException(Throwable ex) {
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2536
        if (ex == null) throw new NullPointerException();
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2537
        result = new AltResult(ex);
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2538
        postComplete();
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2539
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2540
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2541
    /**
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2542
     * Returns the estimated number of CompletableFutures whose
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2543
     * completions are awaiting completion of this CompletableFuture.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2544
     * This method is designed for use in monitoring system state, not
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2545
     * for synchronization control.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2546
     *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2547
     * @return the number of dependent CompletableFutures
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2548
     */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2549
    public int getNumberOfDependents() {
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2550
        int count = 0;
26336
1eee453c24c1 8056249: Improve CompletableFuture resource usage
dl
parents: 25859
diff changeset
  2551
        for (Completion p = stack; p != null; p = p.next)
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2552
            ++count;
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2553
        return count;
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2554
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2555
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2556
    /**
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2557
     * Returns a string identifying this CompletableFuture, as well as
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2558
     * its completion state.  The state, in brackets, contains the
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2559
     * String {@code "Completed Normally"} or the String {@code
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2560
     * "Completed Exceptionally"}, or the String {@code "Not
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2561
     * completed"} followed by the number of CompletableFutures
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2562
     * dependent upon its completion, if any.
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2563
     *
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2564
     * @return a string identifying this CompletableFuture, as well as its state
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2565
     */
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2566
    public String toString() {
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2567
        Object r = result;
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2568
        int count = 0; // avoid call to getNumberOfDependents in case disabled
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2569
        for (Completion p = stack; p != null; p = p.next)
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2570
            ++count;
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2571
        return super.toString() +
47306
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  2572
            ((r == null)
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  2573
             ? ((count == 0)
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  2574
                ? "[Not completed]"
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  2575
                : "[Not completed, " + count + " dependents]")
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  2576
             : (((r instanceof AltResult) && ((AltResult)r).ex != null)
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  2577
                ? "[Completed exceptionally: " + ((AltResult)r).ex + "]"
90b7465b9ac7 8186265: Make toString() methods of "task" objects more useful
dl
parents: 47216
diff changeset
  2578
                : "[Completed normally]"));
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2579
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2580
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2581
    // jdk9 additions
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2582
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2583
    /**
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2584
     * Returns a new incomplete CompletableFuture of the type to be
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2585
     * returned by a CompletionStage method. Subclasses should
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2586
     * normally override this method to return an instance of the same
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2587
     * class as this CompletableFuture. The default implementation
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2588
     * returns an instance of class CompletableFuture.
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2589
     *
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2590
     * @param <U> the type of the value
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2591
     * @return a new CompletableFuture
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 33674
diff changeset
  2592
     * @since 9
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2593
     */
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2594
    public <U> CompletableFuture<U> newIncompleteFuture() {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2595
        return new CompletableFuture<U>();
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2596
    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2597
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2598
    /**
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2599
     * Returns the default Executor used for async methods that do not
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2600
     * specify an Executor. This class uses the {@link
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2601
     * ForkJoinPool#commonPool()} if it supports more than one
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2602
     * parallel thread, or else an Executor using one thread per async
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2603
     * task.  This method may be overridden in subclasses to return
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2604
     * an Executor that provides at least one independent thread.
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2605
     *
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2606
     * @return the executor
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 33674
diff changeset
  2607
     * @since 9
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2608
     */
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2609
    public Executor defaultExecutor() {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2610
        return ASYNC_POOL;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2611
    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2612
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2613
    /**
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2614
     * Returns a new CompletableFuture that is completed normally with
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2615
     * the same value as this CompletableFuture when it completes
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2616
     * normally. If this CompletableFuture completes exceptionally,
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2617
     * then the returned CompletableFuture completes exceptionally
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2618
     * with a CompletionException with this exception as cause. The
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2619
     * behavior is equivalent to {@code thenApply(x -> x)}. This
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2620
     * method may be useful as a form of "defensive copying", to
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2621
     * prevent clients from completing, while still being able to
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2622
     * arrange dependent actions.
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2623
     *
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2624
     * @return the new CompletableFuture
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 33674
diff changeset
  2625
     * @since 9
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2626
     */
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2627
    public CompletableFuture<T> copy() {
39778
5cda06c52cdd 8160402: Garbage retention with CompletableFuture.anyOf
dl
parents: 39724
diff changeset
  2628
        return uniCopyStage(this);
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2629
    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2630
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2631
    /**
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2632
     * Returns a new CompletionStage that is completed normally with
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2633
     * the same value as this CompletableFuture when it completes
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2634
     * normally, and cannot be independently completed or otherwise
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2635
     * used in ways not defined by the methods of interface {@link
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2636
     * CompletionStage}.  If this CompletableFuture completes
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2637
     * exceptionally, then the returned CompletionStage completes
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2638
     * exceptionally with a CompletionException with this exception as
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2639
     * cause.
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2640
     *
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  2641
     * <p>Unless overridden by a subclass, a new non-minimal
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  2642
     * CompletableFuture with all methods available can be obtained from
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  2643
     * a minimal CompletionStage via {@link #toCompletableFuture()}.
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  2644
     * For example, completion of a minimal stage can be awaited by
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  2645
     *
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  2646
     * <pre> {@code minimalStage.toCompletableFuture().join(); }</pre>
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  2647
     *
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2648
     * @return the new CompletionStage
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 33674
diff changeset
  2649
     * @since 9
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2650
     */
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2651
    public CompletionStage<T> minimalCompletionStage() {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2652
        return uniAsMinimalStage();
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2653
    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2654
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2655
    /**
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2656
     * Completes this CompletableFuture with the result of
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2657
     * the given Supplier function invoked from an asynchronous
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2658
     * task using the given executor.
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2659
     *
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2660
     * @param supplier a function returning the value to be used
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2661
     * to complete this CompletableFuture
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2662
     * @param executor the executor to use for asynchronous execution
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2663
     * @return this CompletableFuture
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 33674
diff changeset
  2664
     * @since 9
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2665
     */
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2666
    public CompletableFuture<T> completeAsync(Supplier<? extends T> supplier,
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2667
                                              Executor executor) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2668
        if (supplier == null || executor == null)
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2669
            throw new NullPointerException();
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2670
        executor.execute(new AsyncSupply<T>(this, supplier));
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2671
        return this;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2672
    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2673
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2674
    /**
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2675
     * Completes this CompletableFuture with the result of the given
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2676
     * Supplier function invoked from an asynchronous task using the
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2677
     * default executor.
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2678
     *
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2679
     * @param supplier a function returning the value to be used
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2680
     * to complete this CompletableFuture
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2681
     * @return this CompletableFuture
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 33674
diff changeset
  2682
     * @since 9
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2683
     */
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2684
    public CompletableFuture<T> completeAsync(Supplier<? extends T> supplier) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2685
        return completeAsync(supplier, defaultExecutor());
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2686
    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2687
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2688
    /**
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2689
     * Exceptionally completes this CompletableFuture with
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2690
     * a {@link TimeoutException} if not otherwise completed
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2691
     * before the given timeout.
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2692
     *
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2693
     * @param timeout how long to wait before completing exceptionally
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2694
     *        with a TimeoutException, in units of {@code unit}
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2695
     * @param unit a {@code TimeUnit} determining how to interpret the
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2696
     *        {@code timeout} parameter
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2697
     * @return this CompletableFuture
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 33674
diff changeset
  2698
     * @since 9
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2699
     */
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2700
    public CompletableFuture<T> orTimeout(long timeout, TimeUnit unit) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2701
        if (unit == null)
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2702
            throw new NullPointerException();
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2703
        if (result == null)
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2704
            whenComplete(new Canceller(Delayer.delay(new Timeout(this),
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2705
                                                     timeout, unit)));
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2706
        return this;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2707
    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2708
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2709
    /**
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2710
     * Completes this CompletableFuture with the given value if not
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2711
     * otherwise completed before the given timeout.
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2712
     *
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2713
     * @param value the value to use upon timeout
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2714
     * @param timeout how long to wait before completing normally
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2715
     *        with the given value, in units of {@code unit}
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2716
     * @param unit a {@code TimeUnit} determining how to interpret the
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2717
     *        {@code timeout} parameter
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2718
     * @return this CompletableFuture
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 33674
diff changeset
  2719
     * @since 9
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2720
     */
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2721
    public CompletableFuture<T> completeOnTimeout(T value, long timeout,
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2722
                                                  TimeUnit unit) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2723
        if (unit == null)
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2724
            throw new NullPointerException();
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2725
        if (result == null)
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2726
            whenComplete(new Canceller(Delayer.delay(
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2727
                                           new DelayedCompleter<T>(this, value),
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2728
                                           timeout, unit)));
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2729
        return this;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2730
    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2731
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2732
    /**
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2733
     * Returns a new Executor that submits a task to the given base
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2734
     * executor after the given delay (or no delay if non-positive).
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2735
     * Each delay commences upon invocation of the returned executor's
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2736
     * {@code execute} method.
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2737
     *
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2738
     * @param delay how long to delay, in units of {@code unit}
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2739
     * @param unit a {@code TimeUnit} determining how to interpret the
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2740
     *        {@code delay} parameter
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2741
     * @param executor the base executor
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2742
     * @return the new delayed executor
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 33674
diff changeset
  2743
     * @since 9
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2744
     */
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2745
    public static Executor delayedExecutor(long delay, TimeUnit unit,
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2746
                                           Executor executor) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2747
        if (unit == null || executor == null)
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2748
            throw new NullPointerException();
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2749
        return new DelayedExecutor(delay, unit, executor);
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2750
    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2751
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2752
    /**
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2753
     * Returns a new Executor that submits a task to the default
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2754
     * executor after the given delay (or no delay if non-positive).
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2755
     * Each delay commences upon invocation of the returned executor's
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2756
     * {@code execute} method.
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2757
     *
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2758
     * @param delay how long to delay, in units of {@code unit}
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2759
     * @param unit a {@code TimeUnit} determining how to interpret the
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2760
     *        {@code delay} parameter
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2761
     * @return the new delayed executor
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 33674
diff changeset
  2762
     * @since 9
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2763
     */
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2764
    public static Executor delayedExecutor(long delay, TimeUnit unit) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2765
        if (unit == null)
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2766
            throw new NullPointerException();
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2767
        return new DelayedExecutor(delay, unit, ASYNC_POOL);
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2768
    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2769
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2770
    /**
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2771
     * Returns a new CompletionStage that is already completed with
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2772
     * the given value and supports only those methods in
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2773
     * interface {@link CompletionStage}.
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2774
     *
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2775
     * @param value the value
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2776
     * @param <U> the type of the value
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2777
     * @return the completed CompletionStage
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 33674
diff changeset
  2778
     * @since 9
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2779
     */
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2780
    public static <U> CompletionStage<U> completedStage(U value) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2781
        return new MinimalStage<U>((value == null) ? NIL : value);
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2782
    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2783
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2784
    /**
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2785
     * Returns a new CompletableFuture that is already completed
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2786
     * exceptionally with the given exception.
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2787
     *
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2788
     * @param ex the exception
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2789
     * @param <U> the type of the value
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2790
     * @return the exceptionally completed CompletableFuture
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 33674
diff changeset
  2791
     * @since 9
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2792
     */
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2793
    public static <U> CompletableFuture<U> failedFuture(Throwable ex) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2794
        if (ex == null) throw new NullPointerException();
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2795
        return new CompletableFuture<U>(new AltResult(ex));
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2796
    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2797
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2798
    /**
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2799
     * Returns a new CompletionStage that is already completed
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2800
     * exceptionally with the given exception and supports only those
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2801
     * methods in interface {@link CompletionStage}.
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2802
     *
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2803
     * @param ex the exception
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2804
     * @param <U> the type of the value
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2805
     * @return the exceptionally completed CompletionStage
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 33674
diff changeset
  2806
     * @since 9
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2807
     */
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2808
    public static <U> CompletionStage<U> failedStage(Throwable ex) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2809
        if (ex == null) throw new NullPointerException();
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2810
        return new MinimalStage<U>(new AltResult(ex));
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2811
    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2812
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2813
    /**
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2814
     * Singleton delay scheduler, used only for starting and
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2815
     * cancelling tasks.
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2816
     */
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2817
    static final class Delayer {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2818
        static ScheduledFuture<?> delay(Runnable command, long delay,
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2819
                                        TimeUnit unit) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2820
            return delayer.schedule(command, delay, unit);
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2821
        }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2822
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2823
        static final class DaemonThreadFactory implements ThreadFactory {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2824
            public Thread newThread(Runnable r) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2825
                Thread t = new Thread(r);
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2826
                t.setDaemon(true);
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2827
                t.setName("CompletableFutureDelayScheduler");
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2828
                return t;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2829
            }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2830
        }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2831
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2832
        static final ScheduledThreadPoolExecutor delayer;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2833
        static {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2834
            (delayer = new ScheduledThreadPoolExecutor(
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2835
                1, new DaemonThreadFactory())).
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2836
                setRemoveOnCancelPolicy(true);
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2837
        }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2838
    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2839
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2840
    // Little class-ified lambdas to better support monitoring
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2841
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2842
    static final class DelayedExecutor implements Executor {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2843
        final long delay;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2844
        final TimeUnit unit;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2845
        final Executor executor;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2846
        DelayedExecutor(long delay, TimeUnit unit, Executor executor) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2847
            this.delay = delay; this.unit = unit; this.executor = executor;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2848
        }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2849
        public void execute(Runnable r) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2850
            Delayer.delay(new TaskSubmitter(executor, r), delay, unit);
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2851
        }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2852
    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2853
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2854
    /** Action to submit user task */
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2855
    static final class TaskSubmitter implements Runnable {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2856
        final Executor executor;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2857
        final Runnable action;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2858
        TaskSubmitter(Executor executor, Runnable action) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2859
            this.executor = executor;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2860
            this.action = action;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2861
        }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2862
        public void run() { executor.execute(action); }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2863
    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2864
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2865
    /** Action to completeExceptionally on timeout */
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2866
    static final class Timeout implements Runnable {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2867
        final CompletableFuture<?> f;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2868
        Timeout(CompletableFuture<?> f) { this.f = f; }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2869
        public void run() {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2870
            if (f != null && !f.isDone())
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2871
                f.completeExceptionally(new TimeoutException());
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2872
        }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2873
    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2874
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2875
    /** Action to complete on timeout */
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2876
    static final class DelayedCompleter<U> implements Runnable {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2877
        final CompletableFuture<U> f;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2878
        final U u;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2879
        DelayedCompleter(CompletableFuture<U> f, U u) { this.f = f; this.u = u; }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2880
        public void run() {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2881
            if (f != null)
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2882
                f.complete(u);
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2883
        }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2884
    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2885
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2886
    /** Action to cancel unneeded timeouts */
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2887
    static final class Canceller implements BiConsumer<Object, Throwable> {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2888
        final Future<?> f;
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2889
        Canceller(Future<?> f) { this.f = f; }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2890
        public void accept(Object ignore, Throwable ex) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2891
            if (ex == null && f != null && !f.isDone())
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2892
                f.cancel(false);
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2893
        }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2894
    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2895
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2896
    /**
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2897
     * A subclass that just throws UOE for most non-CompletionStage methods.
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2898
     */
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2899
    static final class MinimalStage<T> extends CompletableFuture<T> {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2900
        MinimalStage() { }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2901
        MinimalStage(Object r) { super(r); }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2902
        @Override public <U> CompletableFuture<U> newIncompleteFuture() {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2903
            return new MinimalStage<U>(); }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2904
        @Override public T get() {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2905
            throw new UnsupportedOperationException(); }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2906
        @Override public T get(long timeout, TimeUnit unit) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2907
            throw new UnsupportedOperationException(); }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2908
        @Override public T getNow(T valueIfAbsent) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2909
            throw new UnsupportedOperationException(); }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2910
        @Override public T join() {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2911
            throw new UnsupportedOperationException(); }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2912
        @Override public boolean complete(T value) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2913
            throw new UnsupportedOperationException(); }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2914
        @Override public boolean completeExceptionally(Throwable ex) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2915
            throw new UnsupportedOperationException(); }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2916
        @Override public boolean cancel(boolean mayInterruptIfRunning) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2917
            throw new UnsupportedOperationException(); }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2918
        @Override public void obtrudeValue(T value) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2919
            throw new UnsupportedOperationException(); }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2920
        @Override public void obtrudeException(Throwable ex) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2921
            throw new UnsupportedOperationException(); }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2922
        @Override public boolean isDone() {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2923
            throw new UnsupportedOperationException(); }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2924
        @Override public boolean isCancelled() {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2925
            throw new UnsupportedOperationException(); }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2926
        @Override public boolean isCompletedExceptionally() {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2927
            throw new UnsupportedOperationException(); }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2928
        @Override public int getNumberOfDependents() {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2929
            throw new UnsupportedOperationException(); }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2930
        @Override public CompletableFuture<T> completeAsync
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2931
            (Supplier<? extends T> supplier, Executor executor) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2932
            throw new UnsupportedOperationException(); }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2933
        @Override public CompletableFuture<T> completeAsync
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2934
            (Supplier<? extends T> supplier) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2935
            throw new UnsupportedOperationException(); }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2936
        @Override public CompletableFuture<T> orTimeout
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2937
            (long timeout, TimeUnit unit) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2938
            throw new UnsupportedOperationException(); }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2939
        @Override public CompletableFuture<T> completeOnTimeout
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2940
            (T value, long timeout, TimeUnit unit) {
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2941
            throw new UnsupportedOperationException(); }
41128
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  2942
        @Override public CompletableFuture<T> toCompletableFuture() {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  2943
            Object r;
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  2944
            if ((r = result) != null)
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  2945
                return new CompletableFuture<T>(encodeRelay(r));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  2946
            else {
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  2947
                CompletableFuture<T> d = new CompletableFuture<>();
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  2948
                unipush(new UniRelay<T,T>(d, this));
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  2949
                return d;
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  2950
            }
0dfc0bc2196c 8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents: 40817
diff changeset
  2951
        }
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2952
    }
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2953
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2954
    // VarHandle mechanics
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2955
    private static final VarHandle RESULT;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2956
    private static final VarHandle STACK;
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2957
    private static final VarHandle NEXT;
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2958
    static {
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2959
        try {
39724
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2960
            MethodHandles.Lookup l = MethodHandles.lookup();
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2961
            RESULT = l.findVarHandle(CompletableFuture.class, "result", Object.class);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2962
            STACK = l.findVarHandle(CompletableFuture.class, "stack", Completion.class);
d935d42f6d93 8157522: Performance improvements to CompletableFuture
dl
parents: 36954
diff changeset
  2963
            NEXT = l.findVarHandle(Completion.class, "next", Completion.class);
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2964
        } catch (ReflectiveOperationException e) {
49565
b5705ade8c8d 8197531: Miscellaneous changes imported from jsr166 CVS 2018-04
dl
parents: 47306
diff changeset
  2965
            throw new ExceptionInInitializerError(e);
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2966
        }
32987
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2967
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2968
        // Reduce the risk of rare disastrous classloading in first call to
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2969
        // LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773
e5e5ab01398e 8134851: Integrate CompletableFuture with API enhancements
dl
parents: 28540
diff changeset
  2970
        Class<?> ensureLoaded = LockSupport.class;
16745
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2971
    }
fcce5e09e23b 8005696: Add CompletableFuture
chegar
parents:
diff changeset
  2972
}