SpringBoot ――异步调用Async

匿名 (未验证) 提交于 2019-12-03 00:37:01

什么是异步调用?

异步调用是相对于同步调用而言的,同步调用是指程序按预定顺序一步步执行,每一步必须等到上一步执行完后才能执行,异步调用则无需等待上一步程序执行完即可执行。


如何实现异步调用?

多线程,这是很多人第一眼想到的关键词,没错,多线程就是一种实现异步调用的方式。


在非spring目项目中我们要实现异步调用的就是使用多线程方式,可以自己实现Runable接口或者集成Thread类,或者使用jdk1.5以上提供了的Executors线程池。


StrngBoot中则提供了很方便的方式执行异步调用。

按照官方示例开撸

代码入下

maven依赖:

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>1.5.3.RELEASE</version>
  5. </parent>
  6. <dependencies>
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-web</artifactId>
  10. </dependency>
  11. </dependencies>


启动类:添加@EnableAsync注解

  1. @SpringBootApplication
  2. @EnableAsync
  3. public class Application{
  4. public static void main(String[] args) {
  5. SpringApplication.run(Application.class, args);
  6. }
  7. }


只需在需要异步执行方法上添加@Async注解

  1. @RestController
  2. @RequestMapping("")
  3. public class AsyncTaskController {
  4. @RequestMapping("")
  5. public String doTask() throws InterruptedException{
  6. long currentTimeMillis = System.currentTimeMillis();
  7. this.task1();
  8. this.task2();
  9. this.task3();
  10. long currentTimeMillis1 = System.currentTimeMillis();
  11. return "task任务总耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms";
  12. }
  13. @Async
  14. public void task1() throws InterruptedException{
  15. long currentTimeMillis = System.currentTimeMillis();
  16. Thread.sleep(1000);
  17. long currentTimeMillis1 = System.currentTimeMillis();
  18. System.out.println("task1任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
  19. }
  20. @Async
  21. public void task2() throws InterruptedException{
  22. long currentTimeMillis = System.currentTimeMillis();
  23. Thread.sleep(2000);
  24. long currentTimeMillis1 = System.currentTimeMillis();
  25. System.out.println("task2任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
  26. }
  27. @Async
  28. public void task3() throws InterruptedException{
  29. long currentTimeMillis = System.currentTimeMillis();
  30. Thread.sleep(3000);
  31. long currentTimeMillis1 = System.currentTimeMillis();
  32. System.out.println("task3任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
  33. }
  34. }


main函数运行spirngboot项目,启动完成后浏览器访问:

http://localhost:8080/

控制台:

  1. task1任务耗时:1012ms
  2. task2任务耗时:2009ms
  3. task3任务耗时:3004ms

等了一段浏览器时候输出入下:

task任务总耗时:6002ms 


异步并没有执行!

难道是代码写错了?反复检查了好几遍,并没有发现什么明显错误,想起spring对@Transactional注解时也有类似问题,spring扫描时具有@Transactional注解方法的类时,是生成一个代理类,由代理类去开启关闭事务,而在同一个类中方法调用是在类体内执行的,spring无法截获这个方法调用。

豁然开朗,将异步任务单独放到一个类中,调整代码入下:


Controller

  1. @RequestMapping("")
  2. @RestController
  3. public class AsyncTaskController {
  4. @Autowired
  5. private AsyncTask asyncTask;
  6. @RequestMapping("")
  7. public String doTask() throws InterruptedException{
  8. long currentTimeMillis = System.currentTimeMillis();
  9. asyncTask.task1();
  10. asyncTask.task2();
  11. asyncTask.task3();
  12. long currentTimeMillis1 = System.currentTimeMillis();
  13. return "task任务总耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms";
  14. }
  15. }


异步任务类

  1. @Component
  2. public class AsyncTask {
  3. @Async
  4. public void task1() throws InterruptedException{
  5. long currentTimeMillis = System.currentTimeMillis();
  6. Thread.sleep(1000);
  7. long currentTimeMillis1 = System.currentTimeMillis();
  8. System.out.println("task1任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
  9. }
  10. @Async
  11. public void task2() throws InterruptedException{
  12. long currentTimeMillis = System.currentTimeMillis();
  13. Thread.sleep(2000);
  14. long currentTimeMillis1 = System.currentTimeMillis();
  15. System.out.println("task2任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
  16. }
  17. @Async
  18. public void task3() throws InterruptedException{
  19. long currentTimeMillis = System.currentTimeMillis();
  20. Thread.sleep(3000);
  21. long currentTimeMillis1 = System.currentTimeMillis();
  22. System.out.println("task3任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
  23. }
  24. }


控制台:

  1. task1任务耗时:1012ms
  2. task2任务耗时:2009ms
  3. task3任务耗时:3004ms

访问浏览器结果入下:

task任务总耗时:19ms

异步调用成功!


如何知道三个异步任务什么时候执行完,执行的结果怎样呢?可以采用添加Fature回调方式判断

代码入下:

异步任务类

  1. @Component
  2. public class AsyncTask {
  3. @Async
  4. public Future<String> task1() throws InterruptedException{
  5. long currentTimeMillis = System.currentTimeMillis();
  6. Thread.sleep(1000);
  7. long currentTimeMillis1 = System.currentTimeMillis();
  8. System.out.println("task1任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
  9. return new AsyncResult<String>("task1执行完毕");
  10. }
  11. @Async
  12. public Future<String> task2() throws InterruptedException{
  13. long currentTimeMillis = System.currentTimeMillis();
  14. Thread.sleep(2000);
  15. long currentTimeMillis1 = System.currentTimeMillis();
  16. System.out.println("task2任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
  17. return new AsyncResult<String>("task2执行完毕");
  18. }
  19. @Async
  20. public Future<String> task3() throws InterruptedException{
  21. long currentTimeMillis = System.currentTimeMillis();
  22. Thread.sleep(3000);
  23. long currentTimeMillis1 = System.currentTimeMillis();
  24. System.out.println("task3任务耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms");
  25. return new AsyncResult<String>("task3执行完毕");
  26. }
  27. }

Controller

  1. @RequestMapping("")
  2. @RestController
  3. public class AsyncTaskController {
  4. @Autowired
  5. private AsyncTask asyncTask;
  6. @RequestMapping("")
  7. public String doTask() throws InterruptedException{
  8. long currentTimeMillis = System.currentTimeMillis();
  9. Future<String> task1 = asyncTask.task1();
  10. Future<String> task2 = asyncTask.task2();
  11. Future<String> task3 = asyncTask.task3();
  12. String result = null;
  13. for (;;) {
  14. if(task1.isDone() && task2.isDone() && task3.isDone()) {
  15. // 三个任务都调用完成,退出循环等待
  16. break;
  17. }
  18. Thread.sleep(1000);
  19. }
  20. long currentTimeMillis1 = System.currentTimeMillis();
  21. result = "task任务总耗时:"+(currentTimeMillis1-currentTimeMillis)+"ms";
  22. return result;
  23. }
  24. }


控制台输出:

  1. task1任务耗时:1000ms
  2. task2任务耗时:2001ms
  3. task3任务耗时:3001ms

浏览器输出:

task任务总耗时:4015ms 



异步调用成功,并且在所有任务都完成时程序才返回了结果!

文章转载自:https://blog.csdn.net/v2sking/article/details/72795742

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!