spring boot 使用注解@async实现异步调用
来源:互联网 发布:h5小游戏源码 编辑:程序博客网 时间:2024/05/29 19:46
什么是“异步调用”?
“异步调用”对应的是“同步调用”,同步调用指程序按照定义顺序依次执行,每一行程序都必须等待上一行程序执行完成之后才能执行;异步调用指程序在顺序执行时,不等待异步调用的语句返回结果就执行后面的程序。
同步调用
下面通过一个简单示例来直观的理解什么是同步调用:
定义Task类,创建三个处理函数分别模拟三个执行任务的操作,操作消耗时间随机取(10秒内)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package
com.kfit.task;
import
java.util.Random;
import
org.springframework.stereotype.Component;
/**
* 定义3个任务
*
* @version v.0.1
*/
@Component
publicclass Task1 {
//定义一个随机对象.
publicstatic Random random =
new
Random();
//任务一;
publicvoid doTaskOne()
throws
Exception {
System.out.println(
"开始做任务一"
);
longstart = System.currentTimeMillis();
Thread.sleep(random.nextInt(
10000
));
longend = System.currentTimeMillis();
System.out.println(
"完成任务一,耗时:"
+ (end - start) +
"毫秒"
);
}
//任务二;
publicvoid doTaskTwo()
throws
Exception {
System.out.println(
"开始做任务二"
);
longstart = System.currentTimeMillis();
Thread.sleep(random.nextInt(
10000
));
longend = System.currentTimeMillis();
System.out.println(
"完成任务二,耗时:"
+ (end - start) +
"毫秒"
);
}
//任务3;
publicvoid doTaskThree()
throws
Exception {
System.out.println(
"开始做任务三"
);
longstart = System.currentTimeMillis();
Thread.sleep(random.nextInt(
10000
));
longend = System.currentTimeMillis();
System.out.println(
"完成任务三,耗时:"
+ (end - start) +
"毫秒"
);
}
}
编写一个访问方法:
1
2
3
4
5
6
7
8
//测试task1.
@RequestMapping
(
"/task1"
)
public
String task1()
throws
Exception{
task1.doTaskOne();
task1.doTaskTwo();
task1.doTaskThree();
return
"task1"
;
}
运行可以看到类似如下输出:
1
2
3
4
5
6
开始做任务一
完成任务一,耗时:
4156
毫秒
开始做任务二
完成任务二,耗时:
557
毫秒
开始做任务三
完成任务三,耗时:
6171
毫秒
异步调用
上述的同步调用虽然顺利的执行完了三个任务,但是可以看到执行时间比较长,若这三个任务本身之间不存在依赖关系,可以并发执行的话,同步调用在执行效率方面就比较差,可以考虑通过异步调用的方式来并发执行。
在Spring Boot中,我们只需要通过使用@Async注解就能简单的将原来的同步函数变为异步函数,Task类改在为如下模式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package
com.kfit.task;
import
java.util.Random;
import
org.springframework.scheduling.annotation.Async;
import
org.springframework.stereotype.Component;
/**
* 定义3个任务
*
* @version v.0.1
*/
@Component
publicclass Task2 {
//定义一个随机对象.
publicstatic Random random =
new
Random();
//任务一;
@Async
publicvoid doTaskOne()
throws
Exception {
System.out.println(
"开始做任务一"
);
longstart = System.currentTimeMillis();
Thread.sleep(random.nextInt(
10000
));
longend = System.currentTimeMillis();
System.out.println(
"完成任务一,耗时:"
+ (end - start) +
"毫秒"
);
}
//任务二;
@Async
publicvoid doTaskTwo()
throws
Exception {
System.out.println(
"开始做任务二"
);
longstart = System.currentTimeMillis();
Thread.sleep(random.nextInt(
10000
));
longend = System.currentTimeMillis();
System.out.println(
"完成任务二,耗时:"
+ (end - start) +
"毫秒"
);
}
//任务3;
@Async
publicvoid doTaskThree()
throws
Exception {
System.out.println(
"开始做任务三"
);
longstart = System.currentTimeMillis();
Thread.sleep(random.nextInt(
10000
));
longend = System.currentTimeMillis();
System.out.println(
"完成任务三,耗时:"
+ (end - start) +
"毫秒"
);
}
}
为了让@Async注解能够生效,还需要在Spring Boot的主程序中配置@EnableAsync,如下所示:
1
2
3
4
5
@SpringBootApplication
@EnableAsync
publicclass App {
//省略其它代码…
}
编写测试方法:
1
2
3
4
5
6
7
8
//测试task2.
@RequestMapping
(
"/task2"
)
public
String task2()
throws
Exception{
task2.doTaskOne();
task2.doTaskTwo();
task2.doTaskThree();
return
"task2"
;
}
此时可以反复执行单元测试,您可能会遇到各种不同的结果,比如:
1
2
3
4
5
6
7
开始做任务一
开始做任务二
开始做任务三
完成任务三,耗时:
57
毫秒
完成任务二,耗时:
3621
毫秒
完成任务一,耗时:
7419
毫秒
阅读全文
0 0
- spring boot 使用注解@async实现异步调用
- 56. spring boot中使用@Async实现异步调用
- Spring Boot(十)使用@Async实现异步调用
- Spring Boot中使用@Async实现异步调用
- Spring Boot中使用@Async实现异步调用
- 56. spring boot中使用@Async实现异步调用
- Spring Boot中使用@Async实现异步调用
- Spring Boot中使用@Async实现异步调用
- spring boot 学习(十一)使用@Async实现异步调用
- Spring Boot中使用@Async实现异步调用
- Spring Boot中使用@Async实现异步调用
- Spring Boot中使用@Async实现异步调用
- Spring Boot中使用@Async实现异步调用
- 56. spring boot中使用@Async实现异步调用【从零开始学Spring Boot】
- Spring boot 使用异步@Async
- spring boot 集成Async 注解 异步处理
- Spring中@Async注解实现方法的异步调用
- Spring中@Async注解实现“方法”的异步调用
- Oracle 12c 多租户专题|12cR2中PDB内存资源管理
- 序列容器
- Two Sum
- 系统分区
- 使用srvany.exe将任何程序作为Windows服务运行
- spring boot 使用注解@async实现异步调用
- Linux Socket编程(转载)
- Macbook编译安装SAGA
- jQuery--checkbox全选/取消全选
- 数据结构之顺序存储
- 序列容器之Deque
- bash常见命令
- 天天学Linux命令32--/etc/group文件详解
- Unity动画系统 动态添加动画事件