Do you really understand @Async? | JD Cloud Technical Team

scenes to be used:

During development, you will encounter some logic that takes a long time or does not require immediate execution results, such as message push, product synchronization, etc., which can use asynchronous methods. In this case, we can use @Async. However, there are risks in using @Async directly. When we do not specify a thread pool, it will default to the SimpleAsyncTaskExecutor thread pool that comes with Spring, which will continuously create threads. When concurrency is large, it will seriously affect performance. So you can use the asynchronous designated thread pool

Introduction:

@Async is Spring's annotation, which can be added to classes or methods. In layman's terms, if this annotation is added, then the class or method will be processed asynchronously when used, that is, a thread will be created to implement the class or method to achieve multi-threading.

Thread pool execution order:

Two ways to use:

The first:

Spring's default thread pool SimpleAsyncTaskExecutor is used.

Access steps:

1. You need to add the annotation @EnableAsync to start the multi-thread annotation on the @SpringBootApplication startup class or the @configure annotation class .

2. Add the @Async annotation to the method that needs to be executed asynchronously.

Default thread pool configuration:

If you need to modify the default configuration, you can add it in yaml or properties to modify the default configuration:

After execution, your execution thread name will be printed:

The second type: (recommended)

Customize the thread pool and execute asynchronously.

Custom thread pools have the following modes, we only introduce the last one:

  • Reimplement the interface AsyncConfigurer;
  • Inherit AsyncConfigurerSupport;
  • Configure a custom TaskExecutor to replace the built-in task executor.

Then add annotations to the corresponding methods and specify the thread pool: asyncExecutor

Specify the name of the thread pool as a custom thread pool name.

Check the log:

Precautions:

Several reasons for @Async failure were found:

  1. The method annotated with @Async is not a public method;
  2. The return value of the @Async annotation can only be void or Future;
  3. The annotation @Async method will also be invalid if it is modified with static;
  4. The startup class is not annotated with @EnableAsync;
  5. The caller and @Async cannot be in the same class;
  6. Marking @Transactional on the Async method is useless, but marking @Transcational on the method called by the Async method is valid;

Author: JD Retail Guo Chunyuan

Source: JD Cloud Developer Community Please indicate the source when reprinting

Alibaba Cloud suffered a serious failure, affecting all products (has been restored). The Russian operating system Aurora OS 5.0, a new UI, was unveiled on Tumblr. Many Internet companies urgently recruited Hongmeng programmers . .NET 8 is officially GA, the latest LTS version UNIX time About to enter the 1.7 billion era (already entered) Xiaomi officially announced that Xiaomi Vela is fully open source, and the underlying kernel is .NET 8 on NuttX Linux. The independent size is reduced by 50%. FFmpeg 6.1 "Heaviside" is released. Microsoft launches a new "Windows App"
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4090830/blog/10143734
Recommended