zoukankan      html  css  js  c++  java
  • SpringBoot第十八篇:异步任务

    作者:追梦1819
    原文:https://www.cnblogs.com/yanfei1819/p/11095891.html
    版权声明:本文为博主原创文章,转载请附上博文链接!


    ## 引言

      系统中的异步任务有很多优点,例如:1)I/O受限等情况下,异步能提;2)增强系统的健壮性;3)改善用户体验。

      但是也有一些缺点,例如:1)如果滥用的话会影响系统的性能;2)相对于同步编程来说,增加编程的难度。

      本章不对异步任务的优缺点做过多的详解,是否需要使用异步任务,要根据系统的业务来决定。本章只阐述 SpringBoot 中的异步任务。


    版本信息

    • JDK:1.8

    • SpringBoot :2.1.4.RELEASE

    • maven:3.3.9

    • IDEA:2019.1.1


    异步任务使用

      关于异步任务的使用,结果的展示可能不是很明显。我们先创建几个常规的方法,然后再使之异步,看看两者结果的区别。

    先创建一个项目,引入 web 的 maven 依赖:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    

    然后创建几个常用的方法:

    package com.yanfei1819.asynctaskdemo.task;
    
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Component;
    
    /**
     * Created by 追梦1819 on 2019-06-21.
     */
    @Component
    public class AsyncTask {
        public void task01() throws InterruptedException {
            Long start = System.currentTimeMillis();
            System.out.println("这是第一个异步任务!");
            Thread.sleep(2000);
            Long end = System.currentTimeMillis();
            System.out.println("第一个任务执行时间是:"+(end-start));
        }
        public void task02() throws InterruptedException {
            Long start = System.currentTimeMillis();
            System.out.println("这是第二个异步任务!");
            Thread.sleep(3000);
            Long end = System.currentTimeMillis();
            System.out.println("第二个任务执行时间是:"+(end-start));
        }
        public void task03() throws InterruptedException {
            Long start = System.currentTimeMillis();
            System.out.println("这是第三个异步任务!");
            Thread.sleep(4000);
            Long end = System.currentTimeMillis();
            System.out.println("第三个任务执行时间是:"+(end-start));
        }
    }
    

    下面创建一个测试类:

    package com.yanfei1819.asynctaskdemo.web.controller;
    
    import com.yanfei1819.asynctaskdemo.task.AsyncTask;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * Created by 追梦1819 on 2019-06-21.
     */
    @RestController
    public class TaskController {
        @Autowired
        private AsyncTask asyncTask;
        @GetMapping("/testTask")
        public void testTask() throws InterruptedException {
            Long start = System.currentTimeMillis();
            asyncTask.task01();
            asyncTask.task02();
            asyncTask.task03();
            Long end = System.currentTimeMillis();
            System.out.println("三个任务执行总时间是:"+(end-start));
        }
    }
    

    最后启动项目,访问 localhost:8080/testTask ,结果如下:

    很明显这是很常规的同步方法。下面我们使用 SpringBoot 自带的异步方法实现方式作异步处理。


    需要改动的地方有两处:

    1. 在启动类的中加注解 @EnableAsync
    2. 在异步的方法上加注解 @Async

    重启项目,访问同一个路径,以下是结果:


    总结

      异步任务的使用跟其他的用法一样很简单,SpringBoot 也将其集成到 Spring 生态圈中,只需要使用其中的几个注解即可。后续将分析其原理。


  • 相关阅读:
    Markdown示例
    初识Markdown
    Ubuntu源整理
    Vi编辑器入门
    Ubuntu,Linux入门系统
    [其他]volatile 关键字
    Qt5:随窗口大小变化背景图片自动缩放的实现
    Qt5:不规则按钮的实现---通过贴图实现
    windows编程:创建DLL
    [转]Firefox拦截12306订票网站的解决办法
  • 原文地址:https://www.cnblogs.com/yanfei1819/p/11095891.html
Copyright © 2011-2022 走看看