Springboot整合异步任务2
多个互不影响的串行执行的方法改为并行执行
注意:两个方法都在同一个类里面,一个方法调用另一个异步方法时,异步不生效。
一、使用场景
例如:当pre_task1、pre_task2、pre_task3、pre_task4互不相干时顺序执行需要花费560ms
图一
若使用异步任务,可更改为以下流程:
这时总耗时变成单个任务最长耗时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