关于java:Java8-CompletableFuture-总结

4次阅读

共计 6361 个字符,预计需要花费 16 分钟才能阅读完成。

原文链接:https://fxbing.github.io/2019…


CompletableFuture 是 Java8 中新增的用来进行函数式异步编程的工具类。

最近学习源码的过程中看到有很多 CompletableFuture 的应用,感觉本人对这个类中的各个办法的应用场景和办法不是很相熟,遂参考了上面几篇博客进行学习(本文大部分内容也都来自上面几篇博客):

Java CompletableFuture 详解

Java8 新的异步编程形式 CompletableFuture(一)

Java8 新的异步编程形式 CompletableFuture(二)

Java8 新的异步编程形式 CompletableFuture(三)

下面的博客介绍的比拟具体,为了本人查阅回看的不便,这里对这些办法进行一下总结(这里只总结不举例,具体应用须要看下面的博客)。

Future 接口

Feture 接口蕴含五个办法,介绍如下:

  • boolean cancel (boolean mayInterruptIfRunning) 勾销工作的执行。参数指定是否立刻中断工作执行,或者等等工作完结
  • boolean isCancelled () 工作是否曾经勾销,工作失常实现前将其勾销,则返回 true
  • boolean isDone () 工作是否曾经实现。须要留神的是如果工作失常终止、异样或勾销,都将返回true
  • V get () throws InterruptedException, ExecutionException 期待工作执行完结,而后取得 V 类型的后果。InterruptedException 线程被中断异样,ExecutionException 工作执行异样,如果工作被勾销,还会抛出 CancellationException
  • V get (long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException 同下面的 get 性能一样,多了设置超时工夫。参数 timeout 指定超时工夫,uint 指定工夫的单位,在枚举类 TimeUnit 中有相干的定义。如果计 算超时,将抛出 TimeoutException

被动实现计算

CompletableFuture实现了 CompletionStageFuture两个接口。

通过阻塞或者轮询取得后果

办法名 形容
public T get() Future接口实现
public T get(long timeout, TimeUnit unit) Future接口实现
public T getNow(T valueIfAbsent) 如果后果曾经计算完则返回后果或者抛出异样,否则返回给定的 valueIfAbsent 值。
public T join() 返回计算的后果或者抛出一个 unchecked 异样(CompletionException)

join()get() 的区别是,join()只会抛出 未查看异样 (不须要应用try...catch.. 进行解决),而 get() 会抛出 查看异样

异步获取后果

  • 上面两个函数的调用会立刻执行,并且只能执行一次。
  • 如果该工作曾经执行实现,那么上面两个调用会有效,只能获取执行实现的后果。其实就是使工作立刻完结(返回指定后果或者指定抛出异样)。
  • 比拟适宜须要返回 CompletableFuture 的办法,先创立一个空的 CompletableFuture,之后通过上面两个函数指定后面创立的CompletableFuture 的返回值。
办法名 形容
complete(T t) 实现异步执行,并返回 future 的后果
completeExceptionally(Throwable ex) 异步执行不失常的完结

动态工厂办法

run 和 supply 的次要区别是异步操作是否有返回值(上面列出的所有办法也根本都是依照 是否有返回值 分为两类)。

办法名 形容
runAsync(Runnable runnable) 应用 ForkJoinPool.commonPool()作为它的线程池执行异步代码。
runAsync(Runnable runnable, Executor executor) 应用指定的 thread pool 执行异步代码。
supplyAsync(Supplier<U> supplier) 应用 ForkJoinPool.commonPool()作为它的线程池执行异步代码,异步操作有返回值。
supplyAsync(Supplier<U> supplier, Executor executor) 应用指定的 thread pool 执行异步代码,异步操作有返回值。

上面简直所有的办法都是一式三份,三种办法的区别是

  • 间接在以后线程执行
  • 换另一个线程(然而不指定线程)异步执行
  • 指定线程执行

转换

相当于 map 操作

办法名 形容
thenApply(Function<? super T,? extends U> fn) 承受一个 Function<? super T,? extends U> 参数用来转换 CompletableFuture
thenApplyAsync(Function<? super T,? extends U> fn) 承受一个 Function<? super T,? extends U> 参数用来转换 CompletableFuture,应用 ForkJoinPool
thenApplyAsync(Function<? super T,? extends U> fn, Executor executor) 承受一个 Function<? super T,? extends U> 参数用来转换 CompletableFuture,应用指定的线程池

相当于 flatMap 操作

办法名 形容
thenCompose(Function<? super T, ? extends CompletionStage<U>> fn) 在异步操作实现的时候对异步操作的后果进行一些操作,并且依然返回 CompletableFuture 类型。
thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn) 在异步操作实现的时候对异步操作的后果进行一些操作,并且依然返回 CompletableFuture 类型。应用 ForkJoinPool。
thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn,Executor executor) 在异步操作实现的时候对异步操作的后果进行一些操作,并且依然返回 CompletableFuture 类型。应用指定的线程池。

组合

办法名 形容
thenCombine(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn) 当两个 CompletableFuture 都失常实现后,执行提供的 fn,用它来组合另外一个 CompletableFuture 的后果。
thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn) 当两个 CompletableFuture 都失常实现后,执行提供的 fn,用它来组合另外一个 CompletableFuture 的后果。应用 ForkJoinPool。
thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn, Executor executor) 当两个 CompletableFuture 都失常实现后,执行提供的 fn,用它来组合另外一个 CompletableFuture 的后果。应用指定的线程池。

thenAcceptBoth 跟 thenCombine 相似,然而返回 CompletableFuture 类型。

办法名 形容
thenAcceptBoth(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> action) 当两个 CompletableFuture 都失常实现后,执行提供的 action,用它来组合另外一个 CompletableFuture 的后果。
thenAcceptBothAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> action) 当两个 CompletableFuture 都失常实现后,执行提供的 action,用它来组合另外一个 CompletableFuture 的后果。应用 ForkJoinPool。
thenAcceptBothAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> action, Executor executor) 当两个 CompletableFuture 都失常实现后,执行提供的 action,用它来组合另外一个 CompletableFuture 的后果。应用指定的线程池。

计算结果实现时的解决

  • Action的类型是BiConsumer<? super T,? super Throwable>,它能够解决失常的计算结果,或者异常情况。
  • 办法不以 Async 结尾,意味着 Action 应用雷同的线程执行,而 Async 可能会应用其它的线程去执行(如果应用雷同的线程池,也可能会被同一个线程选中执行
办法名 形容
whenComplete(BiConsumer<? super T,? super Throwable> action) 当 CompletableFuture 实现计算结果时对后果进行解决,或者当 CompletableFuture 产生异样的时候对异样进行解决。
whenCompleteAsync(BiConsumer<? super T,? super Throwable> action) 当 CompletableFuture 实现计算结果时对后果进行解决,或者当 CompletableFuture 产生异样的时候对异样进行解决。应用 ForkJoinPool。
whenCompleteAsync(BiConsumer<? super T,? super Throwable> action, Executor executor) 当 CompletableFuture 实现计算结果时对后果进行解决,或者当 CompletableFuture 产生异样的时候对异样进行解决。应用指定的线程池。

handle()相当于 whenComplete()+ 转换。

handle()也能够了解为和 thenApply() 的含意更为类似,然而比 thenApply() 减少异样解决的性能。

办法名 形容
handle(BiFunction<? super T, Throwable, ? extends U> fn) 当 CompletableFuture 实现计算结果或者抛出异样的时候,执行提供的 fn
handleAsync(BiFunction<? super T, Throwable, ? extends U> fn) 当 CompletableFuture 实现计算结果或者抛出异样的时候,执行提供的 fn,应用 ForkJoinPool。
handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor) 当 CompletableFuture 实现计算结果或者抛出异样的时候,执行提供的 fn,应用指定的线程池。
办法名 形容
exceptionally(Function fn) 只有当 CompletableFuture 抛出异样的时候,才会触发这个 exceptionally 的计算,调用 function 计算值。

纯生产

办法名 形容
thenAccept(Consumer<? super T> action) 当 CompletableFuture 实现计算结果,只对后果执行 Action,而不返回新的计算值
thenAcceptAsync(Consumer<? super T> action) 当 CompletableFuture 实现计算结果,只对后果执行 Action,而不返回新的计算值,应用 ForkJoinPool。
thenAcceptAsync(Consumer<? super T> action, Executor executor) 当 CompletableFuture 实现计算结果,只对后果执行 Action,而不返回新的计算值

Either

Either 示意的是两个 CompletableFuture,当其中任意一个 CompletableFuture 计算实现的时候就会执行。

办法名 形容
acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action) 当任意一个 CompletableFuture 实现的时候,action 这个消费者就会被执行。
acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action) 当任意一个 CompletableFuture 实现的时候,action 这个消费者就会被执行。应用 ForkJoinPool
acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor) 当任意一个 CompletableFuture 实现的时候,action 这个消费者就会被执行。应用指定的线程池

applyToEither()acceptEither() 的哥哥. 当两个 future 其中一个实现后,后者用于只是简略地调用一些代码,applyToEither()会返回一个新的 future. 这个 future 是在后面两个 future 其中一个实现后进行执行实现。

办法名 形容
applyToEither(CompletionStage<? extends T> other, Function<? super T,U> fn) 当任意一个 CompletableFuture 实现的时候,fn 会被执行,它的返回值会当作新的 CompletableFuture<U> 的计算结果。
applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T,U> fn) 当任意一个 CompletableFuture 实现的时候,fn 会被执行,它的返回值会当作新的 CompletableFuture<U> 的计算结果。应用 ForkJoinPool
applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T,U> fn, Executor executor) 当任意一个 CompletableFuture 实现的时候,fn 会被执行,它的返回值会当作新的 CompletableFuture<U> 的计算结果。应用指定的线程池

其余办法

办法名 形容
allOf(CompletableFuture<?>... cfs) 在所有 Future 对象实现后完结,并返回一个 future。
anyOf(CompletableFuture<?>... cfs) 在任何一个 Future 对象完结后完结,并返回一个 future。
正文完
 0