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 异常处理的更多相关文章

  1. 使用CompletableFuture优化你的代码执行效率

    这篇文章详细讲解java8中CompletableFuture的特性,方法以及实例. 在java8以前,我们使用java的多线程编程,一般是通过Runnable中的run方法来完成,这种方式,有个很明 ...

  2. 012-Future、FutureTask、CompletionService 、CompletableFuture

    一.概述 创建线程的两种方式,一种是直接继承Thread,另外一种就是实现Runnable接口.这两种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果.如果需要获取执行结果,就必须通过共享变量或 ...

  3. Java8 异步编排类CompletableFuture

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. https://www.cnblogs.com/shijiaqi1066/p/8758206 ...

  4. Java8 增强的Future:CompletableFuture(笔记)

    CompletableFuture是Java8新增的一个超大型工具类,为什么说她大呢?因为一方面它实现了Future接口,更重要的是,它实现了CompletionStage接口.这个接口也是Java8 ...

  5. 有了 CompletableFuture,使得异步编程没有那么难了!

    本文导读: 业务需求场景介绍 技术设计方案思考 Future 设计模式实战 CompletableFuture 模式实战 CompletableFuture 生产建议 CompletableFutur ...

  6. Java8系列 (七) CompletableFuture异步编程

    概述 Java8之前用 Future 处理异步请求, 当你需要获取任务结果时, 通常的做法是调用  get(long timeout, TimeUnit unit) 此方法会阻塞当前的线程, 如果任务 ...

  7. Java8新特性--CompletableFuture

    并发与并行 Java 5并发库主要关注于异步任务的处理,它采用了这样一种模式,producer线程创建任务并且利用阻塞队列将其传递给任务的consumer.这种模型在Java 7和8中进一步发展,并且 ...

  8. 编程老司机带你玩转 CompletableFuture 异步编程

    本文从实例出发,介绍 CompletableFuture 基本用法.不过讲的再多,不如亲自上手练习一下.所以建议各位小伙伴看完,上机练习一把,快速掌握 CompletableFuture. 个人博文地 ...

  9. 如何编写优雅的异步代码 — CompletableFuture

    前言 在我们的意识里,同步执行的程序都比较符合人们的思维方式,而异步的东西通常都不好处理.在异步计算的情况下,以回调表示的动作往往会分散在代码中,也可能相互嵌套在内部,如果需要处理其中一个步骤中可能发 ...

随机推荐

  1. jenkins插件安装与升级[三]

    标签(linux): jenkins 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 默认的插件 Folders Plugin OWASP Markup Form ...

  2. 2017-12-30-如何彻底清除现存GIT仓库的大量提交历史

    layout: post title: 2017-12-30-如何彻底清除现存GIT仓库的大量提交历史 key: 20171230 tags: GIT 版本管理 问答 modify_date: 201 ...

  3. JS中的算法与数据结构——排序(Sort)(转)

    排序算法(Sort) 引言 我们平时对计算机中存储的数据执行的两种最常见的操作就是排序和查找,对于计算机的排序和查找的研究,自计算机诞生以来就没有停止过.如今又是大数据,云计算的时代,对数据的排序和查 ...

  4. 浅谈最大流的Dinic算法

    PART 1 什么是网络流 网络流(network-flows)是一种类比水流的解决问题方法,与线性规划密切相关.网络流的理论和应用在不断发展,出现了具有增益的流.多终端流.多商品流以及网络流的分解与 ...

  5. 16_Python闭包

    一.什么是闭包 什么是闭包:内函数对外函数非全局变量的引用,并且外函数的返回值是内函数的引用(地址). def wrapper(): name = 'zhangsan' def inner(): na ...

  6. rsync推送和拉取

    rsync格式: # 拷贝本地文件.当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式.如:rsync -a /data /backup rsync [OP ...

  7. 洛谷 [P1801] 黑匣子

    这道题是一道splay裸题,然而身为蒟蒻的我并不会,所以这道题我维护的是一个大根堆与一个小根堆结合起来的类似沙漏的结构. 本题难点在于询问的不是最大最小值,而是第K小值,所以我们想到了维护这样两个堆, ...

  8. ------- 软件调试——注销 QQ 过滤驱动设置的事件通知 CallBack (完)-------

    ---------------------------------------------------------------------------------- 本系列的最后一篇演示如何通过调试手 ...

  9. 📉 Draggable Curve Control (English)

    Conmajia 2012 Updated on Feb. 18, 2018 In Photoshop, there is a very powerful feature called Curve A ...

  10. "abc123 ,def456",反转字母,其他位置不变

    "abc123 ,def456",反转字母,其他位置不变. 无意间看到个有意思的面试题,忽然来了兴趣想着来做一下. 操作字符串用正则的效率比较高,但第一反应还是用原生来操作.下面说 ...