runAsync 和 supplyAsync

runAsync接受一个Runable的实现,无返回值

CompletableFuture.runAsync(()->System.out.println("无返回结果的运行"));

supplyAsync接受一个Supplier的实现,有返回值

CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("有返回结果的运行");
return 1;
});

获取结果的get和join

都是堵塞,直到返回结果

get方法抛出是经过处理的异常,ExecutionException或**InterruptedException **,需要用户手动捕获

try {
System.out.println(CompletableFuture.supplyAsync(() -> {
System.out.println("有返回结果的运行");
return 1;
}).get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}

join方法抛出的就不用捕获,是经过包装的**CompletionException **或 CancellationException

        System.out.println(CompletableFuture.supplyAsync(() -> {
try {
TimeUnit.MILLISECONDS.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("有返回结果的运行");
return 1;
}).join());

常用方法

获取结果的get\join\getNow

get():一直等待

get(timeout,unit):等待,除非超时

getNow(valueIfAbsent):计算完返回计算的结果,未计算完返回默认的结果

CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {

            try {
TimeUnit.SECONDS.sleep(1);
;
} catch (InterruptedException e) {
e.printStackTrace();
}
return 1;
}); System.out.println("立即获取:"+completableFuture.getNow(9999));
try {
TimeUnit.SECONDS.sleep(2);
System.out.println("doing");
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("等一会获取:"+completableFuture.getNow(9999));

join() 同get()

thenApply\handle

执行完前面的,前面返回的结果返回,然后传给后面再,执行完后面任务,一步一步来。

CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("step 1");
return 1;
}).thenApply(a -> {
System.out.println("step 2");
return a + 2;
}).thenApply(a -> {
System.out.println("step 3");
return a + 3;
});
System.out.println(completableFuture.get());

执行结果:

CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("step 1");
int a=1/0;
return 1;
}).handle((a,b) -> {
System.out.println("step 2");
if (b!=null) {
System.out.println(b.getMessage());
return 0;
}
return a + 2;
}).handle((a,b) -> {
System.out.println("step 3");
if (b!=null) {
System.out.println(b.getMessage());
return 0;
}
return a + 3;
});
System.out.println(completableFuture.get());

执行结果:

thenApply和handle的区别:

thenApply执行的时候,有异常的则整个执行链会中断,直接抛出异常。



handle有异常也可以往下一步走,根据带的异常参数可以进一步处理

thenAccept

接收前面任务的返回结果,当前节点处理,并不返回结果。

CompletableFuture.supplyAsync(()->{
System.out.println("step 1");
return 10;
}).thenAccept(a->{
System.out.println("res "+a);
});

applyToEither

在多个任务段同时执行时,哪个任务段用时最少,就返回哪个

CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("step 1");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 1;
}).applyToEither(CompletableFuture.supplyAsync(() -> {
System.out.println("step 2");
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 2;
}), a -> {
return a;
});
System.out.println(completableFuture.get());

执行结果:

thenCombine

合并多个任务段的返回结果

CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("step 1");
return IntStream.range(1, 11).sum();
}).thenCombine(CompletableFuture.supplyAsync(() -> {
System.out.println("step 2");
return IntStream.range(11, 21).sum();
}), (a, b) -> a + b)
.thenCombine(CompletableFuture.supplyAsync(() -> {
System.out.println("step 3");
return IntStream.range(21, 31).sum();
}), (a, b) -> a + b);
System.out.println(completableFuture.get());

CompletableFuture的入门的更多相关文章

  1. CompletableFuture用法介绍

    一.CompletableFuture用法入门介绍 入门介绍的一个例子: package com.cy.java8; import java.util.Random; import java.util ...

  2. 一条数据的HBase之旅,简明HBase入门教程-Write全流程

    如果将上篇内容理解为一个冗长的"铺垫",那么,从本文开始,剧情才开始正式展开.本文基于提供的样例数据,介绍了写数据的接口,RowKey定义,数据在客户端的组装,数据路由,打包分发, ...

  3. Spring Reactor 入门与实践

    适合阅读的人群:本文适合对 Spring.Netty 等框架,以及 Java 8 的 Lambda.Stream 等特性有基本认识,希望了解 Spring 5 的反应式编程特性的技术人员阅读. 一.前 ...

  4. Angular2入门系列教程7-HTTP(一)-使用Angular2自带的http进行网络请求

    上一篇:Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数 感觉这篇不是很好写,因为涉及到网络请求,如果采用真实的网络请求,这个例子大家拿到手估计还要自己写一个web ...

  5. ABP入门系列(1)——学习Abp框架之实操演练

    作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...

  6. Oracle分析函数入门

    一.Oracle分析函数入门 分析函数是什么?分析函数是Oracle专门用于解决复杂报表统计需求的功能强大的函数,它可以在数据中进行分组然后计算基于组的某种统计值,并且每一组的每一行都可以返回一个统计 ...

  7. Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数

    上一篇:Angular2入门系列教程5-路由(一)-使用简单的路由并在在路由中传递参数 之前介绍了简单的路由以及传参,这篇文章我们将要学习复杂一些的路由以及传递其他附加参数.一个好的路由系统可以使我们 ...

  8. Angular2入门系列教程5-路由(一)-使用简单的路由并在在路由中传递参数

    上一篇:Angular2入门系列教程-服务 上一篇文章我们将Angular2的数据服务分离出来,学习了Angular2的依赖注入,这篇文章我们将要学习Angualr2的路由 为了编写样式方便,我们这篇 ...

  9. Angular2入门系列教程4-服务

    上一篇文章 Angular2入门系列教程-多个组件,主从关系 在编程中,我们通常会将数据提供单独分离出来,以免在编写程序的过程中反复复制粘贴数据请求的代码 Angular2中提供了依赖注入的概念,使得 ...

随机推荐

  1. hutool工具类常用API整理

    0.官网学习地址 https://www.hutool.cn/ 1.依赖 <dependency> <groupId>cn.hutool</groupId> < ...

  2. Blazor Bootstrap 组件库语音组件介绍

    Speech 语音识别与合成 通过麦克风语音采集转换为文字(STT),或者通过文字通过语音朗读出来(TTS) 本组件依赖于 BootstrapBlazor.AzureSpeech,使用本组件时需要引用 ...

  3. 2021.08.30 前缀函数和KMP

    2021.08.30 前缀函数和KMP KMP算法详解-彻底清楚了(转载+部分原创) - sofu6 - 博客园 (cnblogs.com) KMP算法next数组的一种理解思路 - 挠到头秃 - 博 ...

  4. 重磅!业界首个云原生批量计算项目Volcano正式晋级为CNCF孵化项目

    摘要:4月7日,云原生计算基金会(CNCF)宣布,由华为云捐献的业界首个云原生批量计算项目Volcano正式晋级为CNCF孵化项目. 4月7日,云原生计算基金会(CNCF)宣布,由华为云捐献的业界首个 ...

  5. 基于.Net C# 通信开发-网络调试助手

    基于.Net C# 通信开发-网络调试助手1.概述 网络调试助手是集TCP/UDP服务端客户端一体的网络调试工具,可以帮助网络应用设计.开发.测试人员检查所开发的网络应用软硬件的数据收发状况,提高开发 ...

  6. 一文带你读懂什么是vxlan网络

    一个执着于技术的公众号 一.背景 随着云计算.虚拟化相关技术的发展,传统网络无法满足大规模.灵活性要求高的云数据中心的要求,于是便有了overlay网络的概念.overlay网络中被广泛应用的就是vx ...

  7. dubbo发送过程编码失败,会唤醒发送线程吗?

    dubbo发送过程编码失败,会唤醒发送(客户端业务)线程吗?如何实现的? 在上篇文章 dubbo坑- No provider available for the service xxx 中,如果dub ...

  8. 使用NE555实现的延时开关电路

    NE555 的工作机制 先了解 NE555 的 Trigger(Pin2) 和 Threshold(Pin6) 如何检测电压并控制输出 如果 Trigger(Pin2) 检测到任何低于电源电压1/3的 ...

  9. Dart 2.17 正式发布

    文/ Michael Thomsen, Google Dart 团队产品经理,2022 年 5 月 12 日发表于 Dart 官方博客 随着 Flutter 3 在本次 I/O 大会的发布,我们也同时 ...

  10. 为 map 中不存在的 key 提供缺省值

    需求 需要往一个复杂的 map 中写入数据,或为 map 中不存在 key 提供 default value java standard library Map<K, List<V> ...