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>。
最新文章
- hadoopfs: 未找到命令...
- 垂直居中display:table;
- PHP文件上传
- IO流-文本IO\读写二进制数据
- Jquery操作下拉框(DropDownList)实现取值赋值
- 20145206邹京儒《Java程序设计》实验报告一:Java开发环境的熟悉(Windows+IDEA)
- JAVA如何随机生成一个汉字
- asp.net子窗体与父窗体交互
- sql server2008添加登录账户配置权限 &;&; 登录时18456错误
- tcmalloc资料
- 把本地建好的项目提交到git上
- size对齐
- Office Add-in 设计规范与最佳实践
- 函数式编程之foldLeftViaFoldRight
- Dell服务器U盘安装Windows Server时识别不到硬盘
- SlidingMenu第二篇 --- SlidingMenu常用属性介绍
- opencv学习之路(38)、Mat像素统计基础——均值,标准差,协方差;特征值,特征向量
- imp 导入以及换用户报错
- Loj 6068. 「2017 山东一轮集训 Day4」棋盘
- shell之使用cut切割文本文件