@Async Demystification of Spring Asynchronous Execution

background

In Java applications, in most cases, the interactive processing is achieved through synchronization; but when dealing with the interaction with third-party systems, it is easy to cause response delays. Most of them were done by multi-threading before. For such tasks, in fact, after Spring 3.x, @Async has been built-in to perfectly solve this problem. This article will complete the introduction of @Async.

  1. What is an asynchronous call?
    Before explaining asynchronous calls, let's first look at the definition of synchronous calls; synchronization is the sequential execution of the entire processing process, when each process is executed, and the result is returned. Asynchronous call is just sending the call instruction, the caller does not need to wait for the called method to be completely executed; instead, it continues to execute the following process.
    For example, in a certain call, the three process methods A, B, and C need to be called in sequence; if they are all synchronous calls, they need to be executed sequentially before the process is executed; if B is an asynchronous call In the calling method, after A is executed, B is called, instead of waiting for B to complete, the execution starts to call C. After C is executed, it means that the process is finished.
  2. Conventional asynchronous call processing methods
    in Java, generally when dealing with similar scenarios, are based on the creation of independent threads to complete the corresponding asynchronous call logic, through the execution flow between the main thread and different threads, so as to start After the independent thread, the main thread continues execution without stalling and waiting.

Introduction to Spring asynchronous execution Async
The method marked with @Async annotation in Spring is called asynchronous method, which is actually equivalent to ourselves in the current method: new Thread(()-> System.out.println("hello world!") ). br/>The basic method used according to the @Async annotation:
add @Async annotation to the method;
br/>The class object of the @Async annotation method used should be a bean object managed by the Spring container; the
asynchronous method class needs to be configured Annotation @EnableAsync

Spring asynchronous execution Async example

@Async Demystification of Spring Asynchronous Executionbr/>@EnableAsync is equivalent to AsyncConfigurationSelector. According to the mode in @Configuration, it is determined whether the asynchronous mode is ProxyAsyncConfiguration or AspectJAsyncConfiguration. The default mode is AdviceMode.PROXY. Register processing bean at this time:
@Async Demystification of Spring Asynchronous Execution
console output after AsyncAnnotationBeanPostProcessor executes the program

calling MyBean#runTask() thread: main
Running task thread: SimpleAsyncTaskExecutor-1
result from task: task result

Spring AsyncConfigurer for asynchronous execution of Async

@Async Demystification of Spring Asynchronous Execution

The async annotation does not specify an executor

Guess you like

Origin blog.51cto.com/15015181/2556226