• SpringBoot------异步任务的使用


    步骤,如图所示:

    1.添加异步任务业务类

    package top.ytheng.demo.task;
    
    import java.util.concurrent.Future;
    
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.scheduling.annotation.AsyncResult;
    import org.springframework.stereotype.Component;
    
    //异步任务业务类
    @Component
    //标记此类是异步类,也可在方法中标记
    //不加,则类里面的方法为同步执行
    @Async
    public class AsyncTask {
    
        public void task1() throws InterruptedException {
            long begin = System.currentTimeMillis();
            Thread.sleep(1000);
            long end = System.currentTimeMillis();
            System.out.println("任务1耗时:" + (end - begin));
        }
        
        public void task2() throws InterruptedException {
            long begin = System.currentTimeMillis();
            Thread.sleep(2000);
            long end = System.currentTimeMillis();
            System.out.println("任务2耗时:" + (end - begin));
        }
        
        public void task3() throws InterruptedException {
            long begin = System.currentTimeMillis();
            Thread.sleep(3000);
            long end = System.currentTimeMillis();
            System.out.println("任务3耗时:" + (end - begin));
        }
        
        //测试拿到返回结果
        public Future<String> task4() throws InterruptedException {
            long begin = System.currentTimeMillis();
            Thread.sleep(1000);
            long end = System.currentTimeMillis();
            System.out.println("任务4耗时:" + (end - begin));
            return new AsyncResult<String>("任务4");
        }
        
        public Future<String> task5() throws InterruptedException {
            long begin = System.currentTimeMillis();
            Thread.sleep(2000);
            long end = System.currentTimeMillis();
            System.out.println("任务5耗时:" + (end - begin));
            return new AsyncResult<String>("任务5");
        }
        
        public Future<String> task6() throws InterruptedException {
            long begin = System.currentTimeMillis();
            Thread.sleep(3000);
            long end = System.currentTimeMillis();
            System.out.println("任务6耗时:" + (end - begin));
            return new AsyncResult<String>("任务6");
        }
    }

    2.添加测试控制器

    package top.ytheng.demo.controller;
    
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.Future;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import top.ytheng.demo.task.AsyncTask;
    
    @RestController
    @RequestMapping("api/v1/async")
    public class TaskController {
    
        @Autowired
        private AsyncTask asyncTask;
        
        @GetMapping("/test")
        public Object test() throws InterruptedException, ExecutionException {
            long begin = System.currentTimeMillis();
            //asyncTask.task1();
            //asyncTask.task2();
            //asyncTask.task3();
            Future<String> result1 = asyncTask.task4();
            Future<String> result2 = asyncTask.task5();
            Future<String> result3 = asyncTask.task6();
            System.out.println("返回结果:" + result1.get() + "," + result2.get() + "," + result3.get());
            for(;;) {
                if(result1.isDone() && result2.isDone() && result3.isDone()) {
                    break;
                }
            }
            long end = System.currentTimeMillis();
            long total = end - begin;
            System.out.println("总耗时:" + total);
            return "总耗时:" + total;
        }
    }

    3.添加启动类

    package top.ytheng.demo;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.scheduling.annotation.EnableAsync;
    import org.springframework.scheduling.annotation.EnableScheduling;
    
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletComponentScan;
    
    @SpringBootApplication //等于下面3个
    //@SpringBootConfiguration
    //@EnableAutoConfiguration
    //@ComponentScan
    //拦截器用到
    @ServletComponentScan
    //MyBatis用到
    @MapperScan("top.ytheng.demo.mapper")
    //定时使用(开启定时任务)
    @EnableScheduling
    //开启异步任务
    @EnableAsync
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }

    4.右键项目Run As启动,访问url

    http://localhost:8080/api/v1/async/test

    结果:

  • 相关阅读:
    组合算法问题
    递归之全排列问题
    递归之整数划分问题
    利用Python完成一个小游戏:随机挑选一个单词,并对其进行乱序,玩家要猜出原始单词
    对数组元素进行排序的方法总结(利用C++)
    用c++语言编写函数 int index(char *s,char * t),返回字符串t在字符串s中出现的最左边的位置,如果s中没有与t匹配的子串,则返回-1。类似于索引的功能。
    用MFC完成一个简单的猜数字游戏: 输入的四位数中,位置和数字都正确为A,数字相同而位置不同的为B。
    用Matlab完成:从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
    利用matlab实现以下功能:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
    白书_倒三角形_C语言描述
  • 原文地址:https://www.cnblogs.com/tianhengblogs/p/9824271.html
Copyright © 2020-2023  润新知