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的更多相关文章
随机推荐
- STN(Spatial Transformer Networks)
url: https://arxiv.org/abs/1506.02025 year:2015 blog: https://kevinzakka.github.io/2017/01/10/stn-pa ...
- nginx-配置文件样例
1, 总配置文件 user nobody nobody; worker_processes ; worker_rlimit_nofile ; error_log /etc/nginx-idfa/log ...
- sql server相邻表记录交换(单双两两交换)
在博客园的博问中看到了一个这样的提问:一个表中有id和name两个字段,id是连续非空不重复的,想要交换相邻表记录的name值(单双两两交换). 另外,如果最后的单独行没有对应的下一行匹配记录的话,就 ...
- C#数组1
using System; namespace ConsoleApp3 { class Program { static void Main(string[] args) { , , , , , }; ...
- Wpf,Unity6
<?xml version="1.0" encoding="utf-8"?><packages> <package id=&quo ...
- Four Ways to Read Configuration Setting in C#(COPY)
Introduction This article will demonstrate us how we can get/read the configuration setting from Web ...
- 使用Node.js简单创建一个服务器
首先,我们要了解Node.js不是一种语言,它只是一个除了浏览器之外的,可以运行js的环境. 其次,Node能做些什么 ? web服务器. 命令行工具. 网络爬虫. 桌面应用程序开发等 3.接下 ...
- Java生鲜电商平台-App系统架构开发与设计
Java生鲜电商平台-App系统架构开发与设计 说明:阅读此文,你可以学习到以下的技术分享 1.Java生鲜电商平台-App架构设计经验谈:接口的设计2.Java生鲜电商平台-App架构设计经验谈:技 ...
- centOS服务器添加电脑ssh key以支持远程登陆
1,生成电脑的密钥对(在powershell或cmd命令行中) ssh-keygen -t rsa -C "自己的邮箱" 2.打开刚刚生成的电脑公钥(~即代表用户主目录,/则代表根 ...
- CSS3 3D变形 transform---rotateX(), rotateY(), rotateZ(), 透视(perspective)
2d x y 3d x y z 左手坐标系 伸出左手,让拇指和食指成“L”形,大拇指向右,食指向上,中指指向前方.这样我们就建立了一个左手坐标系,拇指.食指和中指分别代表X.Y.Z轴的正方向.如下图 ...