CompletableFuture的thenCompose使用具体说明
1.thenCompose
如果你还没有了解CompletableFuture或者希望再次熟悉一下,可以参考 CompletableFuture使用方法详细说明
1.1. thenCompose的特点
thenCompose方法会在某个任务执行完成后,将该任务的执行结果作为方法入参然后执行指定的方法,该方法会返回一个新的CompletableFuture实例。
也就是对一个CompletableFuture返回的结果进行后续操作,返回一个新的CompletableFuture。
1.2.thenCompose的定义
public <U> CompletableFuture<U> thenCompose(
Function<? super T, ? extends CompletionStage<U>> fn) {
return uniComposeStage(null, fn);
}
public <U> CompletableFuture<U> thenComposeAsync(
Function<? super T, ? extends CompletionStage<U>> fn) {
return uniComposeStage(asyncPool, fn);
}
public <U> CompletableFuture<U> thenComposeAsync(
Function<? super T, ? extends CompletionStage<U>> fn,
Executor executor) {
return uniComposeStage(screenExecutor(executor), fn);
}
可以看到方法的返回值CompletionStage<U>,重点就是需要理解它们的传入参数fn。接下来和thenApply进行对比说明。
2.thenApply与thenCompose的异同
thenApply和thenCompose都是对一个CompletableFuture返回的结果进行后续操作,返回一个新的CompletableFuture。
不同的是两个方法的参数不同,上面我们已经看到了thenCompose的方法定义,下面我们看看thenApply的方法定义
public <U> CompletableFuture<U> thenApply(
Function<? super T,? extends U> fn) {
return uniApplyStage(null, fn);
}
public <U> CompletableFuture<U> thenApplyAsync(
Function<? super T,? extends U> fn) {
return uniApplyStage(asyncPool, fn);
}
public <U> CompletableFuture<U> thenApplyAsync(
Function<? super T,? extends U> fn, Executor executor) {
return uniApplyStage(screenExecutor(executor), fn);
}
很明显,和thenCompose方法对比,两个方法的返回值都是CompletionStage,不同之处在于它们的传入参数。
- thenApply:fn函数是一个对一个已完成的stage或者说CompletableFuture的的返回值进行计算、操作,也就是说转换的是泛型中的类型,相当于将CompletableFuture 转换生成新的CompletableFuture
- thenCompose:fn函数是对另一个CompletableFuture进行计算、操作,也就是说用来连接两个CompletableFuture,是生成一个新的CompletableFuture。
上面的理解吗?大白话说就是thenApply从 CompletableFuture<T> 生成新的CompletableFuture<U>,只是将CompletableFuture的T类型转换为了U类型而已,CompletableFuture还是同一个CompletableFuture。
而thenCompose是生成了一个新的CompletableFuture。
3.示例
上面只是进行了说明,接下里进行代码示例配合说明,更容易理解
thenApply示例:
@Test
public void test() throws ExecutionException, InterruptedException {
/*
如果supplyAsync直接返回, 得到CompletableFuture<String>
现在经过了thenApply的处理, CompletableFuture<String> 转换为 CompletableFuture<Integer>, CompletableFuture是同一个
*/
final CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
// 先执行supplyAsync方法,得到返回值
return "hello";
}).thenApply(value -> {
// 接收到supplyAsync的返回值“hello”
if ("hello".equals(value)) {
return 111;
} else {
return 000;
}
});
final Integer integer = completableFuture.get();
System.out.println("结果:" + integer);
}
执行结果:
结果:111
thenCompose示例:
@Test
public void test() throws ExecutionException, InterruptedException {
//
final CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
// 先执行supplyAsync方法,得到返回值
return "hello";
}).thenCompose(value -> CompletableFuture.supplyAsync(() -> {
// thenCompose方法返回一个新的CompletableFuture
if ("hello".equals(value)) {
return 111;
} else {
return 000;
}
}));
final Integer integer = completableFuture.get();
System.out.println("结果:" + integer);
}
执行结果:
结果:111
总结:thApply和thenCompose都是将一个CompletableFuture<String>转换为CompletableFuture<Integer>。
不同的是,thenApply中的传入函数的返回值是String,而thenCompose的传入函数的返回值是CompletableFuture<Integer>。
CompletableFuture的thenCompose使用具体说明的更多相关文章
- Java CompletableFuture 详解
Future是Java 5添加的类,用来描述一个异步计算的结果.你可以使用isDone方法检查计算是否完成,或者使用get阻塞住调用线程,直到计算完成返回结果,你也可以使用cancel方法停止任务的执 ...
- 使用CompletableFuture优化你的代码执行效率
这篇文章详细讲解java8中CompletableFuture的特性,方法以及实例. 在java8以前,我们使用java的多线程编程,一般是通过Runnable中的run方法来完成,这种方式,有个很明 ...
- 从CompletableFuture到异步编程设计
从CompletableFuture到异步编程设计,笔者就分为2部分来分享CompletableFuture异步编程设计,前半部分总结下CompletableFuture使用实践,后半部分分享下Com ...
- Java8 异步编排类CompletableFuture
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. https://www.cnblogs.com/shijiaqi1066/p/8758206 ...
- Java8 增强的Future:CompletableFuture(笔记)
CompletableFuture是Java8新增的一个超大型工具类,为什么说她大呢?因为一方面它实现了Future接口,更重要的是,它实现了CompletionStage接口.这个接口也是Java8 ...
- Java8 CompletableFuture 编程
一.简介 所谓异步调用其实就是实现一个无需等待被调用函数的返回值而让操作继续运行的方法.在 Java 语言中,简单的讲就是另启一个线程来完成调用中的部分计算,使调用继续运行或返回,而不需要等待计算结 ...
- 搞定 CompletableFuture,并发异步编程和编写串行程序还有什么区别?你们要的多图长文
你有一个思想,我有一个思想,我们交换后,一个人就有两个思想 If you can NOT explain it simply, you do NOT understand it well enough ...
- 异步技巧之CompletableFuture
摘自--https://juejin.im/post/5b4622df5188251ac9766f47 异步技巧之CompletableFuture 1.Future接口 1.1 什么是Future? ...
- 🏆【Java技术专区】「并发编程专题」教你如何使用异步神器CompletableFuture
前提概要 在java8以前,我们使用java的多线程编程,一般是通过Runnable中的run方法来完成,这种方式,有个很明显的缺点,就是,没有返回值.这时候,大家可能会去尝试使用Callable中的 ...
- CompletableFuture 使用总结
转载请注明出处: 1.Future使用对比 Future表示一个异步计算的结果.它提供了isDone()来检测计算是否已经完成,并且在计算结束后,可以通过get()方法来获取计算结果.在异步计算中,F ...
随机推荐
- (数据科学学习手札149)用matplotlib轻松绘制漂亮的表格
本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 大家好我是费老师,matplotlib作为数据可 ...
- 华为云MRS支持lakeformation能力,打造一站式湖仓,释放数据价值
摘要:对云端用户而言,业务价值发现是最重要的,华为MRS支持LakeFormation后,成功降低了数据应用的成本,帮助客户落地"存"与"算"的管理,加快推进了 ...
- 【安全】漏洞复现及修复——CVE-2023-24055 keepass触发器注入获取明文密码
write by homelander 如要转载请注明出处谢谢:https://www.cnblogs.com/vitalemontea/p/17104168.html 1.前言 [仅供学习使用,切勿 ...
- nginx微信对接
location /MP_verify_l47ZUDtvieeDlVWR.txt { default_type text/html; return 200 'l47ZUDtvieeDlVWR'; }
- Vue 07 js方法Object.defineProperty
1 描述 该方法允许精确地添加或修改对象的属性.可以对已有的属性进行获取及修改,也可以添加新的属性并进行操作. 2 方法参数 Object.defineProperty(操作的对象,添加的属性的名称, ...
- react 高效高质量搭建后台系统 系列 —— 前端权限
其他章节请看: react 高效高质量搭建后台系统 系列 权限 本系列已近尾声,权限是后台系统必不可少的一部分,本篇首先分析spug项目中权限的实现,最后在将权限加入到我们的项目中来. spug 中权 ...
- ft5426触摸屏I2C
触摸的点数, 先写入地址0x38, 寄存器0x02, 再次读取0x38的数据,得到1个触摸点 读取全部坐标信息,需要读入30字节数据
- JZOJ 3234. 阴阳
阴阳 题面 分析 个人认为是极好的题,很容易写 如果你学点分治是无奈背板的,那就做做这道题,加深你对点分治的理解 一般的,处理树上大规模统计问题,我们分治的关键是找一棵子树的重心 找到分治中心,即新一 ...
- LOJ.数列分块入门3
题目 分析 由大题目知此题分块 注意处理前驱下标的合法性 \(Code\) #include<cstdio> #include<cmath> #include<algor ...
- [BSR文摘] 如何解释CRP正常而多普勒超声显示关节炎活动的RA亚型
如何解释CRP正常而多普勒超声显示关节炎活动的RA亚型 Braford CM, et al.Rheumatology 2016. Present ID: 72. 背景:临床门诊越来越多地利用肌肉骨骼超 ...