EnableAsync注解的意思是可以异步执行,就是开启多线程的意思。可以标注在方法、类上。

@EnableAsync使用
 1 @Component
 2 public class Task {
 3 
 4     @Async
 5     public void doTaskOne() throws Exception {
 6         // 同上内容,省略
 7     }
 8 
 9     @Async
10     public void doTaskTwo() throws Exception {
11         // 同上内容,省略
12     }
13 
14     @Async
15     public void doTaskThree() throws Exception {
16         // 同上内容,省略
17     }
18 
19 }
@EnableAsync使用

为了让@Async注解能够生效,还需要在Spring Boot的主程序中配置@EnableAsync,如下所示:

@EnableAsync使用
1 @SpringBootApplication
2 @EnableAsync
3 public class Application {
4 
5     public static void main(String[] args) {
6         SpringApplication.run(Application.class, args);
7     }
8 
9 }
@EnableAsync使用

注: @Async所修饰的函数不要定义为static类型,这样异步调用不会生效

相关文章:

  • 2022-12-23
  • 2021-08-04
  • 2022-12-23
  • 2021-08-27
  • 2021-09-06
  • 2021-05-13
  • 2021-07-27
猜你喜欢
  • 2022-12-23
  • 2021-12-23
  • 2021-08-02
  • 2022-12-23
  • 2022-12-23
  • 2021-05-21
相关资源
相似解决方案