runAsync 和 supplyAsync

runAsync接受一个Runable的实现,无返回值

CompletableFuture.runAsync(()->System.out.println("无返回结果的运行"));

supplyAsync接受一个Supplier的实现,有返回值

CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("有返回结果的运行");
return 1;
});

获取结果的get和join

都是堵塞,直到返回结果

get方法抛出是经过处理的异常,ExecutionException或**InterruptedException **,需要用户手动捕获

try {
System.out.println(CompletableFuture.supplyAsync(() -> {
System.out.println("有返回结果的运行");
return 1;
}).get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}

join方法抛出的就不用捕获,是经过包装的**CompletionException **或 CancellationException

        System.out.println(CompletableFuture.supplyAsync(() -> {
try {
TimeUnit.MILLISECONDS.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("有返回结果的运行");
return 1;
}).join());

常用方法

获取结果的get\join\getNow

get():一直等待

get(timeout,unit):等待,除非超时

getNow(valueIfAbsent):计算完返回计算的结果,未计算完返回默认的结果

CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {

            try {
TimeUnit.SECONDS.sleep(1);
;
} catch (InterruptedException e) {
e.printStackTrace();
}
return 1;
}); System.out.println("立即获取:"+completableFuture.getNow(9999));
try {
TimeUnit.SECONDS.sleep(2);
System.out.println("doing");
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("等一会获取:"+completableFuture.getNow(9999));

join() 同get()

thenApply\handle

执行完前面的,前面返回的结果返回,然后传给后面再,执行完后面任务,一步一步来。

CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("step 1");
return 1;
}).thenApply(a -> {
System.out.println("step 2");
return a + 2;
}).thenApply(a -> {
System.out.println("step 3");
return a + 3;
});
System.out.println(completableFuture.get());

执行结果:

CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("step 1");
int a=1/0;
return 1;
}).handle((a,b) -> {
System.out.println("step 2");
if (b!=null) {
System.out.println(b.getMessage());
return 0;
}
return a + 2;
}).handle((a,b) -> {
System.out.println("step 3");
if (b!=null) {
System.out.println(b.getMessage());
return 0;
}
return a + 3;
});
System.out.println(completableFuture.get());

执行结果:

thenApply和handle的区别:

thenApply执行的时候,有异常的则整个执行链会中断,直接抛出异常。



handle有异常也可以往下一步走,根据带的异常参数可以进一步处理

thenAccept

接收前面任务的返回结果,当前节点处理,并不返回结果。

CompletableFuture.supplyAsync(()->{
System.out.println("step 1");
return 10;
}).thenAccept(a->{
System.out.println("res "+a);
});

applyToEither

在多个任务段同时执行时,哪个任务段用时最少,就返回哪个

CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("step 1");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 1;
}).applyToEither(CompletableFuture.supplyAsync(() -> {
System.out.println("step 2");
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 2;
}), a -> {
return a;
});
System.out.println(completableFuture.get());

执行结果:

thenCombine

合并多个任务段的返回结果

CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("step 1");
return IntStream.range(1, 11).sum();
}).thenCombine(CompletableFuture.supplyAsync(() -> {
System.out.println("step 2");
return IntStream.range(11, 21).sum();
}), (a, b) -> a + b)
.thenCombine(CompletableFuture.supplyAsync(() -> {
System.out.println("step 3");
return IntStream.range(21, 31).sum();
}), (a, b) -> a + b);
System.out.println(completableFuture.get());

最新文章

  1. ajax大全
  2. 一个CURL
  3. 记一次troubleshooting(一):奇慢的脚本
  4. Android异步回调中的UI同步性问题
  5. 在maven项目中解决第三方jar包依赖的问题
  6. 国内市场上 Android 手机屏幕分辨率的比例情况如何?
  7. seajs快速了解
  8. Word Amalgamation(枚举 + 排序)
  9. STM32-AFIO
  10. 经典SQL练习题
  11. 暴力求解——hdu 1799 循环多少次?
  12. Servlet之保存用户偏好设置简单功能的实现
  13. kafka的高可用和一致性探究
  14. HandlerInterceptor里@Autowired对象为空的解决方法
  15. 技术简历这样写,才能得到BAT面试官的青睐
  16. 转载:C++ 二维数组new
  17. Python-集合的常用操作
  18. Python之旅Day1 数据类型初识(数字|字符串|列表|数据运算) 编码 表达式(if...else|for|while)
  19. 大神你好,可以帮我P张图吗?
  20. Android分享到微信时点击分享无反应的问题解决(注意事项)

热门文章

  1. MKL库奇异值分解(LAPACKE_dgesvd)
  2. 攻防世界-MISC:stegano
  3. ucore lab2 物理内存管理 学习笔记
  4. lab_0 清华大学ucore实验环境配置详细步骤!(小白入)
  5. Spring Ioc源码分析系列--前言
  6. 溢出属性,定位,z-index,JS
  7. Python | 内置函数(BIF)
  8. 图文详解 HDFS 的工作机制及其原理
  9. windows 10 21H1 顶部任务栏点击音量或其他图标不出弹框
  10. 992. Sort Array By Parity II - LeetCode