CompletableFuture CompletableFuture.supplyAsync 异常处理
- CompletableFuture 异常处理completeExceptionally可以把异常抛到主线程
- /**
- * User: laizhenwei
- * Date: 2018-01-30 Time: 22:26
- * Description:
- */
- @RunWith(SpringRunner.class)
- //@SpringBootTest
- public class CompletableFutureTests {
- @Test
- public void testMethod() {
- String[] orders = {"1", "2", "3", "4", "5", "6"};
- List<CompletableFuture<Boolean>> futures = new ArrayList<>();
- Arrays.stream(orders).forEach(id -> {
- try{
- futures.add(submitAsync(id));
- }catch (Exception ex){
- System.out.println(ex);
- }
- });
- futures.stream().forEach(f-> {
- try {
- System.out.println(f.get());
- } catch (Exception e) {
- e.printStackTrace();
- }
- });
- }
- private static Boolean submit(String order) {
- try {
- TimeUnit.SECONDS.sleep(1);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- throw new RuntimeException("抛一个异常" + order);
- }
- private static CompletableFuture<Boolean> submitAsync(String order) {
- CompletableFuture<Boolean> future = new CompletableFuture<>();
- new Thread(() -> {
- try {
- Boolean result = submit(order);
- future.complete(result);
- } catch (Exception ex) {
- future.completeExceptionally(ex);
- }
- }).start();
- return future;
- }
- }
使用 CompletableFuture.supplyAsync 简化代码 加入线程池,exceptionally处理异常
- /**
- * User: laizhenwei
- * Date: 2018-01-30 Time: 22:26
- * Description:
- */
- @RunWith(SpringRunner.class)
- //@SpringBootTest
- public class CompletableFutureTests {
- ExecutorService executor = Executors.newFixedThreadPool(3);
- @Test
- public void testMethod() {
- String[] orders = {"1", "2", "3", "4", "5", "6"};
- Arrays.stream(orders).forEach(id -> CompletableFuture.supplyAsync(() -> submit(id), executor).exceptionally(e -> {
- System.out.println(e);
- return false;
- }));
- executor.shutdown();
- while (!executor.isTerminated()) {
- try {
- TimeUnit.MILLISECONDS.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- private static Boolean submit(String order) {
- try {
- TimeUnit.SECONDS.sleep(1);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- throw new RuntimeException("抛一个异常" + order);
- }
- }
CompletableFuture CompletableFuture.supplyAsync 异常处理的更多相关文章
- 使用CompletableFuture优化你的代码执行效率
这篇文章详细讲解java8中CompletableFuture的特性,方法以及实例. 在java8以前,我们使用java的多线程编程,一般是通过Runnable中的run方法来完成,这种方式,有个很明 ...
- 012-Future、FutureTask、CompletionService 、CompletableFuture
一.概述 创建线程的两种方式,一种是直接继承Thread,另外一种就是实现Runnable接口.这两种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果.如果需要获取执行结果,就必须通过共享变量或 ...
- Java8 异步编排类CompletableFuture
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. https://www.cnblogs.com/shijiaqi1066/p/8758206 ...
- Java8 增强的Future:CompletableFuture(笔记)
CompletableFuture是Java8新增的一个超大型工具类,为什么说她大呢?因为一方面它实现了Future接口,更重要的是,它实现了CompletionStage接口.这个接口也是Java8 ...
- 有了 CompletableFuture,使得异步编程没有那么难了!
本文导读: 业务需求场景介绍 技术设计方案思考 Future 设计模式实战 CompletableFuture 模式实战 CompletableFuture 生产建议 CompletableFutur ...
- Java8系列 (七) CompletableFuture异步编程
概述 Java8之前用 Future 处理异步请求, 当你需要获取任务结果时, 通常的做法是调用 get(long timeout, TimeUnit unit) 此方法会阻塞当前的线程, 如果任务 ...
- Java8新特性--CompletableFuture
并发与并行 Java 5并发库主要关注于异步任务的处理,它采用了这样一种模式,producer线程创建任务并且利用阻塞队列将其传递给任务的consumer.这种模型在Java 7和8中进一步发展,并且 ...
- 编程老司机带你玩转 CompletableFuture 异步编程
本文从实例出发,介绍 CompletableFuture 基本用法.不过讲的再多,不如亲自上手练习一下.所以建议各位小伙伴看完,上机练习一把,快速掌握 CompletableFuture. 个人博文地 ...
- 如何编写优雅的异步代码 — CompletableFuture
前言 在我们的意识里,同步执行的程序都比较符合人们的思维方式,而异步的东西通常都不好处理.在异步计算的情况下,以回调表示的动作往往会分散在代码中,也可能相互嵌套在内部,如果需要处理其中一个步骤中可能发 ...
随机推荐
- jsp小结
JSP执行步骤 完整步骤: 第一步:用户通过浏览器发出一个Http请求: 第二步:web服务器识别是对jsp页面的请求: 第三步:jsp容器通过jsp引擎将jsp页面转化为servlet代码(纯ja ...
- 【转】Vim使用笔记
http://www.cnblogs.com/jiqingwu/archive/2012/06/14/vim_notes.html 曾经使用了两年多的Vim,手册也翻过一遍.虽然现在不怎么用vim了, ...
- spring boot热部署
1.pom配置 参考:http://412887952-qq-com.iteye.com/blog/2300313 2.intellij配置 参考:http://blog.csdn.net/wjc47 ...
- 面向对象_05【类的继承:extends、重写父类】
类的继承:现有类的基础上构建一个新的类,构建出来的类被称作子类,子类可继承父类的属性和方法. 什么时候定义继承?当类与类之间存在着所属关系的时候,就定义继承.xxx是yyy中的一种==>xxx ...
- web.config文件中配置数据库连接的两种方式
web.config文件中配置数据库连接的两种方式 标签: 数据库webconfig 2015-04-28 18:18 31590人阅读 评论(1)收藏举报 分类: 数据库(74) 在网站开发 ...
- gulp压缩文件最简示例
安装gulp-uglify 作为项目的开发依赖即可 $ npm gulp-uglify --save-dev 压缩js文件 gulpfile.js const gulp = require('gulp ...
- dl在不同浏览器下显示不同
dl在chrome浏览器和在火狐浏览器下的默认样式是不一样的,解决方法将dl换成ol或ul样式就正常了.
- 初识Python装饰器
python中,一切皆对象.做为面向对象开发中非常重要的一个环节,函数有着无可替代的作用. 函数可以作为对象赋值给一个变量,可以作为元素添加到集合对象中,可以作为参数值传递给其它函数,还可以当做函数的 ...
- 安装pcntl以实现php多进程
pcntl 扩展包一般就在php源码的ext目录下. cd ./ext/pcntl /opt/server/php5/bin/phpize ./configure \ --with-php-confi ...
- JMeter生成HTML性能报告
有时候我们写性能报告的时候需要一些性能分布图,JMeter是可以生成HTML性能报告的 一.准备工作 1:jmeter3.0版本之后开始支持动态生成测试报表 2:jdk版本1.7以上 3:需要jmx脚 ...