Lambda从入门到精通之四十 CompletableFuture异步编程 API总结

CompletableFuture中的方法非常强大,方法也非常多,我们基于前面的章节已经深入了解的CompletableFuture的使用方法,那么我们接下来对CompletableFuture的API进行总结。

二选一,没有返回值

CompletableFuture<Void> acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action) 
		Returns a new CompletionStage that, when either this or the other given stage complete normally, is executed with the corresponding result as argument to the supplied action. 

CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action) 
		Returns a new CompletionStage that, when either this or the other given stage complete normally, is executed using this stage's default asynchronous execution facility, with the corresponding result as argument to the supplied action. 

CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor) 
		Returns a new CompletionStage that, when either this or the other given stage complete normally, is executed using the supplied executor, with the corresponding result as argument to the supplied function. 

二选一,有返回值

<U> CompletableFuture<U> applyToEither(CompletionStage<? extends T> other, Function<? super T,U> fn) 
		Returns a new CompletionStage that, when either this or the other given stage complete normally, is executed with the corresponding result as argument to the supplied function. 

<U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T,U> fn) 
		Returns a new CompletionStage that, when either this or the other given stage complete normally, is executed using this stage's default asynchronous execution facility, with the corresponding result as argument to the supplied function. 

<U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T,U> fn, Executor executor) 
		Returns a new CompletionStage that, when either this or the other given stage complete normally, is executed using the supplied executor, with the corresponding result as argument to the supplied function. 

等待并收集计算完成的结果

static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs) 
		Returns a new CompletableFuture that is completed when all of the given CompletableFutures complete. 

static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs) 
		Returns a new CompletableFuture that is completed when any of the given CompletableFutures complete, with the same result. 

当CompletableFuture的计算结果完成或者抛出异常时,执行特给定的Action,返回的类型可以与入参类型不同

<U> CompletableFuture<U> handle(BiFunction<? super T,Throwable,? extends U> fn) 
		Returns a new CompletionStage that, when this stage completes either normally or exceptionally, is executed with this stage's result and exception as arguments to the supplied function. 

<U> CompletableFuture<U> handleAsync(BiFunction<? super T,Throwable,? extends U> fn) 
		Returns a new CompletionStage that, when this stage completes either normally or exceptionally, is executed using this stage's default asynchronous execution facility, with this stage's result and exception as arguments to the supplied function. 

<U> CompletableFuture<U> handleAsync(BiFunction<? super T,Throwable,? extends U> fn, Executor executor) 
		Returns a new CompletionStage that, when this stage completes either normally or exceptionally, is executed using the supplied executor, with this stage's result and exception as arguments to the supplied function. 

等待任务运行完成,但并不使用前面任务的计算结果作为后续执行Action的参数,仅仅是等待前面任务运行完成

CompletableFuture<Void> runAfterBoth(CompletionStage<?> other, Runnable action) 
		Returns a new CompletionStage that, when this and the other given stage both complete normally, executes the given action. 

CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action) 
		Returns a new CompletionStage that, when this and the other given stage complete normally, executes the given action using this stage's default asynchronous execution facility. 

CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor) 
		Returns a new CompletionStage that, when this and the other given stage complete normally, executes the given action using the supplied executor See the CompletionStage documentation for rules covering exceptional completion. 

CompletableFuture<Void> runAfterEither(CompletionStage<?> other, Runnable action) 
		Returns a new CompletionStage that, when either this or the other given stage complete normally, executes the given action. 

CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action) 
		Returns a new CompletionStage that, when either this or the other given stage complete normally, executes the given action using this stage's default asynchronous execution facility. 

CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action, Executor executor) 
		Returns a new CompletionStage that, when either this or the other given stage complete normally, executes the given action using supplied executor. 

static CompletableFuture<Void> runAsync(Runnable runnable) 
		Returns a new CompletableFuture that is asynchronously completed by a task running in the ForkJoinPool.commonPool() after it runs the given action. 

static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor) 
		Returns a new CompletableFuture that is asynchronously completed by a task running in the given executor after it runs the given action. 

按照给定的方法计算生成CompletableFuture类型的结果

static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) 
		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. 

static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) 
		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. 

等待前面的任务运行结束,将运行结果作为参数执行Action,没有返回值

CompletableFuture<Void> thenAccept(Consumer<? super T> action) 
		Returns a new CompletionStage that, when this stage completes normally, is executed with this stage's result as the argument to the supplied action. 

CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action) 
		Returns a new CompletionStage that, when this stage completes normally, is executed using this stage's default asynchronous execution facility, with this stage's result as the argument to the supplied action. 

CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor) 
		Returns a new CompletionStage that, when this stage completes normally, is executed using the supplied Executor, with this stage's result as the argument to the supplied action. 

等待两个任务运行结束,将运行结果作为参数执行BiConsumer Action,返回结果由BiConsumer来定义

<U> CompletableFuture<Void> thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super T,? super U> action) 
		Returns a new CompletionStage that, when this and the other given stage both complete normally, is executed with the two results as arguments to the supplied action. 

<U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T,? super U> action) 
		Returns a new CompletionStage that, when this and the other given stage complete normally, is executed using this stage's default asynchronous execution facility, with the two results as arguments to the supplied action. 

<U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T,? super U> action, Executor executor) 
		Returns a new CompletionStage that, when this and the other given stage complete normally, is executed using the supplied executor, with the two results as arguments to the supplied function. 

将前一个任务结果转换后返回,两个任务有依赖关系,后一个任务计算的参数是前一个任务的计算结果,最终的计算结果由后一个计算任务返回

<U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn) 
		Returns a new CompletionStage that, when this stage completes normally, is executed with this stage's result as the argument to the supplied function. 

<U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn) 
		Returns a new CompletionStage that, when this stage completes normally, is executed using this stage's default asynchronous execution facility, with this stage's result as the argument to the supplied function. 

<U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor) 
		Returns a new CompletionStage that, when this stage completes normally, is executed using the supplied Executor, with this stage's result as the argument to the supplied function. 

将两个任务结果合并返回,两个任务并行计算,然后组合到一起返回结果

<U,V> CompletableFuture<V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn) 
		Returns a new CompletionStage that, when this and the other given stage both complete normally, is executed with the two results as arguments to the supplied function. 

<U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn) 
		Returns a new CompletionStage that, when this and the other given stage complete normally, is executed using this stage's default asynchronous execution facility, with the two results as arguments to the supplied function. 

<U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn, Executor executor) 
		Returns a new CompletionStage that, when this and the other given stage complete normally, is executed using the supplied executor, with the two results as arguments to the supplied function. 

将两个任务结果转换返回,两个任务有依赖关系,后一个任务计算的参数是前一个任务的计算结果,最终的计算结果由后一个计算任务返回

<U> CompletableFuture<U> thenCompose(Function<? super T,? extends CompletionStage<U>> fn) 
		Returns a new CompletionStage that, when this stage completes normally, is executed with this stage as the argument to the supplied function. 

<U> CompletableFuture<U> thenComposeAsync(Function<? super T,? extends CompletionStage<U>> fn) 
		Returns a new CompletionStage that, when this stage completes normally, is executed using this stage's default asynchronous execution facility, with this stage as the argument to the supplied function. 

<U> CompletableFuture<U> thenComposeAsync(Function<? super T,? extends CompletionStage<U>> fn, Executor executor) 
		Returns a new CompletionStage that, when this stage completes normally, is executed using the supplied Executor, with this stage's result as the argument to the supplied function. 

前一个任务计算完成,开始执行Action,Action的执行不依赖前一个任务的结果,也就是不需要前一个计算结果作为参数

CompletableFuture<Void> thenRun(Runnable action) 
		Returns a new CompletionStage that, when this stage completes normally, executes the given action. 

CompletableFuture<Void> thenRunAsync(Runnable action) 
		Returns a new CompletionStage that, when this stage completes normally, executes the given action using this stage's default asynchronous execution facility. 

CompletableFuture<Void> thenRunAsync(Runnable action, Executor executor) 
		Returns a new CompletionStage that, when this stage completes normally, executes the given action using the supplied Executor. 

当CompletableFuture的计算结果完成或者抛出异常时,执行特给定的Action,返回的类型要与入参类型相同

CompletableFuture<T> whenComplete(BiConsumer<? super T,? super Throwable> action) 
		Returns a new CompletionStage with the same result or exception as this stage, and when this stage completes, executes the given action with the result (or null if none) and the exception (or null if none) of this stage. 

CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> action) 
		Returns a new CompletionStage with the same result or exception as this stage, and when this stage completes, executes the given action executes the given action using this stage's default asynchronous execution facility, with the result (or null if none) and the exception (or null if none) of this stage as arguments. 

CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> action, Executor executor) 
		Returns a new CompletionStage with the same result or exception as this stage, and when this stage completes, executes using the supplied Executor, the given action with the result (or null if none) and the exception (or null if none) of this stage as arguments. 

其他非方法,提供计算、状态检查、结果获取等功能

boolean cancel(boolean mayInterruptIfRunning) 
boolean complete(T value) 
static <U> CompletableFuture<U> completedFuture(U value) 
boolean completeExceptionally(Throwable ex) 
CompletableFuture<T> exceptionally(Function<Throwable,? extends T> fn) 
T get() 
T get(long timeout, TimeUnit unit) 
T getNow(T valueIfAbsent) 
int getNumberOfDependents() 
boolean isCancelled() 
boolean isCompletedExceptionally() 
boolean isDone() 
T join() 
void obtrudeException(Throwable ex) 
void obtrudeValue(T value) 
CompletableFuture<T> toCompletableFuture()