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();
}
} }

最新文章

  1. 搭建LNAMP环境(二)- 源码安装Nginx1.10
  2. SCOI2005栅栏
  3. 也说面试 - 一个努力的iOS Dev
  4. 线性表基本维护[ACM]
  5. ios获取摄像头与相册图片
  6. VIM树状文件列表NERDTree
  7. String.IsNullOrWhiteSpace和String.IsNullOrEmpty的区别
  8. FFMPEG视音频编解码零基础学习方法-b
  9. Linux GCC
  10. attr与prop的区别
  11. 深入浅出Java动态代理
  12. 最优化方法:范数和规则化regularization
  13. W3CSchool闯关笔记(中级脚本算法)
  14. ASP.net core 使用UEditor.Core 实现 ueditor 上传功能
  15. 5、继承(extends)
  16. C# 之 向服务器上传资源
  17. Vue自学
  18. css选择器querySelector
  19. 内核中container_of宏的详细分析【转】
  20. 洛谷P3248 [HNOI2016]树(主席树 倍增 )

热门文章

  1. PyCharm 2017: Remote debugging using remote interpreter doesn't work
  2. CAT 监控搭建
  3. 【BZOJ1921】【CTSC2010】珠宝商(点分治,后缀自动机)
  4. 2018-12-25-win10-uwp-通知列表
  5. Java生鲜电商平台-Java后端生成Token架构与设计详解
  6. Vue入门、插件安装
  7. Python之dict(或对象)与json之间转化
  8. mac安装linux
  9. 中文代码之Django官方入门:建立模型
  10. [20191012]组成rowid.txt