CompletableFuture2
2024-10-19 00:43:06
public class CompletableFuture2 { public static void main(String[] args) throws InterruptedException {
// thenAcceptBoth();
// acceptEither();
// runAfterBoth();
// runAfterEither();
// combine();
compose();
Thread.currentThread().join();
} /**
* 前一个CompletableFuture执行完后的结果,作为参数传递给后面的Function
*/
private static void compose(){
CompletableFuture.supplyAsync(() -> {
System.out.println("start the compose1");
sleep();
System.out.println("end the compose1");
return "hello1";
}).thenCompose((s) -> CompletableFuture.supplyAsync(() -> {
System.out.println("start the compose2");
sleep();
System.out.println("end the compose2");
return s.length();
})).thenAccept(System.out::println); } /**
* 两个都执行完后,再执行后面的BiFunction,BiFunction是有两个参数一个返回值的
*/
private static void combine(){
CompletableFuture.supplyAsync(() -> {
System.out.println("start the combine1");
sleep();
System.out.println("end the combine1");
return "hello1";
}).thenCombine(CompletableFuture.supplyAsync(() -> {
System.out.println("start the combine2");
sleep();
System.out.println("end the combine2");
return ;
}), (s, i) -> s.length() > i
).whenComplete((v, t) -> {
System.out.println(v);
}); } /**
* 其中有任何一个执行完后,执行后面的runnable
*/
private static void runAfterEither(){
CompletableFuture.supplyAsync(() -> {
System.out.println("start the runAfterEither1");
sleep();
System.out.println("end the runAfterEither1");
return "hello1";
}).runAfterEither(CompletableFuture.supplyAsync(() -> {
System.out.println("start the runAfterEither2");
sleep();
System.out.println("end the runAfterEither2");
return "hello1";
}),()-> System.out.println("======"));
} /**
* 两个都执行完后,执行后面的runnable
*/
private static void runAfterBoth(){
CompletableFuture.supplyAsync(() -> {
System.out.println("start the runAfterBoth1");
sleep();
System.out.println("end the runAfterBoth1");
return "hello1";
}).runAfterBoth(CompletableFuture.supplyAsync(() -> {
System.out.println("start the runAfterBoth2");
sleep();
System.out.println("end the runAfterBoth2");
return "hello1";
}), () -> System.out.println("=====")
);
} /**
* 其中有任何一个执行完后,执行后面的consumer
*/
private static void acceptEither(){
CompletableFuture.supplyAsync(() -> {
System.out.println("start the acceptEither1");
sleep();
System.out.println("end the acceptEither1");
return "hello1";
}).acceptEither(CompletableFuture.supplyAsync(() -> {
System.out.println("start the acceptEither2");
sleep();
System.out.println("end the acceptEither2");
return "hello2";
}), (s) -> {
System.out.println(s);
}); } /**
* thenAcceptBoth 两个都执行完后,执行后面的BiConsumer,BiConsumer两个参数,无返回值
*/
private static void thenAcceptBoth(){
CompletableFuture.supplyAsync(()->{
System.out.println("start the supplyAsync");
sleep();
System.out.println("end the supplyAsync");
return "hello";
}).thenAcceptBoth(CompletableFuture.supplyAsync(()->{
System.out.println("start the thenAcceptBoth");
sleep();
System.out.println("end the thenAcceptBoth");
return ;
}),(s,i)->{
System.out.println(s+" ===" + i);
});
} private static void sleep(int sec){
try {
TimeUnit.SECONDS.sleep(sec);
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
最新文章
- 搭建LNAMP环境(二)- 源码安装Nginx1.10
- SCOI2005栅栏
- 也说面试 - 一个努力的iOS Dev
- 线性表基本维护[ACM]
- ios获取摄像头与相册图片
- VIM树状文件列表NERDTree
- String.IsNullOrWhiteSpace和String.IsNullOrEmpty的区别
- FFMPEG视音频编解码零基础学习方法-b
- Linux GCC
- attr与prop的区别
- 深入浅出Java动态代理
- 最优化方法:范数和规则化regularization
- W3CSchool闯关笔记(中级脚本算法)
- ASP.net core 使用UEditor.Core 实现 ueditor 上传功能
- 5、继承(extends)
- C# 之 向服务器上传资源
- Vue自学
- css选择器querySelector
- 内核中container_of宏的详细分析【转】
- 洛谷P3248 [HNOI2016]树(主席树 倍增 )
热门文章
- PyCharm 2017: Remote debugging using remote interpreter doesn't work
- CAT 监控搭建
- 【BZOJ1921】【CTSC2010】珠宝商(点分治,后缀自动机)
- 2018-12-25-win10-uwp-通知列表
- Java生鲜电商平台-Java后端生成Token架构与设计详解
- Vue入门、插件安装
- Python之dict(或对象)与json之间转化
- mac安装linux
- 中文代码之Django官方入门:建立模型
- [20191012]组成rowid.txt