CompletableFuture has the following static methods:
supplyAsync(Supplier<T>)
andsupplyAsync(Supplier<T>, Executor)
- Create a future from code that runs on a different threadrunAsync(Runnable, Executor)
andrunAsync(Runnable)
- Similar to supplyAsync, but returns a void futurecompletedFuture(T)
- Returns a future that's already completeallOf(CompletableFuture...)
- A future that completes when all futures completeanyOf(CompletableFuture...)
- Similar to allOf, but returns as soon as any of the futures complete
Methods for introspecting the state of a future:
isDone()
,isCancelled()
andisCompletedExceptionally()
- Self-explanatory?getNumberOfDependents()
- Estimated number of callbacks for future completion (For monitoring only!)
Methods for extracting the value of a future:
getNow(T default)
- Non-blocking - returns the default value if it is not complete.get()
- Blocking get - throws checked exceptionsget(long, TimeUnit)
- Blocking get, but with a timeoutjoin()
- Blocking get - throws unchecked exceptions
Methods for setting the state of the future
complete(Object)
,completeExceptionally(Throwable)
andcancel(boolean)
- Complete the future in various waysobtrudeValue(Object)
andobtrudeException(Throwable)
- Complete the future, mutating the value it even if it is already completed!
It has a bunch of methods that operate on a future to create a new (and improved!) future:
thenApply(Function)
- transform value -> valuethenApplyAsync(Function, Executor)
-thenApplyAsync(Function)
-thenAccept(Consumer)
-thenAcceptAsync(Consumer, Executor)
-thenAcceptAsync(Consumer)
-thenRun(Runnable)
-thenRunAsync(Runnable, Executor)
-thenRunAsync(Runnable)
-thenCombine(CompletionStage, BiFunction)
-thenCombineAsync(CompletionStage, BiFunction, Executor)
-thenCombineAsync(CompletionStage, BiFunction)
-thenAcceptBoth(CompletionStage, BiConsumer)
-thenAcceptBothAsync(CompletionStage, BiConsumer, Executor)
-thenAcceptBothAsync(CompletionStage, BiConsumer)
-runAfterBoth(CompletionStage, Runnable)
-runAfterBothAsync(CompletionStage, Runnable, Executor)
-runAfterBothAsync(CompletionStage, Runnable)
-applyToEither(CompletionStage, Function)
-applyToEitherAsync(CompletionStage, Function, Executor)
-applyToEitherAsync(CompletionStage, Function)
-acceptEither(CompletionStage, Consumer)
-acceptEitherAsync(CompletionStage, Consumer, Executor)
-acceptEitherAsync(CompletionStage, Consumer)
-runAfterEither(CompletionStage, Runnable)
-runAfterEitherAsync(CompletionStage, Runnable, Executor)
-runAfterEitherAsync(CompletionStage, Runnable)
-thenCompose(Function)
- transform value -> CompletionStage(value)thenComposeAsync(Function, Executor)
-thenComposeAsync(Function)
-whenComplete(BiConsumer)
-whenCompleteAsync(BiConsumer, Executor)
-whenCompleteAsync(BiConsumer)
-handle(BiFunction)
- transform either value or exceptionhandleAsync(BiFunction, Executor)
-handleAsync(BiFunction)
-exceptionally(Function)
- transform exception
There are a lot of methods here - most of them can be expressed in terms of handle
and thenCompose
and can be considered convenience methods and to better express intent.
Some of them can be considered callbacks instead of transforms, but they still return futures so you can take actions on the completion, even if you don't care about their values.
With Java 9 we got some additions to the API.
New static methods:
delayedExecutor(long, TimeUnit)
anddelayedExecutor(long, TimeUnit, Executor)
- Get an executor that delays the workcompletedStage(Object)
,failedFuture(Throwable)
andfailedStage(Throwable)
- Complements for thecompletedFuture(Object)
method
New instance methods:
newIncompleteFuture()
- Virtual constructor, intended for subclassingdefaultExecutor()
- Get the default executor that is used for default*Async
transforms.copy()
- Convenience method forthenApply(x -> x)
minimalCompletionStage()
- Returns a more restricted futurecompleteAsync(Supplier, Executor)
andcompleteAsync(Supplier)
- Complete the future using a suppliercompleteOnTimeout(Object, long, TimeUnit)
- Complete the future with a value after a timeoutorTimeout(long, TimeUnit)
- Complete the future with an exception after a timeout
Nothing has changed - this API seems to have stabilized.