zoukankan      html  css  js  c++  java
  • @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 }
    复制代码

    为了让@Async注解能够生效,还需要在Spring Boot的主程序中配置@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 }
    复制代码

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

  • 相关阅读:
    ural1238. Folding(记忆化)
    URAL1410. Crack
    树套树Day1线段树套平衡树bzoj3196
    noipd2t3列队
    NOIP2017D1T3
    uoj279温暖会指引我们前行
    一篇打脸文
    Link-Cut Tree
    重口味费用流
    bzoj1000~1025
  • 原文地址:https://www.cnblogs.com/h-c-g/p/10724045.html
Copyright © 2011-2022 走看看