当前位置:网站首页>@EnableAsync @Async
@EnableAsync @Async
2022-07-04 00:35:00 【Xiangge learns big data】
One . Basic introduction
@Async yes spring In order to facilitate developers to make asynchronous calls , Add this annotation to the method ,spring A new thread will be obtained from the thread pool to execute the method , Implement asynchronous call
@EnableAsync Indicates that support for asynchronous tasks is enabled , Can be placed in springboot On the startup class of , It can also be placed on the configuration class of the custom thread pool , See below for details
Two . Simplest use
stay springboot In the project , Add... Directly to the startup class @EnableAsync, And then in service For methods that need asynchronous invocation, add @Async,
So when controller When the layer calls this method , It will automatically create a new thread outside the main thread to execute this method , See the figure below for details. demo
1.springboot The startup class enables asynchronous support

2.service Layer method plus @Async, If you add this annotation to a class, it means that the methods of the whole class are executed asynchronously , It is suggested to add it to a specific method

3.controller Layer calls service Asynchronous method of layer , Here, the main thread is used to execute before and after the asynchronous method 2 Print out times

4. The result of the call
First, let's see that there is no asynchronous execution , The result of normal sequential execution
You can see , Execute in order , All is main Threads http-nio-8181-exec-124 perform , also service The execution result of the method is in the middle , As shown below

Because our method uses @Async annotation , So the main thread http-nio-8181-exec-124 Unequal asynchronous method completion , End first , Asynchronous thread task-1 Carry on

tips: There is no custom thread pool @Async The default thread pool is SimpleAsyncTaskExecutor
3、 ... and . Customize the thread pool to use @Async
1. Create a new thread pool configuration class ,@EnableAsync Add... To the configuration class , Don't add it to the startup class , Different thread pools can be configured , use bean Of name Make a distinction

[email protected] The same is used in service Add , If multiple thread pools are configured , It can be used @Async("name"), Then it represents the thread pool @Bean Of name, To specify which thread pool to use
Suppose only one thread pool is configured , Direct use @Async It will execute with a custom thread pool
Suppose multiple thread pools are configured , use @Async No thread pool is specified , Will use the default SimpleAsyncTaskExecutor To deal with it

Suppose multiple thread pools are configured , use @Async("name"), Will process with the specified thread pool
such as service Specify pool1 Thread pool

Execution results , The asynchronous thread name is pool Configured fzhThread

https://www.cnblogs.com/fzhblog/p/14012401.html
Four . The reason why the annotation did not take effect
1. The asynchronous method uses static modification
2. Asynchronous method classes are not used @Service annotation ( Or other comments ) Lead to spring Cannot scan to asynchronous class
3.controller Required in @Autowired or @Resource Wait for the annotation to inject automatically service class , You can't do it by yourself new object
When we use multithreading , Often need to create Thread class , Or realize Runnable Interface , To create Executors, In the use of spring in , Has given us good support , Just use @Async You can define a thread task . adopt spring For us ThreadPoolTaskExecutor You can use thread pool .
By default ,Spring Relevant thread pool definitions will be searched : Or search for the only one in the context TaskExecutor bean, Or search for the name “taskExecutor” Of Executor bean. If both cannot be resolved , Will use SimpleAsyncTaskExecutor To handle asynchronous method calls .
边栏推荐
- Shell script three swordsman sed
- Att & CK actual combat series - red team actual combat - V
- 功能:求出菲波那契数列的前一项与后一项之比的极限的 近似值。例如:当误差为0.0001时,函数值为0.618056。
- Understanding of Radix
- The difference between fetchtype lazy and eagle in JPA
- 删除所有值为y的元素。数组元素中的值和y的值由主函数通过键盘输入。
- The culprit of unrestrained consumption -- Summary
- Arc 135 supplementary report
- P1656 bombing Railway
- Is it really possible that the monthly salary is 3K and the monthly salary is 15K?
猜你喜欢

老姜的特点

OS interrupt mechanism and interrupt handler

Joint examination of six provinces 2017

ESP Arduino playing with peripherals (V) basic concept of interrupt and timer interrupt

Att & CK actual combat series - red team actual combat - V
![[2021]NeRF in the Wild: Neural Radiance Fields for Unconstrained Photo Collections](/img/c6/3dc7d01600f6713afdbb4cf3df5238.jpg)
[2021]NeRF in the Wild: Neural Radiance Fields for Unconstrained Photo Collections
![[Mongodb] 2. Use mongodb --------- use compass](/img/d5/0eb7dd4c407fbf2e9ba1b175f5424d.jpg)
[Mongodb] 2. Use mongodb --------- use compass
![[error record] configure NDK header file path in Visual Studio](/img/9f/89f68c037dcf68a31a2de064dd8471.jpg)
[error record] configure NDK header file path in Visual Studio
![[NLP] text classification still stays at Bert? Duality is too strong than learning framework](/img/49/1ff6025bdb0445e5638c1451e0b267.jpg)
[NLP] text classification still stays at Bert? Duality is too strong than learning framework

URL (data:image/png; Base64, ivborw0k... Use case
随机推荐
Several ways to set up a blog locally [attach relevant software download links]
Selenium library 4.5.0 keyword explanation (III)
Development and application of fcitx functional plug-ins
Similarities and differences of text similarity between Jaccard and cosine
Regular expressions and text processors for shell programming
On covariance of array and wildcard of generic type
MySQL winter vacation self-study 2022 12 (2)
Global and Chinese markets of distributed control system (DCS) consumption 2022-2028: Research Report on technology, participants, trends, market size and share
Correlation analysis summary
Beijing invites reporters and media
OS interrupt mechanism and interrupt handler
Stock price forecast
What is the Valentine's Day gift given by the operator to the product?
It is worthy of "Alibaba internal software test interview notes" from beginning to end, all of which are essence
[prefix and notes] prefix and introduction and use
Suggestions for improving code quality
The super fully automated test learning materials sorted out after a long talk with a Tencent eight year old test all night! (full of dry goods
Double efficiency. Six easy-to-use pychar plug-ins are recommended
Shell script three swordsman sed
8. Go implementation of string conversion integer (ATOI) and leetcode