Creates a new complete CompletableFuture with given encoded result.
Creates a new incomplete CompletableFuture.
Pushes completion to this and b unless both done. Caller should first check that either result or b.result is null.
If not already completed, completes this CompletableFuture with a {@link CancellationException}. Dependent CompletableFutures that have not already completed will also complete exceptionally, with a {@link CompletionException} caused by this {@code CancellationException}.
Traverses stack and unlinks one or more dead Completions, if found.
If not already completed, sets the value returned by {@link #get()} and related methods to the given value.
Completes this CompletableFuture with the result of the given Supplier function invoked from an asynchronous task using the given executor.
Completes this CompletableFuture with the result of the given Supplier function invoked from an asynchronous task using the default executor.
If not already completed, causes invocations of {@link #get()} and related methods to throw the given exception.
Completes with the null value, unless already completed.
Completes this CompletableFuture with the given value if not otherwise completed before the given timeout.
Completes with a non-exceptional result, unless already completed.
Returns a new CompletableFuture that is completed normally with the same value as this CompletableFuture when it completes normally. If this CompletableFuture completes exceptionally, then the returned CompletableFuture completes exceptionally with a CompletionException with this exception as cause. The behavior is equivalent to {@code thenApply(x -> x)}. This method may be useful as a form of "defensive copying", to prevent clients from completing, while still being able to arrange dependent actions.
Returns the default Executor used for async methods that do not specify an Executor. This class uses the {@link ForkJoinPool#commonPool()} if it supports more than one parallel thread, or else an Executor using one thread per async task. This method may be overridden in subclasses to return an Executor that provides at least one independent thread.
Returns a new CompletableFuture that is completed when this CompletableFuture completes, with the result of the given function of the exception triggering this CompletableFuture's completion when it completes exceptionally; otherwise, if this CompletableFuture completes normally, then the returned CompletableFuture also completes normally with the same value. Note: More flexible versions of this functionality are available using methods {@code whenComplete} and {@code handle}.
Callback invoked when the operation fails.
Waits if necessary for this future to complete, and then returns its result.
Waits if necessary for at most the given time for this future to complete, and then returns its result, if available.
Returns the result value (or throws any encountered exception) if completed, else returns the given valueIfAbsent.
Returns the estimated number of CompletableFutures whose completions are awaiting completion of this CompletableFuture. This method is designed for use in monitoring system state, not for synchronization control.
Returns the result value when complete, or throws an (unchecked) exception if completed exceptionally. To better conform with the use of common functional forms, if a computation involved in the completion of this CompletableFuture threw an exception, this method throws an (unchecked) {@link CompletionException} with the underlying exception as its cause.
Returns a new CompletionStage that is completed normally with the same value as this CompletableFuture when it completes normally, and cannot be independently completed or otherwise used in ways not defined by the methods of interface {@link CompletionStage}. If this CompletableFuture completes exceptionally, then the returned CompletionStage completes exceptionally with a CompletionException with this exception as cause.
Forcibly causes subsequent invocations of method {@link #get()} and related methods to throw the given exception, whether or not already completed. This method is designed for use only in error recovery actions, and even in such situations may result in ongoing dependent completions using established versus overwritten outcomes.
Forcibly sets or resets the value subsequently returned by method {@link #get()} and related methods, whether or not already completed. This method is designed for use only in error recovery actions, and even in such situations may result in ongoing dependent completions using established versus overwritten outcomes.
Exceptionally completes this CompletableFuture with a {@link TimeoutException} if not otherwise completed before the given timeout.
Pushes completion to this and b unless either done. Caller should first check that result and b.result are both null.
Pops and tries to trigger all reachable dependents. Call only when known to be done.
Post-processing after successful BiCompletion tryFire.
Unconditionally pushes c onto stack, retrying if necessary.
Callback invoked when the operation completes.
Callback invoked when the operation completes.
Returns this CompletableFuture.
Returns a string identifying this CompletableFuture, as well as its completion state. The state, in brackets, contains the String {@code "Completed Normally"} or the String {@code "Completed Exceptionally"}, or the String {@code "Not completed"} followed by the number of CompletableFutures dependent upon its completion, if any.
Returns true if successfully pushed c onto stack.
Pushes the given completion unless it completes while trying. Caller should first check that result is null.
Returns {@code true} if completed in any fashion: normally, exceptionally, or via cancellation.
Returns {@code true} if this CompletableFuture was cancelled before it completed normally.
Returns {@code true} if this CompletableFuture completed exceptionally, in any way. Possible causes include cancellation, explicit invocation of {@code completeExceptionally}, and abrupt termination of a CompletionStage action.
{@code supplyAsync(supplier, delayedExecutor(timeout, timeUnit))}. To support methods with delays and timeouts, this class maintains at most one daemon thread for triggering and cancelling actions, not for running them.
<li>All CompletionStage methods are implemented independently of other methods, so the behavior of one method is not impacted by overrides of others in subclasses.
<li>All CompletionStage methods return CompletableFutures. To restrict usages to only those methods defined in interface CompletionStage, use method {@link #minimalCompletionStage}. Or to ensure only that clients do not themselves modify a future, use method {@link #copy}. </ul>
<p>CompletableFuture also implements {@link Future} with the following policies: <ul>
<li>Since (unlike {@link FutureTask}) this class has no direct control over the computation that causes it to be completed, cancellation is treated as just another form of exceptional completion. Method {@link #cancel cancel} has the same effect as {@code completeExceptionally(new CancellationException())}. Method {@link #isCompletedExceptionally} can be used to determine if a CompletableFuture completed in any exceptional fashion.
<li>In case of exceptional completion with a CompletionException, methods {@link #get()} and {@link #get(long, TimeUnit)} throw an {@link ExecutionException} with the same cause as held in the corresponding CompletionException. To simplify usage in most contexts, this class also defines methods {@link #join()} and {@link #getNow} that instead throw the CompletionException directly in these cases. </ul>
<p>Arguments used to pass a completion result (that is, for parameters of type {@code T}) for methods accepting them may be null, but passing a null value for any other parameter will result in a {@link NullPointerException} being thrown.
<p>Subclasses of this class should normally override the "virtual constructor" method {@link #newIncompleteFuture}, which establishes the concrete type returned by CompletionStage methods. For example, here is a class that substitutes a different default Executor and disables the {@code obtrude} methods:
<pre> {@code class MyCompletableFuture(T) : CompletableFuture!(T) { final Executor myExecutor = ...; MyCompletableFuture() { } <U> CompletableFuture!(U) newIncompleteFuture() { return new MyCompletableFuture!(U)(); } Executor defaultExecutor() { return myExecutor; } void obtrudeValue(T value) { throw new UnsupportedOperationException(); } void obtrudeException(Throwable ex) { throw new UnsupportedOperationException(); } }}</pre>
@author Doug Lea @param <T> The result type returned by this future's {@code join} and {@code get} methods
A {@link Future} that may be explicitly completed (setting its value and status), and may be used as a {@link CompletionStage}, supporting dependent functions and actions that trigger upon its completion.
<p>When two or more threads attempt to {@link #complete complete}, {@link #completeExceptionally completeExceptionally}, or {@link #cancel cancel} a CompletableFuture, only one of them succeeds.
<p>In addition to these and related methods for directly manipulating status and results, CompletableFuture implements interface {@link CompletionStage} with the following policies: <ul>
<li>Actions supplied for dependent completions of <em>non-async</em> methods may be performed by the thread that completes the current CompletableFuture, or by any other caller of a completion method.
<li>All <em>async</em> methods without an explicit Executor argument are performed using the {@link ForkJoinPool#commonPool()} (unless it does not support a parallelism level of at least two, in which case, a new Thread is created to run each task). This may be overridden for non-static methods in subclasses by defining method {@link #defaultExecutor()}. To simplify monitoring, debugging, and tracking, all generated asynchronous tasks are instances of the marker interface {@link AsynchronousCompletionTask}. Operations with time-delays can use adapter methods defined in this class, for