SoFunction
Updated on 2025-03-08

Detailed explanation of using @Async to implement asynchronous task calls in SpringBoot

1. What is an asynchronous call (asynchronous call)

A method that allows the operation to continue without waiting for the return value of the called function (from Baidu Encyclopedia).

That is, when the program is executed in sequence, the subsequent program is executed without waiting for the asynchronously called statement to return the result.

2. Spring Boot uses @Async annotation to achieve asynchronous

We only need to use @Async annotation to simply turn synchronous functions into asynchronous functions, such as creating a new Asynctask asynchronous task class, the source code is as follows.

package ;
import ;
import ;
/**
 * @description
 * @date 2019/4/19 11:20
 */
@Component
public class Asynctask {
    // Mission 1    @Async
    public void doTaskOne() throws Exception {
        ("Start Task One");
        long start = ();
        (3000);
        long end = ();
        ("Complete Task One, time-consuming:" + (end - start) + "millisecond");
    }
    // Mission 2    @Async
    public void doTaskTwo() throws Exception {
        ("Start Task Two");
        long start = ();
        (5000);
        long end = ();
        ("Complete Task 2, time-consuming:" + (end - start) + "millisecond");
    }
    // Mission 3    @Async
    public void doTaskThree() throws Exception {
        ("Start Mission Three");
        long start = ();
        (8000);
        long end = ();
        ("Complete Task Three, time-consuming:" + (end - start) + "millisecond");
    }
}

In order for the @Async annotation to take effect, you also need to configure @EnableAsync in the main program of SpringBoot, as shown below.

package ;
import ;
import ;
import ;
@SpringBootApplication
@EnableAsync
public class AsynctaskApplication {
	public static void main(String[] args) {
		(, args);
	}
}

Write a test class, the source code is as follows.

package ;
import ;
import ;
import ;
import ;
/**
  * @description @Async annotation implements asynchronous tasks
  * @date 2019/4/19 11:20
  */
@RestController
@RequestMapping("/test")
public class AsynctaskController {
    @Resource
    private Asynctask asynctask;
    @RequestMapping("/task")
    public String task2() throws Exception {
        ();
        ();
        ();
        return "===== test ok =====";
    }
}

Test results: When repeated execution, many different test results may occur (task 1, 2, and 3 are executed in different orders). The following is a situation.

Start Task Three
Start doing task one
Start Task Two
Complete task one, time taken: 2992 milliseconds
Complete task 2, time taken: 4987 milliseconds
Complete task three, time taken: 7980 milliseconds

3. Things to note

1. Add annotation @EnableAsync in the @SpringBootApplication startup class;

2. Use the annotation @Async for asynchronous methods, and the return value is void or Future;

3. Remember that asynchronous methods and calling methods should be written in different classes respectively. If written in one class, it will have no effect.

This is the article about the detailed explanation of using @Async to implement asynchronous task calls in SpringBoot. For more related contents of @Async asynchronous task calls, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!