当前位置:网站首页>Multithreading tool class completabilefuture
Multithreading tool class completabilefuture
2022-06-29 06:36:00 【Jingling cat】
Detailed introduction
Java8 CompletableFuture Use it all
CompletableFuture Detailed explanation ( One ): Basic concepts and usage
Categories and examples
CompletableFuture The use of,
JDK1.8 New characteristics CompletableFuture summary
Future and CompletableFuture Use
CompletableFuture Learning record
The multithreading CompletableFuture Use
Easy to understand
With Future and Executor To perform our asynchronous tasks Why do you need this ? use Future Get asynchronous execution results There are two ways : call Get() perhaps polling isDOne() Is it True
Neither of these methods is very good , Because the main thread will also be forced to wait
To reduce this wait JAVA8 Introduced this CP Yes Future Improvements have been made. You can pass in the callback object
Create a class
completeFuture Can be used to create default return values
runAsync Asynchronous execution , No return value
supplyAsync Asynchronous execution , There is a return value
anyOf Any execution is complete , You can take the next step
allOf Complete all tasks , Before we can carry out the next task
// Asynchronous task , No return value , Internal forkjoin Thread pool
CompletableFuture c1 = CompletableFuture
.runAsync(()->{
System.out.println(" Turn on the switch , Start making , Don't worry about it ")});
// Asynchronous task , No return value , Use a custom thread pool
CompletableFuture c11 = CompletableFuture
.runAsync(()->{
System.out.println(" Turn on the switch , Start making , Don't worry about it ")},newSingleThreadExecutor());
// Asynchronous task , There is a return value , Use the internal default thread pool
CompletableFuture<String> c2 = CompletableFuture
.supplyAsync(()->{
System.out.println(" Wash the rice ");return " Clean rice ";});
// As long as there is one finish , Then finish , There is an exception thrown , It carries an exception
CompletableFuture.anyOf(c1,c2);
// Have to wait for all future Only when it's all finished
CompletableFuture.allOf(c1,c2);
State value class
join Consolidated results , wait for
get Merge and wait for results , You can increase the timeout ;get and join difference ,join It just throws unchecked abnormal ,get Will return a specific exception
getNow If the result calculation is completed or abnormal , Results or exceptions are returned ; otherwise , return valueIfAbsent Value
isCancelled
isCompletedExceptionally
isDone
// Don't throw exceptions , A blocked wait
future.join()
// Throw an exception if there is an exception , A blocked wait , Wait indefinitely
future.get()
// Throw an exception if there is an exception , The longest wait 1 Hours , An hour later , If there's no data yet , Is abnormal .
future.get(1,TimeUnit.Hours)
The control class For active control CompletableFuture The act of completing
complete
completeExceptionally
cancel
3 Ways of planting :
// complete
future.complete(" Steamed Rice ");
// abnormal
future.completeExceptionally();
// Cancel , Parameters have no practical meaning , No eggs .
future.cancel(false);
In the class
CompletableFuture The most important feature , Without this ,CompletableFuture It doesn't make sense , This property is Inject callback behavior . There are many ways to connect , It can be summarized into three categories :
CompletableFuture + (Runnable,Consumer,Function)
CompletableFuture + CompletableFuture
CompletableFuture + Processing results
CompletableFuture future = CompletableFuture.supplyAsync(()->{
System.out.println(" Put in and wash the materials for making rice ");
return " Clean rice without COVID-19. ";
}).thenAcceptAsync(result->{
System.out.println(" Electrify , Set mode , Start cooking rice ");
}).thenRunAsync(()->{
System.out.println(" The rice is ready , You can eat ");
})
Summary memory
With Async ending Methods , All are Asynchronous methods , Corresponding No, Async It is Synchronization method , Generally, an asynchronous method corresponds to a synchronous method .
With Async The suffix ends Methods , There are Two heavy loads Methods , One is Use default forkjoin Thread pool , One is to use Custom thread pool
With run Opening method , The entry parameter must be No arguments Of , also no return value , Similar to the execution of Runnable Method .
With supply Opening method , The entrance is also No parameters Of , however There is a return value
With Accept Beginning or end Methods , The entry parameter is With parameters , however no return value
With Apply Beginning or end Methods , The entry has parameters , There is a return value
with either suffix Methods , Express Whoever finishes first will consume
边栏推荐
- Honeypot based on MySQL load data local INFILE
- Rich material libraries make modeling easy and efficient for developers
- [Flink] flinksql and table programming cases
- 目标检测——使用yolov6进行视频推理
- [C language series] - branch and loop statements
- Two houses with different colors and the farthest distance
- Conditional test, if and case conditional test statements of shell script
- Teach you how to develop your own NPM package (publish to the NPM official website)
- Ribbon 服务调用与负载均衡
- Why is there a packaging type?
猜你喜欢

JIRA basic usage sharing

Hyperledger Fabric 2. X custom smart contract

Fault: ntfrs warning log for id13562

Conditional test, if and case conditional test statements of shell script

Segment in Lucene

Servlet version conflict causes page 404

Observer mode vs publish subscribe mode

2022.02.15 - 240. Lucky number in matrix

Rearrangement string of leetcode simple question

Creation of Arduino uno development environment
随机推荐
Sum of digits under k-ary representation of leetcode simple problem
About: deleting unwanted event log lists
Difference between URI and URL
Antlr4 recognizes the format of escape string containing quotation marks
力扣今日题-324. 摆动排序 II
Go basic data type conversion
[C language series] - branch and loop statements
Mongodb sort function
MySQL learning notes
Use of sed in shell script
SCM engineering experience - time slice
Rich material libraries make modeling easy and efficient for developers
Leetcode theme [array] -217- there are duplicate elements
Fault: display Storport driver out of date in component health
融入STEAM教育的劳动技能课程
[Flink] flinksql and table programming cases
力扣每日一题-第30天-594.最长和谐子序列
Leetcode simple problem building arrays with stack operation
[deep learning] - maze task learning I (to realize the random movement of agents)
Is there any difference between a=a+b and a+=b?