当前位置:网站首页>多线程工具类 CompletableFuture
多线程工具类 CompletableFuture
2022-06-29 06:32:00 【叮当的猫猫】
详细介绍
Java8 CompletableFuture 用法全解
CompletableFuture 详解(一):基本概念及用法
分类和示例
CompletableFuture使用详解
JDK1.8新特性CompletableFuture总结
Future和CompletableFuture的使用
CompletableFuture学习记录
多线程之CompletableFuture使用
通俗易懂
有了Future和Executor来执行我们的异步任务 为什么还需要这个?用Future获得异步执行结果 有两种方法:调用 Get() 或者 轮询 isDOne() 是否为True
这两种方法都不是太好,因为主线程也会被迫等待
为了减少这种等待 JAVA8引入了这个CP 对Future做了改进 可以传入回调对象
创建类
completeFuture 可以用于创建默认返回值
runAsync 异步执行,无返回值
supplyAsync 异步执行,有返回值
anyOf 任意一个执行完成,就可以进行下一步动作
allOf 全部完成所有任务,才可以进行下一步任务
// 异步任务,无返回值,采用内部的forkjoin线程池
CompletableFuture c1 = CompletableFuture
.runAsync(()->{
System.out.println("打开开关,开始制作,就不用管了")});
// 异步任务,无返回值,使用自定义的线程池
CompletableFuture c11 = CompletableFuture
.runAsync(()->{
System.out.println("打开开关,开始制作,就不用管了")},newSingleThreadExecutor());
// 异步任务,有返回值,使用内部默认的线程池
CompletableFuture<String> c2 = CompletableFuture
.supplyAsync(()->{
System.out.println("清洗米饭");return "干净的米饭";});
// 只要有一个完成,则完成,有一个抛出异常,则携带异常
CompletableFuture.anyOf(c1,c2);
// 必须等待所有的future全部完成才可以
CompletableFuture.allOf(c1,c2);
状态取值类
join 合并结果,等待
get 合并等待结果,可以增加超时时间;get和join区别,join只会抛出unchecked异常,get会返回具体的异常
getNow 如果结果计算完成或者异常了,则返回结果或异常;否则,返回valueIfAbsent的值
isCancelled
isCompletedExceptionally
isDone
// 不抛出异常,阻塞的等待
future.join()
// 有异常则抛出异常,阻塞的等待,无限等待
future.get()
// 有异常则抛出异常,最长等待1个小时,一个小时之后,如果还没有数据,则异常。
future.get(1,TimeUnit.Hours)
控制类 用于主动控制CompletableFuture的完成行为
complete
completeExceptionally
cancel
3种方式:
// 完成
future.complete("米饭");
// 异常
future.completeExceptionally();
// 取消,参数并没有实际意义,没任何卵用。
future.cancel(false);
接续类
CompletableFuture 最重要的特性,没有这个的话,CompletableFuture就没意义了,这个特性是 注入回调行为。接续方式有很多种,可以总结为一下三类:
CompletableFuture + (Runnable,Consumer,Function)
CompletableFuture + CompletableFuture
CompletableFuture + 处理结果
CompletableFuture future = CompletableFuture.supplyAsync(()->{
System.out.println("投放和清洗制作米饭的材料");
return "干净的没有新冠病毒的大米";
}).thenAcceptAsync(result->{
System.out.println("通电,设定模式,开始煮米饭");
}).thenRunAsync(()->{
System.out.println("米饭做好了,可以吃了");
})
总结记忆
以 Async 结尾 的方法,都是 异步方法,对应的 没有 Async 则是 同步方法,一般都是一个异步方法对应一个同步方法。
以 Async 后缀结尾 的方法,都有 两个重载 的方法,一个是 使用默认的 forkjoin 线程池,一种是使用 自定义线程池
以 run 开头的方法,其入口参数一定是 无参 的,并且 没有返回值 ,类似于执行Runnable方法。
以 supply 开头的方法,入口也是 没有参数 的,但是 有返回值
以 Accept 开头或者结尾 的方法,入口参数是 有参数,但是 没有返回值
以 Apply 开头或者结尾 的方法,入口有参数,有返回值
带有 either 后缀 的方法,表示 谁先完成就消费谁
边栏推荐
- Small program large screen adaptation Guide
- JDBC | Chapter 6: simple use of database connection pool
- Design of leetcode simple problem goal parser
- Single application and microservice application
- Browser local storage
- [high concurrency] deeply analyze the callable interface
- The simple problem of leetcode is to divide an array into three parts equal to sum
- How does MySQL implement distributed locks?
- Difference between URI and URL
- How to combine two byte arrays [repeat] - how to combine two byte arrays [duplicate]
猜你喜欢

Problems with MySQL database query

Monitor employee turnover dynamics. This system makes employees tremble!

Jenkins operation Chapter 6 mail server sending build results

Meta metauniverse female safety problems occur frequently. How to solve the relevant problems in the metauniverse?

Hyperledger Fabric 2. X custom smart contract

Easy to understand TCP four waves (multi picture explanation)

Creation of Arduino uno development environment

Plugin

Internet enterprises need CRM software to help

Single application and microservice application
随机推荐
Benign competition will promote each other
JDBC | Chapter 5: closing and releasing JDBC connection resources
Fault: KDC warning log for id29
Games101 Lecture 10 geometry 1 Notes
5,10,15,20-tetra (3,5-dimethoxyphenyl) porphyrin ((tdmpp) H2) /2-nitro-5,10,15,20-tetra (3,5-dimethoxyphenyl) porphyrin copper (no2tdmpp) Cu) supplied by Qiyue
Servlet version conflict causes page 404
[Flink] flinksql and table programming cases
About: deleting unwanted event log lists
Linux Installation redis
ICLR is going to have a big discussion on the deep generation model. Max welling and the winner of the AAAI million dollar award are here. Bengio is one of the organizers
力扣今日题-324. 摆动排序 II
QT writing map comprehensive application 58 compatible with multi browser kernel
Establishing the development environment of esp8266
Fresnel diffraction with rectangular aperture based on MATLAB
Hustoj SPJ example
2,5-di (3,4-dicarboxyphenoxy) - 4 '- phenylethynylbiphenyldianhydride (pephqda) / Qiyue custom supply porphyrin modified amphiphilic block copolymer peg113-pcl46-porphyrin
Convert data frame with date column to timeseries
National Defense University project summary
Test Development - ten years of sharpening one sword (VII) interface test tool postman
Haar cascades and LBP cascades in face detection [closed] - Haar cascades vs. LBP cascades in face detection [closed]