zoukankan      html  css  js  c++  java
  • Spring Boot中使用@Async实现异步调用

      在Spring Boot中,我们只需要通过使用@Async注解就能简单的将原来的同步函数变为异步函数,为了让@Async注解能够生效,还需要在Spring Boot的主程序中配置@EnableAsync。实例如下:

      创建Task类,Task里面包含三个方法,分别是:doTaskOne,doTaskTwo,doTaskThree

    @Component
    public class Task {
    
        private static Random random = new Random();
    
        @Async
        public void doTaskOne() throws Exception {
            System.out.println("开始做任务一");
            long start = System.currentTimeMillis();
            Thread.sleep(random.nextInt(10000));
            long end = System.currentTimeMillis();
            System.out.println("完成任务一,耗时:" + (end - start) + "毫秒");
        }
    
        @Async
        public void doTaskTwo() throws Exception {
            System.out.println("开始做任务二");
            long start = System.currentTimeMillis();
            Thread.sleep(random.nextInt(10000));
            long end = System.currentTimeMillis();
            System.out.println("完成任务二,耗时:" + (end - start) + "毫秒");
        }
    
        @Async
        public void doTaskThree() throws Exception {
            System.out.println("开始做任务三");
            long start = System.currentTimeMillis();
            Thread.sleep(random.nextInt(10000));
            long end = System.currentTimeMillis();
            System.out.println("完成任务三,耗时:" + (end - start) + "毫秒");
        }
    }
    @SpringBootApplication
    @EnableScheduling
    @EnableAsync
    public class Application {
    
        public static void main(String[] args){
            SpringApplication.run(Application.class,args);
        }
    }

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

    异步回调

      为了让doTaskOnedoTaskTwodoTaskThree能正常结束,假设我们需要统计一下三个任务并发执行共耗时多少,这就需要等到上述三个函数都完成调动之后记录时间,并计算结果。那么我们如何判断上述三个异步调用是否已经执行完成呢?我们需要使用Future<T>来返回异步调用的结果,就像如下方式改造doTaskOne函数:  

    @Async
        public Future<String> doTaskOne() throws Exception {
            System.out.println("开始做任务一");
            long start = System.currentTimeMillis();
            Thread.sleep(random.nextInt(10000));
            long end = System.currentTimeMillis();
            System.out.println("完成任务一,耗时:" + (end - start) + "毫秒");
            return new AsyncResult<>("任务完成");
        }

      按照上述方式改造其他的函数,测试代码如下:

    @Test
    public void test() throws Exception {
    
        long start = System.currentTimeMillis();
    
        Future<String> task1 = task.doTaskOne();
        Future<String> task2 = task.doTaskTwo();
        Future<String> task3 = task.doTaskThree();
    
        while(true) {
            if(task1.isDone() && task2.isDone() && task3.isDone()) {
                // 三个任务都调用完成,退出循环等待
                break;
            }
            Thread.sleep(1000);
        }
    
        long end = System.currentTimeMillis();
    
        System.out.println("任务全部完成,总耗时:" + (end - start) + "毫秒");
    
    }
  • 相关阅读:
    [LeetCode]Power of N
    uboot学习之uboot启动流程简述
    [LeetCode]Palindrome Pairs
    [LeetCode]Gray Code
    快速排序 partition函数的所有版本比较
    Linked List Cycle && Linked List Cycle II
    Simplify Path leetcode
    Pow(x, n) leetcode
    Swap Nodes in Pairs leetcode
    Multiply Strings leetcode
  • 原文地址:https://www.cnblogs.com/senlinyang/p/8653328.html
Copyright © 2011-2022 走看看