当前位置:网站首页>@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 .
边栏推荐
- MySQL is installed as a Windows Service
- Pair
- 功能:求出菲波那契数列的前一项与后一项之比的极限的 近似值。例如:当误差为0.0001时,函数值为0.618056。
- Generic
- Subgraph isomorphism -subgraph isomorphism
- Global and Chinese markets for blood and liquid heating devices 2022-2028: Research Report on technology, participants, trends, market size and share
- Pytest unit test framework: simple and easy to use parameterization and multiple operation modes
- From functools import reduce -- see the use of reduce function from typical examples
- What is the potential of pocket network, which is favored by well-known investors?
- Zipper table in data warehouse (compressed storage)
猜你喜欢
How will the complete NFT platform work in 2022? How about its core functions and online time?
It is worthy of "Alibaba internal software test interview notes" from beginning to end, all of which are essence
The difference between objects and objects
Bodong medical sprint Hong Kong stocks: a 9-month loss of 200million Hillhouse and Philips are shareholders
A method to solve Bert long text matching
Sorry, Tencent I also refused
Interview script of Software Test Engineer
URL (data:image/png; Base64, ivborw0k... Use case
A Kuan food rushed to the Shenzhen Stock Exchange: with annual sales of 1.1 billion, Hillhouse and Maotai CCB are shareholders
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
随机推荐
The frost peel off the purple dragon scale, and the xiariba people will talk about database SQL optimization and the principle of indexing (primary / secondary / clustered / non clustered)
MySQL winter vacation self-study 2022 12 (1)
Speed up the energy Internet of things. What can low-power Internet of things technology represented by Zeta do?
Why use get/set instead of exposing properties
[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!
Global and Chinese market of process beer equipment 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of breast cancer imaging 2022-2028: Research Report on technology, participants, trends, market size and share
The difference between objects and objects
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Subgraph isomorphism -subgraph isomorphism
Is the account opening of Guoyuan securities really safe and reliable
Global and Chinese markets for coronary artery disease treatment devices 2022-2028: Research Report on technology, participants, trends, market size and share
Analysis: misunderstanding of choosing WMS warehouse management system
[software testing] you haven't mastered these real interview questions of big companies?
挖财帮个人开的证券账户安全吗?是不是有套路
How will the complete NFT platform work in 2022? How about its core functions and online time?
Global and Chinese markets of distributed control system (DCS) consumption 2022-2028: Research Report on technology, participants, trends, market size and share
[2021]NeRF in the Wild: Neural Radiance Fields for Unconstrained Photo Collections
ESP Arduino playing with peripherals (V) basic concept of interrupt and timer interrupt
Beijing invites reporters and media