CompletableFuture3
public class CompletableFuture3 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
// testJoin();
testCompletableException();
}
/**
* completeExceptionally可以使得get方法不会阻塞,如果future没有完成,那调用get方法会抛出异常
* @throws ExecutionException
* @throws InterruptedException
*/
public static void testCompletableException() throws ExecutionException, InterruptedException {
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
sleep();
System.out.println("=======");
return "hello";
});
sleep();
future.completeExceptionally(new Exception());
System.out.println(future.get());
}
/**
* 获取结果时,与get的区别是get会抛出检查异常,join不会抛出检查异常
*/
public static void testJoin(){
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
sleep();
System.out.println("=======");
return "hello";
});
String result = future.join();
System.out.println(result);
}
private static void sleep(int sec){
try {
TimeUnit.SECONDS.sleep(sec);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
CompletableFuture3的更多相关文章
随机推荐
- SQL Server内部如何管理对象的数据Page?
一个表或Index使用的数据页空间是由IAM Page Chain来管理的.SQL Server 使用一个IAM(Index Allocation Map)Page来管理数据库文件中最多4GB的空间, ...
- SQL 带有output、inserted、deleted
因需求的关系需要将修改的值返回,故查了些资料发现了OUTPUT这个好东西,现记录下来以防以后忘记 使用例子: 1.对于INSERT,可以引用inserted表以查询新行的属性. insert i ...
- 敏捷软件开发_实例2<四>
敏捷软件开发_实例2 上一章中对薪水支付案例的用例和类做了详细的阐述,在本篇会介绍薪水支付案例包的划分和数据库,UI的设计. 包的划分 一个错误包的划分 为什么这个包是错误的: 如果对classifi ...
- CSS字体属性
CSS字体属性 CSS Fonts(字体)属性拥有定义字体系列.大小.粗细和文字样式(如斜体) 字体系列 <style type="text/css"> div{ fo ...
- opencv::DNN介绍
DNN模块介绍: Tiny-dnn模块 支持深度学习框架 - Caffe - TensorFlow - Torch/PyTorch DNN运用 图像分类 对象检测 实时对象检测 图像分割 预测 视频对 ...
- springcloud微服务实战:Eureka+Zuul+Ribbon+Hystrix+SpringConfig
原文地址:http://blog.csdn.net/yp090416/article/details/78017552 springcloud微服务实战:Eureka+Zuul+Ribbon+Hyst ...
- MySQL 设置表注释
新增表增加注释 CREATE TABLE sys_tables ( owner ) NOT NULL COMMENT '归属用户', table_name ) NOT NULL COMMENT '表名 ...
- js 常用工具方法
1.格式化字符串 String.prototype.format = function () { let args = arguments; return this.replace(/\{(\d+)\ ...
- C# 集合的交集 差集 并集 去重
C# 集合的交集 差集 并集 去重 两个对象list,直接比较是不行的,因为他们存的地址不一样 需要重写GetHashCode()与Equals(object obj)方法告诉电脑 class Stu ...
- [Go] gocron源码阅读-通过第三方cli包实现命令行参数获取和管理
gocron源码中使用的是下面这个第三方包来实现的,下面就单独的拿出来测试以下效果,和官方flag包差不多 go get github.com/urfave/cli package main impo ...