Sdílet prostřednictvím


CompletableFuture Class

Definition

A Future that may be explicitly completed (setting its value and status), and may be used as a CompletionStage, supporting dependent functions and actions that trigger upon its completion.

[Android.Runtime.Register("java/util/concurrent/CompletableFuture", ApiSince=24, DoNotGenerateAcw=true)]
[Java.Interop.JavaTypeParameters(new System.String[] { "T" })]
public class CompletableFuture : Java.Lang.Object, IDisposable, Java.Interop.IJavaPeerable, Java.Util.Concurrent.ICompletionStage, Java.Util.Concurrent.IFuture
[<Android.Runtime.Register("java/util/concurrent/CompletableFuture", ApiSince=24, DoNotGenerateAcw=true)>]
[<Java.Interop.JavaTypeParameters(new System.String[] { "T" })>]
type CompletableFuture = class
    inherit Object
    interface ICompletionStage
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
    interface IFuture
Inheritance
CompletableFuture
Attributes
Implements

Remarks

A Future that may be explicitly completed (setting its value and status), and may be used as a CompletionStage, supporting dependent functions and actions that trigger upon its completion.

When two or more threads attempt to #complete complete, #completeExceptionally completeExceptionally, or #cancel cancel a CompletableFuture, only one of them succeeds.

In addition to these and related methods for directly manipulating status and results, CompletableFuture implements interface 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 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). To simplify monitoring, debugging, and tracking, all generated asynchronous tasks are instances of the marker interface AsynchronousCompletionTask. Operations with time-delays can use adapter methods defined in this class, for example: 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 public methods, so the behavior of one method is not impacted by overrides of others in subclasses.

</ul>

CompletableFuture also implements Future with the following policies: <ul>

<li>Since (unlike 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 #cancel cancel has the same effect as completeExceptionally(new CancellationException()). Method #isCompletedExceptionally can be used to determine if a CompletableFuture completed in any exceptional fashion.

<li>In case of exceptional completion with a CompletionException, methods #get() and #get(long, TimeUnit) throw an ExecutionException with the same cause as held in the corresponding CompletionException. To simplify usage in most contexts, this class also defines methods #join() and #getNow that instead throw the CompletionException directly in these cases. </ul>

Arguments used to pass a completion result (that is, for parameters of type T) for methods accepting them may be null, but passing a null value for any other parameter will result in a NullPointerException being thrown.

Added in 1.8.

Java documentation for java.util.concurrent.CompletableFuture.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

CompletableFuture()

Creates a new incomplete CompletableFuture.

CompletableFuture(IntPtr, JniHandleOwnership)

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
IsCancelled

Returns true if this CompletableFuture was cancelled before it completed normally.

IsCompletedExceptionally

Returns true if this CompletableFuture completed exceptionally, in any way.

IsDone

Returns true if completed in any fashion: normally, exceptionally, or via cancellation.

JniIdentityHashCode (Inherited from Object)
JniPeerMembers
NumberOfDependents

Returns the estimated number of CompletableFutures whose completions are awaiting completion of this CompletableFuture.

PeerReference (Inherited from Object)
ThresholdClass
ThresholdType

Methods

AcceptEither(ICompletionStage, IConsumer)
AcceptEitherAsync(ICompletionStage, IConsumer, IExecutor)
AcceptEitherAsync(ICompletionStage, IConsumer)
AllOf(CompletableFuture[])

Returns a new CompletableFuture that is completed when all of the given CompletableFutures complete.

AnyOf(CompletableFuture[])

Returns a new CompletableFuture that is completed when any of the given CompletableFutures complete, with the same result.

ApplyToEither(ICompletionStage, IFunction)
ApplyToEitherAsync(ICompletionStage, IFunction, IExecutor)
ApplyToEitherAsync(ICompletionStage, IFunction)
Cancel(Boolean)

If not already completed, completes this CompletableFuture with a CancellationException.

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
Complete(Object)

If not already completed, sets the value returned by #get() and related methods to the given value.

CompleteAsync(ISupplier, IExecutor)

Completes this CompletableFuture with the result of the given Supplier function invoked from an asynchronous task using the given executor.

CompleteAsync(ISupplier)

Completes this CompletableFuture with the result of the given Supplier function invoked from an asynchronous task using the default executor.

CompletedFuture(Object)

Returns a new CompletableFuture that is already completed with the given value.

CompletedStage(Object)

Returns a new CompletionStage that is already completed with the given value and supports only those methods in interface CompletionStage.

CompleteExceptionally(Throwable)

If not already completed, causes invocations of #get() and related methods to throw the given exception.

CompleteOnTimeout(Object, Int64, TimeUnit)

Completes this CompletableFuture with the given value if not otherwise completed before the given timeout.

Copy()

Returns a new CompletableFuture that is completed normally with the same value as this CompletableFuture when it completes normally.

DefaultExecutor()

Returns the default Executor used for async methods that do not specify an Executor.

DelayedExecutor(Int64, TimeUnit, IExecutor)

Returns a new Executor that submits a task to the given base executor after the given delay (or no delay if non-positive).

DelayedExecutor(Int64, TimeUnit)

Returns a new Executor that submits a task to the default executor after the given delay (or no delay if non-positive).

Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
Exceptionally(IFunction)

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.

ExceptionallyAsync(IFunction, IExecutor)
ExceptionallyAsync(IFunction)
ExceptionallyCompose(IFunction)
ExceptionallyComposeAsync(IFunction, IExecutor)
ExceptionallyComposeAsync(IFunction)
FailedFuture(Throwable)

Returns a new CompletableFuture that is already completed exceptionally with the given exception.

FailedStage(Throwable)

Returns a new CompletionStage that is already completed exceptionally with the given exception and supports only those methods in interface CompletionStage.

Get()

Waits if necessary for this future to complete, and then returns its result.

Get(Int64, TimeUnit)

Waits if necessary for at most the given time for this future to complete, and then returns its result, if available.

GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
GetNow(Object)

Returns the result value (or throws any encountered exception) if completed, else returns the given valueIfAbsent.

Handle(IBiFunction)
HandleAsync(IBiFunction, IExecutor)
HandleAsync(IBiFunction)
JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Join()

Returns the result value when complete, or throws an (unchecked) exception if completed exceptionally.

MinimalCompletionStage()

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 CompletionStage.

NewIncompleteFuture()

Returns a new incomplete CompletableFuture of the type to be returned by a CompletionStage method.

Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
ObtrudeException(Throwable)

Forcibly causes subsequent invocations of method #get() and related methods to throw the given exception, whether or not already completed.

ObtrudeValue(Object)

Forcibly sets or resets the value subsequently returned by method #get() and related methods, whether or not already completed.

OrTimeout(Int64, TimeUnit)

Exceptionally completes this CompletableFuture with a TimeoutException if not otherwise completed before the given timeout.

RunAfterBoth(ICompletionStage, IRunnable)
RunAfterBothAsync(ICompletionStage, IRunnable, IExecutor)
RunAfterBothAsync(ICompletionStage, IRunnable)
RunAfterEither(ICompletionStage, IRunnable)
RunAfterEitherAsync(ICompletionStage, IRunnable, IExecutor)
RunAfterEitherAsync(ICompletionStage, IRunnable)
RunAsync(IRunnable, IExecutor)

Returns a new CompletableFuture that is asynchronously completed by a task running in the given executor after it runs the given action.

RunAsync(IRunnable)

Returns a new CompletableFuture that is asynchronously completed by a task running in the ForkJoinPool#commonPool() after it runs the given action.

SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
SupplyAsync(ISupplier, IExecutor)

Returns a new CompletableFuture that is asynchronously completed by a task running in the given executor with the value obtained by calling the given Supplier.

SupplyAsync(ISupplier)

Returns a new CompletableFuture that is asynchronously completed by a task running in the ForkJoinPool#commonPool() with the value obtained by calling the given Supplier.

ThenAccept(IConsumer)
ThenAcceptAsync(IConsumer, IExecutor)
ThenAcceptAsync(IConsumer)
ThenAcceptBoth(ICompletionStage, IBiConsumer)
ThenAcceptBothAsync(ICompletionStage, IBiConsumer, IExecutor)
ThenAcceptBothAsync(ICompletionStage, IBiConsumer)
ThenApply(IFunction)
ThenApplyAsync(IFunction, IExecutor)
ThenApplyAsync(IFunction)
ThenCombine(ICompletionStage, IBiFunction)
ThenCombineAsync(ICompletionStage, IBiFunction, IExecutor)
ThenCombineAsync(ICompletionStage, IBiFunction)
ThenCompose(IFunction)
ThenComposeAsync(IFunction, IExecutor)
ThenComposeAsync(IFunction)
ThenRun(IRunnable)
ThenRunAsync(IRunnable, IExecutor)
ThenRunAsync(IRunnable)
ToArray<T>() (Inherited from Object)
ToCompletableFuture()

Returns this CompletableFuture.

ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
WhenComplete(IBiConsumer)
WhenCompleteAsync(IBiConsumer, IExecutor)
WhenCompleteAsync(IBiConsumer)

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)
GetAsync(IFuture, Int64, TimeUnit)
GetAsync(IFuture)

Applies to