当前位置:网站首页>多线程工具类 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 后缀 的方法,表示 谁先完成就消费谁
边栏推荐
- Monitor employee turnover dynamics. This system makes employees tremble!
- JIRA basic usage sharing
- Fault: display Storport driver out of date in component health
- Servlet version conflict causes page 404
- Mongodb basic knowledge summary
- How to use regex in file find
- Failure: unable to log in to "taxpayer equity platform"
- Plugin
- Fresnel diffraction with rectangular aperture based on MATLAB
- Go basic data type conversion
猜你喜欢
![ASP. Net core 6 framework unveiling example demonstration [03]:dapr initial experience](/img/fd/4c24e10fc91a7ce7e709a0874ba675.jpg)
ASP. Net core 6 framework unveiling example demonstration [03]:dapr initial experience

百度小程序自动提交搜索
![Meso tetra (4-N, N, n-trimethylaminophenyl) porphyrin (ttmapp) /meso tetra - [4- (BOC threonine) aminophenyl] porphyrin (TAPP thr BOC) supplied by Qiyue](/img/a9/0869c4f39a96cff63d1e310292c46d.jpg)
Meso tetra (4-N, N, n-trimethylaminophenyl) porphyrin (ttmapp) /meso tetra - [4- (BOC threonine) aminophenyl] porphyrin (TAPP thr BOC) supplied by Qiyue
![[high concurrency] deeply analyze the callable interface](/img/fb/89ebd5592f1383c8015a47cec78a59.jpg)
[high concurrency] deeply analyze the callable interface

Sourcetree remote red exclamation point

Pointer from beginner to advanced (2)

MySQL add / delete / modify query SQL statement exercise yyds dry goods inventory

Jenkins operation Chapter 6 mail server sending build results

Purple red solid meso tetra (o-alkoxyphenyl) porphyrin cobalt (meso-t (2-rop) PCO) / tetra (n, n-diphenyl-p-amino) phenyl porphyrin (tdpatph2)

Principle of screen printing adjustment of EDA (cadence and AD) software
随机推荐
2022.02.15 - 240. Lucky number in matrix
Convert data frame with date column to timeseries
Venn diagram proportional and color shading with semi transparency
Easy to understand TCP four waves (multi picture explanation)
百度小程序自动提交搜索
Fault: NetBt log for id4321
Difference between URI and URL
What is 'EC2-Other' filter in 'Cost Explorer' dashboard mean? [closed]
Games101 Lecture 10 geometry 1 Notes
Conditional test, if and case conditional test statements of shell script
2022.02.15
[chromium] win10 vs2019 environment chromium configuration and compilation.
Analysis comp122 the Caesar cipher
VLAN experiment
SCM engineering experience - time slice
2,5-di (3,4-dicarboxyphenoxy) - 4 '- phenylethynylbiphenyldianhydride (pephqda) / Qiyue custom supply porphyrin modified amphiphilic block copolymer peg113-pcl46-porphyrin
Observer mode vs publish subscribe mode
2022.02.15 - SX10-31. House raiding III
Fault: display Storport driver out of date in component health
Teach you how to develop your own NPM package (publish to the NPM official website)