springboot 异步@Async

同步代码

方法类

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_70在这里插入图片描述

异步代码

启动类添加 @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";
    }

}

输出结果:在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值