欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot 异步@Async

程序员文章站 2022-04-30 20:37:42
...

同步代码

方法类

import org.springframework.stereotype.Component;

/**
 * @author wenhui
 * @description
 * @Date 2020/1/1
 */
@Component
public class AsyncTask {

    public void task1() throws InterruptedException {
        long current1=System.currentTimeMillis();
        Thread.sleep(1000);
        long current2=System.currentTimeMillis();
        System.out.println("task1任务耗时: "+(current2-current1));
    }

    public void task2() throws InterruptedException {
        long current1=System.currentTimeMillis();
        Thread.sleep(2000);
        long current2=System.currentTimeMillis();
        System.out.println("task2任务耗时: "+(current2-current1));
    }

    public void task3() throws InterruptedException {
        long current1=System.currentTimeMillis();
        Thread.sleep(10000);
        long current2=System.currentTimeMillis();
        System.out.println("task3任务耗时: "+(current2-current1));
    }

}

测试类,注意这里Junit无法测试

import com.example.entity.AsyncTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wenhui
 * @description
 * @Date 2020/1/1
 */
@RestController
public class DemoController {

    @Autowired
    AsyncTask asyncTask;

    @GetMapping("/async/test")
    public String asyncTest() {
        long current1 = System.currentTimeMillis();
        try {
            asyncTask.task1();
            asyncTask.task2();
            asyncTask.task3();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long current2 = System.currentTimeMillis();
        System.out.println("三个任务耗时:"+(current2 - current1));
        return "ok";
    }
}

输出结果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200101204739956.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMxNTE5OTg5,size_16,color_FFFFFF,t_70springboot 异步@Async

异步代码

启动类添加 @EnableAsync

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@ComponentScan("com.example")
@EnableAsync
public class RedisdemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(RedisdemoApplication.class, args);
    }

}

方法类添加 @Async

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author wenhui
 * @description
 * @Date 2020/1/1
 */
@Component
public class AsyncTask {

    @Async
    public void task1() throws InterruptedException {
        long current1=System.currentTimeMillis();
        Thread.sleep(1000);
        long current2=System.currentTimeMillis();
        System.out.println("task1任务耗时: "+(current2-current1));
    }

    @Async
    public void task2() throws InterruptedException {
        long current1=System.currentTimeMillis();
        Thread.sleep(2000);
        long current2=System.currentTimeMillis();
        System.out.println("task2任务耗时: "+(current2-current1));
    }

    @Async
    public void task3() throws InterruptedException {
        long current1=System.currentTimeMillis();
        Thread.sleep(10000);
        long current2=System.currentTimeMillis();
        System.out.println("task3任务耗时: "+(current2-current1));
    }

}

测试类:

import com.example.entity.AsyncTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wenhui
 * @description
 * @Date 2020/1/1
 */
@RestController
public class DemoController {

    @Autowired
    AsyncTask asyncTask;

    @GetMapping("/async/test")
    public String asyncTest() {
        long current1 = System.currentTimeMillis();
        try {
            asyncTask.task1();
            asyncTask.task2();
            asyncTask.task3();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long current2 = System.currentTimeMillis();
        System.out.println("三个任务耗时:"+(current2 - current1));
        return "ok";
    }

}

输出结果:springboot 异步@Async

相关标签: Java