这一组办法接收一个Function作为参数,这个Function的输入是当前的CompletableFuture的计算值,返回结不雅将是一个新的CompletableFuture,这个新的CompletableFuture会组合本来的CompletableFuture和函数返回的CompletableFuture。是以它的功能类似:
A +--> B +---> C
记住,thenCompose返回的对象并不一是函数fn返回的对象,如不雅本来的CompletableFuture还没有计算出来,它就会生成一个新的组合后的CompletableFuture。
例子:
- CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
- return 100;
- });
- CompletableFuture<String> f = future.thenCompose( i -> {
- return CompletableFuture.supplyAsync(() -> {
- return (i * 10) + "";
- });
- });
- System.out.println(f.get()); //1000
而下面的一组办法thenCombine用来复合别的一个CompletionStage的结不雅。它的功能类似:
A +
|
+------> C
+------^
B +
两个CompletionStage是并行履行的,它们之间并没有先后依附次序,other并不会等待先前的CompletableFuture履行完毕后再履行。
更进一步
- public <U,V> CompletableFuture<V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn)
- public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn)
- public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn, Executor executor)
其实大年夜功能上来讲,它们的功能更类似thenAcceptBoth,只不过thenAcceptBoth是纯花费,它的函数参数没有返回值,而thenCombine的函数参数fn有返回值。
- CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
- return 100;
- });
- CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
- return "abc";
- });
推荐阅读
【限时免费】岁尾最强一次云计算大年夜会,看传统、社区、互联网企业若何碰撞?【编辑推荐】2017 Android和iOS几回再三被曝Bug,它们都怎么了?谷歌苹不雅竟齐翻车!谈谈安卓8/iOS11那些BugiOS消息转发>>>详细阅读
地址:http://www.17bianji.com/lsqh/40058.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示