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
前言 在我们的意识里,同步执行的程序都比较符合人们的思维方式,而异步的东西通常都不好处理.在异步计算的情况下,以回调表示的动作往往会分散在代码中,也可能相互嵌套在内部,如果需要处理其中一个步骤中可能发 ...
随机推荐
- python初识-day3
1.字符串常用操作(较多,用代码加注释表示) name = '\tMy name is congcong' print(name.capitalize())#输出结果为 My name is cong ...
- PHP7.1 报错 Warning Illegal string offset
报错如下: Warning: Illegal string offset '阿根廷' in F:\wnmp\www\test.php on line 24 Warning: Illegal strin ...
- 新awk整理
总感觉上一篇awk的总结几乎是照着man翻译过来的,惨不忍睹 无意间在互联网上有找到了宝贵的资料 感觉整理的很好,想着照着这个来重新写下,对照新的man更新下吧,只是总是在改变的 一.awk简介二.a ...
- awk的+=用法
awk增加统计列值为增加列数或进行运行结果统计,使用符号 + =.增加的结果赋给符号左边变量值,增加到变量的域在符号右边.例如将 $ 1加入变量total,表达式为toatl+=$1.列值增加很有用. ...
- SSH反向连接及Autossh
参考文章 http://www.freeoa.net/osuport/netmanage/autossh-useage-refer_2831.html 接触Linux恐怕对SSH再熟悉不过了,还有sc ...
- Linux时间转标准时间
[root@nhserver2 ~]# date -d '1970-1-1 0:0:0 GMT + 1394592071 seconds'Wed Mar 12 10:41:11 CST 2014
- thinkpadE470 win10安装虚拟机时出现‘intel VT-x’处于禁用状态 无法执行64位操作系统
上图为安装虚拟机时弹出的问题. 解决办法: 本人电脑型号为thinkpadE470.遇到此问题查阅了很多,都是针对一般电脑的.折腾了好一会儿.故特写一篇针对此型号的. 1.重启计算机,一开始出现联想标 ...
- SpringMVC源码情操陶冶-AbstractHandlerMethodMapping
承接前文SpringMVC源码情操陶冶-AbstractHandlerMapping,本文将介绍如何注册HandlerMethod对象作为handler 类结构瞧一瞧 public abstract ...
- MySQL系统临时表、用户临时表
MySQL临时表分为系统使用的临时表和用户使用的临时表. 系统使用的临时表是指MySQL在执行某些SQL语句时需要依赖临时表来完成整个过程.系统使用的临时表的情况可以分为以下几种: * group ...
- Python基础篇(二)
Python最基本的数据结构是序列(sequence),序列中的每个元素被分以以0开头的唯一的一个id号. Python中有6种内建的序列:列表,元组,字符串,Unicode字符串,buffer对象和 ...