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
前言 在我们的意识里,同步执行的程序都比较符合人们的思维方式,而异步的东西通常都不好处理.在异步计算的情况下,以回调表示的动作往往会分散在代码中,也可能相互嵌套在内部,如果需要处理其中一个步骤中可能发 ...
随机推荐
- 04_Javascript初步第三天
事件 内联模型.脚本模型,DOM2级模型 <!--内联模型--> <input type="button" value="bt1" oncli ...
- Memcached原理与应用
Memcached原理与应用 标签: linux 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 1.Memcached是什么 高性能 支持高并发 分布式内存缓存 ...
- dotween tips
涉及kill及复用的行为比较奇怪. 使用shortcut方式调用dotween时,每次调用都是增加一个新的tweener,如果该tweener控制的属性与上次调用相同时,会出现奇怪的行为,应该是多个t ...
- CentOS 7 yum 安装 MySQL5.7
1.下载 MySQL 官方的 Yum Repository ,官网地址:https://dev.mysql.com/downloads/repo/yum/ 从 MySQL 官网选取合适的 MySQL ...
- 面向对象_04【关键字:super使用】
super关键字:解决当子类重写父类对象方法后,子类对象无法访问父类的成员1,调用父类成员变量和方法 super.成员变量 super.成员方法([参数1,参数2.......])Example: / ...
- lvs+keepalive构建高可用集群
大纲 一.前言 二.Keepalived 详解 三.环境准备 四.LVS+Keepalived 实现高可用的前端负载均衡器 一.前言 Keepalived使用的vrrp协议方式,虚拟路由 ...
- Python之数据结构基础
一.数据结构基础 a.什么是数据结构 b.数据结构的分类 c.列表 import random from timewrap import ...
- 《.NET 设计规范》第 9 章:常用的设计模式
第 9 章:常用的设计模式 9.1 聚合组件 考虑为常用的特性域提供聚合组件. 要用聚合组件来对高层的概念(物理对象)进行建模,而不是对系统级的任务进行建模. 要让聚合组件的名字与众所周知的系统实体相 ...
- CodeChef Chef and Churu [分块]
题意: 单点修改$a$ 询问$a$的区间和$f$的区间和 原来普通计算机是这道题改编的吧... 对$f$分块,预处理$c[i][j]$为块i中$a_j$出现几次,$O(NH(N))$,只要每个块差分加 ...
- 【转】 C/C++程序员必须熟练应用的开源项目
作为一个经验丰富的C/C++程序员, 肯定亲手写过各种功能的代码, 比如封装过数据库访问的类, 封装过网络通信的类,封装过日志操作的类, 封装过文件访问的类, 封装过UI界面库等, 也在实际的项目中应 ...