当前位置:网站首页>@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 .
边栏推荐
- What does redis do? Redis often practices grammar every day
- 【leetcode】300. Longest increasing subsequence (dynamic programming, dichotomy)
- [PHP basics] cookie basics, application case code and attack and defense
- [cloud native topic -48]:kubesphere cloud Governance - operation - overview of multi tenant concept
- Joint examination of six provinces 2017
- [error record] configure NDK header file path in Visual Studio
- Struct in linked list
- Shell script three swordsman sed
- Self study software testing. To what extent can you go out and find a job?
- [error record] configure NDK header file path in Visual Studio (three header file paths of NDK | ASM header file path selection related to CPU architecture)
猜你喜欢

Kubedl hostnetwork: accelerating the efficiency of distributed training communication

SPI based on firmware library

Kubedl hostnetwork: accelerating the efficiency of distributed training communication
![[about text classification trick] things you don't know](/img/c0/fdb04f6b31f1dba2658c2430dc4036.jpg)
[about text classification trick] things you don't know

Analysis: misunderstanding of choosing WMS warehouse management system

Interview script of Software Test Engineer

Similarities and differences of text similarity between Jaccard and cosine

Regular expression of shell script value

swagger中响应参数为Boolean或是integer如何设置响应描述信息

Collation of the most complete Chinese naturallanguageprocessing data sets, platforms and tools
随机推荐
Is it really possible that the monthly salary is 3K and the monthly salary is 15K?
Development and application of fcitx functional plug-ins
[prefix and notes] prefix and introduction and use
P1656 bombing Railway
Beijing invites reporters and media
功能:将主函数中输入的字符串反序存放。例如:输入字符串“abcdefg”,则应输出“gfedcba”。
Is the securities account opened by Caicai for individuals safe? Is there a routine
A dichotomy of Valentine's Day
Data mining vs Machine Learning: what is the difference between them? Which is more suitable for you to learn
Recommendation of knowledge base management system
Cannot build artifact 'test Web: War expanded' because it is included into a circular depend solution
不得不会的Oracle数据库知识点(二)
For loop
Solution to the impact of Remote Code Execution Vulnerability of log4j2 component on December 9, 2021
What is the Valentine's Day gift given by the operator to the product?
老姜的特点
Celebrate the new year | Suihua fire rescue detachment has wonderful cultural activities during the Spring Festival
ITK learning notes (VII) the position of ITK rotation direction remains unchanged
MySQL 8.0.12 error: error 2013 (HY000): lost connection to MySQL server during query
Introducing Software Testing