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的更多相关文章

随机推荐

  1. 自己实现LinkedList

    public class MyLinkedList<E> { private Node first; private int size; public int size(){ return ...

  2. Docker第二弹之常用命令

    Docker的常用命令 底层原理 Docker是如何工作的 Docker是一个Client-Server结构的系统,Docker守护进程运行在主机上, 然后通过Socket连接从客户端访问,守护进程从 ...

  3. node.js如何批量赋值

    1. 数组解析赋值 let a = 1; let b = 2; let c = 3; 等同于 let [a, b, c] = [1, 2, 3]; 默认值 let [a, b = "B&qu ...

  4. 关于excel中的vlookup就是查找当前列对应的下一列的值的使用

    关于excel中的vlookup就是查找当前列对应的下一列的值的使用 vlookup的使用一些说明 vlookup函数一个4个参数解释下 vlookup(查找的值,表格范围,表格范围中第几列的值,0是 ...

  5. C#上手练习2(FOR语句)

    循环语句和条件语句一样都是每个程序中必不可少的,循环语句是用来完成一些重复的工作的,以减少编写代码的工作量. C# for 循环是最常用的循环语句,语法形式非常简单,多用于固定次数的循环. 具体的语法 ...

  6. paypal开发指南

    一.开发者地址: https://developer.paypal.com 使用在paypal上注册的账号登陆即可, 二.沙箱账号 paypay自动会为你创建两个沙箱账号,一个商家,一个买家.在acc ...

  7. [20190515]热备份模式与rman冲突.txt

    [20190515]热备份模式与rman冲突.txt --//别人的系统做dg时打开热备份模式,忘记关闭,做rman备份时报错.做一个记录.--//实际上也怪自己,实施时没有讲清楚.通过例子说明: 1 ...

  8. 最短时间(几秒内)利用C#往SQLserver数据库一次性插入10万条数据

    用途说明: 公司要求做一个数据导入程序,要求将Excel数据,大批量的导入到数据库中,尽量少的访问数据库,高性能的对数据库进行存储.于是在网上进行查找,发现了一个比较好的解决方案,就是采用SqlBul ...

  9. nginx是怎么处理http请求的

    nginx是怎么处理http请求的 参考:How nginx processes a request nginx first decides which server should process t ...

  10. linux修改权限

    修改system目录(包括子目录)的所有者和组 sudo chown -R seven:seven system