spring boot 学习(十一)使用@Async实现异步调用
使用@Async实现异步调用
什么是”异步调用”与”同步调用”
“同步调用”就是程序按照一定的顺序依次执行,,每一行程序代码必须等上一行代码执行完毕才能执行;”异步调用”则是只要上一行代码执行,无需等待结果的返回就开始执行本身任务。
通常情况下,”同步调用”执行程序所花费的时间比较多,执行效率比较差。所以,在代码本身不存在依赖关系的话,我们可以考虑通过”异步调用”的方式来并发执行。
“异步调用”
在 spring boot 框架中,只要提过@Async注解就能奖普通的同步任务改为异步调用任务。
注意: @Async所修饰的函数不要定义为static类型,这样异步调用不会生效
1. 开启@Async注解
在Spring Boot主类添加@EnableAsync注解
2. 定义异步任务
定义Task类,创建三个处理函数分别模拟三个执行任务的操作,操作消耗时间随机取(10秒内)。
@Component
public class Task { //定义一个随机对象.
public static Random random =new Random(); @Async //加入"异步调用"注解
public void doTaskOne() throws InterruptedException {
System.out.println("开始执行任务一");
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
System.out.println("完成任务一,耗时:" + (end - start) + "毫秒");
} @Async
public void doTaskTwo() throws InterruptedException {
System.out.println("开始执行任务二");
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
System.out.println("完成任务二,耗时:" + (end - start) + "毫秒");
} @Async
public void doTaaskThree() throws InterruptedException {
System.out.println("开始执行任务三");
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
System.out.println("完成任务三,耗时:" + (end - start) + "毫秒");
}
}
3. 创建Controller进行测试
注意@Autowired注入类,因为这个类已经被 Spring 管理了。如果使用 new 来获得线程类将不会执行异步效果,这里涉及到在 Spring 中使用多线程。
@Controller
public class TaskController { @Autowired
private Task TASK; @ResponseBody
@RequestMapping("/task")
public String task() throws Exception {
System.out.println("开始执行Controller任务");
long start = System.currentTimeMillis();
TASK.doTaskOne();
TASK.doTaskTwo();
TASK.doTaaskThree();
long end = System.currentTimeMillis();
System.out.println("完成Controller任务,耗时:" + (end - start) + "毫秒");
return "success";
}
}
4. 多次调用
访问 http://localhost:8080/task 截图:
spring boot 学习(十一)使用@Async实现异步调用的更多相关文章
- spring boot中使用@Async实现异步调用任务
本篇文章主要介绍了spring boot中使用@Async实现异步调用任务,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 什么是“异步调用”? “异步调用”对应的是“同步 ...
- Spring Boot使用@Async实现异步调用
原文:http://blog.csdn.net/a286352250/article/details/53157822 项目GitHub地址 : https://github.com/FrameRes ...
- Spring Boot 学习笔记(六) 整合 RESTful 参数传递
Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...
- Spring Boot学习大全(入门)
Spring Boot学习(入门) 1.了解Spring boot Spring boot的官网(https://spring.io),我们需要的一些jar包,配置文件都可以在下载.添置书签后,我自己 ...
- Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客
==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...
- Spring boot学习1 构建微服务:Spring boot 入门篇
Spring boot学习1 构建微服务:Spring boot 入门篇 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...
- spring boot 学习资料
spring boot 学习资料: 学习资料 网址 Spring Boot Cookbook-极客学院 http://wiki.jikexueyuan.com/project/spring-boot- ...
- Spring Boot学习笔记2——基本使用之最佳实践[z]
前言 在上一篇文章Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用已经对Spring Boot的基本体系与基本使用进行了学习,本文主要目的是更加进一步的来说明对于Spring B ...
- spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战
SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...
随机推荐
- 09: TemplateView , ListView ,DetailView三种常用类视图用法
1.1 视图混合介绍 1.Mixin和View的职能区分 1. Mixin提供数据,View提供模板和渲染,所以一般get_context_data在Mixin中,get(),post(),head( ...
- 20145307陈俊达《网络对抗》Exp 8 Web基础
20145307陈俊达<网络对抗>Exp 8 Web基础 基础问题回答 1.什么是表单? 表单是一个包含表单元素的区域,表单元素是允许用户在表单中输入信息的元素,表单在网页中主要负责数据采 ...
- tensorflow之神经网络实现流程总结
tensorflow之神经网络实现流程总结 1.数据预处理preprocess 2.前向传播的神经网络搭建(包括activation_function和层数) 3.指数下降的learning_rate ...
- VS编译器之间相互打开的技巧
例如:VS2010的工程在VS2012上打开,在工程属性里面 选择“常规” --> "平台工具集中" 选择 正在打开版本的型号.
- Dell Vostro5370安装Win10/Ubuntu18LTS
如何安装Win10/Ubuntu双系统 测试环境: DELL PRECISION 7510: CPU:Intel Core i5-6300HQ HD:256G NVME SSD 操作步骤: 无损将硬盘 ...
- JavaScript:Array属性方法
,,,,]; console.dir(arr); var pro=Object.getPrototypeOf(arr); console.dir(pro); 来一个个的查看数组的属性,方法 1.Arr ...
- The destination you provided is not a full refname (i.e., starting with "refs/")
$ git push v5 v5/hotfix/5.1:hotfix/5.1-quartzerror: The destination you provided is not a full refna ...
- java web项目启动进入首页的配置方式(包含过滤跳转首页实现)
本文为博主原创,未经允许不得转载: 项目启动成功,进入首页的方式,我们往往在web.xml 中通过以下的方式默认进入跳转首页, <welcome-file-list> <welcom ...
- The way to Go(2): 语言的主要特性与发展的环境和影响因素
Reference: Github: Go Github: The way to Go 语言的主要特性与发展的环境和影响因素 现有编程语言对于Go语言发展的影响: Why Go? C/C++ 的发展速 ...
- Ubuntu 14.04 下解决maven访问速度慢问题
参考: maven国内镜像(maven下载慢的解决方法) maven中央仓库访问速度太慢的解决办法 Ubuntu 14.04 下解决maven访问速度慢问题 在启动OVX的时候,由于sh脚本中需要使用 ...