Why does Alibaba not recommend using the @Async annotation directly?

In the Taishan version of the "Alibaba Development Manual", it is not recommended to use Async annotations in development. Why? In actual development, asynchronous programming has become an essential skill. In order to help developers perform asynchronous programming more easily, Java 8 introduces the Async annotation, which makes asynchronous programming easier. However, although Async annotations can bring us a lot of benefits, it is not a good idea to use Async annotations directly in some cases. This article will explore the limitations of Async annotations and why it is not recommended to use Async annotations directly.
insert image description here

What is Async annotation

The Async annotation is an annotation in Java 8 that is used to identify that a method is executed asynchronously. When a method is marked as Async, the method will be executed in a new thread and can immediately return a CompletableFuture object. Using CompletableFuture makes it easier to manage the results of asynchronous computations. Here is a sample code using the Async annotation:

@Async
public CompletableFuture<String> doSomethingAsync() {
    
    
    // 异步执行一些操作
}

In the above code, the doSomethingAsync() method is marked as Async, which means that the method will be executed asynchronously in a new thread and return a CompletableFuture object.

Advantages of Async annotations

Using Async annotations has the following advantages:

  • Simplify asynchronous programming : Using the Async annotation can make asynchronous programming easier. Developers only need to mark the methods that need to be executed asynchronously as Async, and then use CompletableFuture to manage the results of asynchronous calculations.
  • Improve the response speed of the application : Using Async annotations can execute time-consuming operations asynchronously, thereby avoiding blocking the main thread and improving the response speed of the application.
  • Improve the concurrency performance of the system : Using Async annotations can make multiple asynchronous operations execute concurrently, thereby improving the concurrency performance of the system.
    insert image description here

Limitations of Async Annotations

However, although the Async annotation can bring us many benefits, it also has some limitations.

  • Exception handling : Exception handling can become more complicated when using the Async annotation. Since the asynchronous operation is performed in another thread, if the asynchronous operation throws an exception, this exception may not be caught. To solve this problem, developers need to use CompletableFuture's exception handling mechanism to catch exceptions thrown by asynchronous operations.
  • Memory usage : When using the Async annotation, since each asynchronous operation will be executed in a new thread, it may cause a large number of threads to be created. This can lead to high memory usage, which can lead to poor application performance.
  • Blocking operations : When using the Async annotation, if the asynchronous operation contains blocking operations, this may cause threads in the thread pool to be blocked, resulting in degraded application performance.

Reasons why it is not recommended to use Async annotations directly

Due to the limitations of Async annotations, it might not be a good idea to use Async annotations directly. Here are the reasons why it is not recommended to use the Async annotation directly:

  • May cause performance problems : Since the Async annotation creates new threads to perform asynchronous operations, if used improperly, it may cause excessive consumption of threads in the thread pool, resulting in performance problems.
  • May cause memory leaks : If the thread pool is not properly managed when using the Async annotation, it may cause memory leaks. For example, if the thread pool size is incorrectly configured, the threads in the thread pool may not be reclaimed, resulting in memory leaks.
  • May cause deadlock problem : If the asynchronous operation contains blocking operations, the threads in the thread pool may be blocked, resulting in deadlock problems.
    To sum up, using Async annotations directly may cause various problems, so it is not recommended to use Async annotations directly.
    insert image description here

How to better use Async annotations

Although it is not recommended to use Async annotations directly, in some cases, using Async annotations is still a good choice. Here are some best practices for using the Async annotation:

  • Configure thread pool : When using Async annotation, you should configure an appropriate thread pool size. The size of the thread pool should be determined according to the nature and needs of the application.
    Use exception handling mechanism: When using Async annotation, you should use CompletableFuture's exception handling mechanism to catch exceptions thrown by asynchronous operations.
  • Avoid blocking operations : When using the Async annotation, you should avoid including blocking operations in asynchronous operations. If blocking operations must be used, CompletableFuture's supplyAsync() method should be used to ensure that blocking operations are executed in a new thread.
  • Use CompletableFuture API : When using the Async annotation, you should use the CompletableFuture API to manage the results of asynchronous computations. Using the CompletableFuture API makes handling the results of asynchronous operations easier and avoids some potential problems.

To sum up, using Async annotations can bring us many benefits, but we need to pay attention to some issues when using Async annotations to avoid performance problems, memory leaks, and deadlocks. Therefore, when using Async annotations, we should follow some best practices to ensure code correctness and performance.

Guess you like

Origin blog.csdn.net/KRYST4L123/article/details/129836399