Springboot整合异步任务2

Springboot整合异步任务2

多个互不影响的串行执行的方法改为并行执行
注意:两个方法都在同一个类里面,一个方法调用另一个异步方法时,异步不生效。

一、使用场景

例如:当pre_task1、pre_task2、pre_task3、pre_task4互不相干时顺序执行需要花费560ms

图一在这里插入图片1

若使用异步任务,可更改为以下流程:
在这里插入图片描述
这时总耗时变成单个任务最长耗时300ms

二、串行执行

1.任务方法

四个任务方法,通过Thread.sleep()模拟耗时的任务
示例代码
代码如下(示例):

@Component
public class AsyncTask {
    public String task1() throws InterruptedException {
        long begin = System.currentTimeMillis();
        Thread.sleep(10L);
        long end = System.currentTimeMillis();
        System.out.println("task1耗时:" + (end - begin));
        return "task1"}

    public String task2() throws InterruptedException {
        long begin = System.currentTimeMillis();
        Thread.sleep(100L);
        long end = System.currentTimeMillis();
        System.out.println("task2耗时:" + (end - begin));
        return "task2";
    }

    public String task3() throws InterruptedException {
        long begin = System.currentTimeMillis();
        Thread.sleep(300L);
        long end = System.currentTimeMillis();
        System.out.println("task3耗时:" + (end - begin));
        return "task3";
    }

    public String task4() throws InterruptedException {
        long begin = System.currentTimeMillis();
        Thread.sleep(150L);
        long end = System.currentTimeMillis();
        System.out.println("task4耗时:" + (end - begin));
        return "task4";
    }
}

2.执行

代码如下(示例):

 public void test2() throws InterruptedException, ExecutionException {
        long begin = System.currentTimeMillis();

        String s1 = this.asyncTask.task1();
        String s2 = this.asyncTask.task2();
        String s3 = this.asyncTask.task3();
        String s4 = this.asyncTask.task4();

        long end = System.currentTimeMillis();
        long time = end - begin;
        System.out.println("执行总耗时:" + time);
    }

task1,task2,task3,task4并行运行总耗时:563ms在这里插入图片描述

三、并行执行

1.异步方法

修改任务方法为异步方法
在需要异步执行的方法上加@Async注解,或在类上加@Async注解(该类下所有注解为异步方法)

2.带返回值的异步方法

返回值类型为:Future,Object指任意类型
返回结果:new AysncResult(ObbjectValue);
代码如下(示例):

@Component
@Async // 异步
public class AsyncTask {

    public Future<String> task1() throws InterruptedException {
        long begin = System.currentTimeMillis();
        Thread.sleep(10L);
        long end = System.currentTimeMillis();
        System.out.println("task1耗时:" + (end - begin));
        return new AsyncResult<String>("task1");
    }

    public Future<String> task2() throws InterruptedException {
        long begin = System.currentTimeMillis();
        Thread.sleep(100L);
        long end = System.currentTimeMillis();
        System.out.println("task2耗时:" + (end - begin));
        return new AsyncResult<String>("task2");
    }

    public Future<String> task3() throws InterruptedException {
        long begin = System.currentTimeMillis();
        Thread.sleep(300L);
        long end = System.currentTimeMillis();
        System.out.println("task3耗时:" + (end - begin));
        return new AsyncResult<String>("task3");
    }

    public Future<String> task4() throws InterruptedException {
        long begin = System.currentTimeMillis();
        Thread.sleep(150L);
        long end = System.currentTimeMillis();
        System.out.println("task4耗时:" + (end - begin));
        return new AsyncResult<String>("task4");
    }
}

3.执行

代码如下(示例):
s1.isDone() = true时表示异步任务s1执行完成

  @GetMapping("test1")
    public JsonData test1() throws InterruptedException, ExecutionException {
        long begin = System.currentTimeMillis();

        Future<String> s1 = this.asyncTask.task1();// 异步任务
        Future<String> s2 = this.asyncTask.task2();// 异步任务
        Future<String> s3 = this.asyncTask.task3();// 异步任务
        Future<String> s4 = this.asyncTask.task4();// 异步任务

		// 当task1,task2,task3,task4都执行完成时退出循环
        while(true) {
            if(s1.isDone() && s2.isDone() && s3.isDone() && s4.isDone()){
                break; 
            }
        }
        // 这里可以使用task1,task2,task3,task3返回的结果
        System.out.println(s1.get() + "执行完成");
        System.out.println(s2.get() + "执行完成");
        System.out.println(s3.get() + "执行完成");
        System.out.println(s4.get() + "执行完成");
        long end = System.currentTimeMillis();
        long time = end - begin;
        System.out.println("执行总耗时:" + time);
        return JsonData.success(time);
    }

task1,task2,task3,task4并行运行总耗时:310ms在这里插入图片描述
串行执行耗时563ms,并行执行耗时310ms

总结

串行执行耗时563ms,改为并行执行耗时310ms

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值