SpringBoot CHAPTER 18: asynchronous tasks

Author: Dream 1819
Original: https://blog.csdn.net/weixin_39759846/article/details/93858446
Copyright: This article is a blogger original article, reproduced, please attach Bowen link!

 

introduction

  Asynchronous tasks system has many advantages, for example: under 1) I / O limited, etc., can provide asynchronous; 2) enhance robustness of the system; 3) improve the user experience.

  But there are some drawbacks, for example: 1) if misused will affect system performance; 2) with respect to programming, synchronization, increase the difficulty of programming.

  Advantages and disadvantages of asynchronous tasks in this chapter does not do too much explain, whether to use asynchronous task, to be determined according to business systems. This chapter deals only with SpringBoot asynchronous tasks.

 

Version Information

  • JDK:1.8

  • SpringBoot :2.1.4.RELEASE

  • maven:3.3.9

  • IDEA:2019.1.1

 

Asynchronous tasks

  About the use of asynchronous tasks, it shows the results may not be obvious. Let's create a few conventional method, and then make asynchronous, look at the difference between the two results.

First create a project, the introduction of web-maven-dependent:

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

 

Then create a few commonly used methods:

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));
    }
}

 

Create a test class the following:

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));
    }
}

 

Finally start the project, visit  localhost:8080/testTask the following results:

Obviously this is a very conventional synchronization method. Here we use SpringBoot own asynchronous method implementations for asynchronous processing.

 

We need to change where there are two:

  1. In addition to start the class notes  @EnableAsync ;
  2. Add annotations on the asynchronous method  @Async.

Restart the project, access to the same path, the following are the results:

 

to sum up

  Using asynchronous task is very simple, like with other uses, SpringBoot will also integrate it into the Spring ecosystem, we only need to use a few of them to comment. Follow-up will analyze its principles.

 

Guess you like

Origin blog.csdn.net/weixin_39759846/article/details/93858446