在后端开发中经常遇到一些耗时或者第三方系统调用的情况,我们知道Java程序一般的执行流程是顺序执行(不考虑多线程并发的情况),但是顺序执行的效率肯定是无法达到我们的预期的,这时就期望可以并行执行,常规的做法是使用多线程或线程池,需要额外编写代码实现。在spring3.0后引入了@Async注解,使用该注解可以达到线程池的执行效果,而且在开发上非常简单。

一、概述

springboot是基于spring框架的,在springboot环境下演示@Async注解的使用方式。先看下该注解的定义,

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Async {/*** A qualifier value for the specified asynchronous operation(s).* <p>May be used to determine the target executor to be used when executing this* method, matching the qualifier value (or the bean name) of a specific* {@link java.util.concurrent.Executor Executor} or* {@link org.springframework.core.task.TaskExecutor TaskExecutor}* bean definition.* <p>When specified on a class level {@code @Async} annotation, indicates that the* given executor should be used for all methods within the class. Method level use* of {@code Async#value} always overrides any value set at the class level.* @since 3.1.2*/String value() default "";}

可以看到该注解只有一个属性,那就是value,从注释上知道value指定的是执行该任务的线程池,也就是说我们可以使用子定义的线程池执行我们的任务,而不是系统默认的。在看该注解上的注解,

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented

也就是说该注解可以用在方法和类上。标记在类上表示类中的所有方法都以异步方式执行,也就是提交到线程池执行。

二、详述

上面简单对@Async注解进行了解释,下面看用法。

1、@EnableAsync注解

在springboot中要使用@Async注解必须在springboot启动类上使用@EnableAsync注解,开启@Async注解的自动配置,如下,

package com.example.demo;import com.example.demo.properties.ApplicationPro;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;@SpringBootApplication
@EnableConfigurationProperties({ApplicationPro.class})
//开启@Async注解的自动配置
@EnableAsync
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}

只有在启动类上使用@EnableAsync注解,@Async注解才会生效。

2、@Async注解

上面使用@EnableAsync注解已经开启了对@Async注解的配置,下面看具体的异步调用类,

package com.example.demo.service;import com.example.demo.Student;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;import java.util.concurrent.Future;@Service
@Async
public class AsyncService {public Future<Student> get(){Student stu=new Student("1","3");try {Thread.sleep(10000l);} catch (InterruptedException e) {e.printStackTrace();}return AsyncResult.forValue(stu);}public void  executeRemote(){try {Thread.sleep(10000l);} catch (InterruptedException e) {e.printStackTrace();}}
}

首先,要使该类让spring管理必须使用@Service注解(或其他注解也可以),然后在类上标记@Async注解,前面说过@Async注解可以在方法或类上使用,在类上使用则表示类中的所有方法均使用异步执行的方式。异步执行类中有两个方法,每个方法为了演示执行的耗时操作均睡眠10s。这两个方法一个是有返回值的,另一个是无返回值的,重点看有返回值的,

public Future<Student> get(){Student stu=new Student("1","3");try {Thread.sleep(10000l);} catch (InterruptedException e) {e.printStackTrace();}return AsyncResult.forValue(stu);}

为什么方法的返回值是Future,在@Async注释上有下面这句话,

从上面的注解正好可以说明返回Future是没问题,但是我们的方法就是一个普通的方法,要怎么才能返回Future类那,不慌,spring针对@Async注解提供了AsyncResult类,从类名就知道该类就是为了@Async注解准备的,

使用其中的forValue方法,便可以返回一个带有泛型的Future类了。

看下测试类,

package com.example.demo.controller;import com.example.demo.Student;
import com.example.demo.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
@Controller
@RequestMapping("async")
public class ControllerAsyncTest {@Autowiredprivate AsyncService asyncService;@RequestMapping("/test")@ResponseBodypublic Student get(){try {long start=System.currentTimeMillis();//调用带有返回值的get方法Future<Student> result=asyncService.get();//调用无返回值的executeRemote方法asyncService.executeRemote();Student student=result.get();long end=System.currentTimeMillis();System.out.println("执行时间:"+(end-start));return  student;} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}return null;}
}

测试类就是一个简单的controller,调用了get和executeRemote方法,这两个方法分别会睡眠10s,而且get会有返回值,下面看是否可以拿到get的返回值,并看下调用这两个方法的时间,

可以成功拿到返回值,看执行时间,

2020-12-12 21:37:43.556  INFO 11780 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 5 ms
执行时间:10006

执行时间是10006ms,也就是10s多,按照上面的分析两个方法分别睡眠了10s,如果同步执行那肯定是20s,把@Async注解去掉看执行时间,

2020-12-12 21:41:07.840  INFO 11584 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 5 ms
执行时间:20001

执行时间是20001ms,算上两个方法睡眠的时间,和测试类本身的时间,20001ms是没错的。从这里可以看出@Async注解的作用,把每个方法当作任务提交给了线程池,提高了任务执行的时间。

另外,在获取异步的执行结果使用了下面的方法,

Future<Student> result=asyncService.get();
asyncService.executeRemote();
//获得执行结果
Student student=result.get();

由于在主线程要获得任务的执行结果,使用Future类的get方法获得结果,该结果需要等到任务执行完以后才可以获得。

来源:https://www.tuicool.com/articles/NBjAzmi

spring async 默认线程池_springboot:异步调用@Async相关推荐

  1. @async 默认线程池_Springboot线程池的使用和扩展

    点击上方"Java知音",选择"置顶公众号" 技术文章第一时间送达! 作者:程序员欣宸 https://blog.csdn.net/boling_cavalry ...

  2. @async 默认线程池_springboot@Async默认线程池导致OOM问题

    地址:http://suo.im/5Y3RGF 作者:ignorewho 前言: 最近项目上在测试人员压测过程中发现了OOM问题,项目使用springboot搭建项目工程,通过查看日志中包含信息:un ...

  3. @async 默认线程池_SpringBoot 线程池的使用

      Java大联盟 帮助万千Java学习者持续成长 关注 作者|Muscleheng blog.csdn.net/Muscleheng/article/details/81409672 前言 最近在做 ...

  4. async spring 默认线程池_SpringBoot中Async异步方法和定时任务介绍

    1.功能说明 Spring提供了Async注解来实现方法的异步调用.即当调用Async标识的方法时,调用线程不会等待被调用方法执行完成即返回继续执行以下操作,而被调用的方法则会启动一个独立线程来执行此 ...

  5. async spring 默认线程池_Spring boot注解@Async线程池实例详解

    这篇文章主要介绍了Spring boot注解@Async线程池实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 从Spring3开始提供了@A ...

  6. @async 默认线程池_.NET Web应用中为什么要使用async/await异步编程?

    布莱恩特:.NET Core开发精选文章目录,持续更新,欢迎投稿!​zhuanlan.zhihu.com 前言 1.什么是async/await? await和async是.NET Framework ...

  7. springboot 多线程_SpringBoot异步调用@Async

    一. 什么是异步调用? 异步调用是相对于同步调用而言的,同步调用是指程序按预定顺序一步步执行,每一步必须等到上一步执行完后才能执行,异步调用则无需等待上一步程序执行完即可执行. 二. 如何实现异步调用 ...

  8. python 进程池、线程池 与异步调用、回调机制

    进程池.线程池使用案例 from concurrent.futures import ProcessPoolExecutor # 进程池模块 from concurrent.futures impor ...

  9. async spring 默认线程池_springboot-@Async默认线程池导致OOM问题

    转 springboot-@Async默认线程池导致OOM问题 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 前言: 1.最近项目上在测试人员压 ...

最新文章

  1. NET::ERR_CACHE_MISS
  2. java四种访问权限
  3. ExtJS 折线图趟过的坑
  4. 如何系统性地保障软件性能
  5. 利用连接对象增删改数据 jdbc 20210409235645323
  6. UGUI——基本组件
  7. bzoj2426 [HAOI2010]工厂选址 读题+贪心
  8. 疯狂软件对Oracle放弃Java EE的看法
  9. 通过Spark生成HFile,并以BulkLoad方式将数据导入到HBase
  10. 模式串匹配的BF算法和KMP算法
  11. ubuntu 实现文本方式和图形方式的转换_word格式怎么转成pdf-pdf转换软件_pdf格式转word工具在线免费转换...
  12. 金额要用BigDecimal,原理分析
  13. cad一键卸载工具叫什么_CAD专用卸载修复工具,一键完全彻底卸载删除CAD软件的专用卸载工具...
  14. 网管服务器系统巡检报告,网络设备巡检报告-20210730125215.docx-原创力文档
  15. 水晶报表加载本地图片 【BY阿泰】
  16. IP地址和子网划分学习笔记
  17. 怎样用路由器共享需要网页认证的wifi
  18. 计算机网络机房安置要点,机房平面布置图
  19. 写推文满一年,分享 5 个小编常用的写作软件
  20. 图论典型问题--握手定理

热门文章

  1. [云炬创业管理笔记]第九章为创业成败而准备测试4
  2. [云炬创业基础笔记]第五章创业机会评估测试2
  3. 云炬随笔20160914
  4. netcdf4excel插件安装完出现问题
  5. html 加入li的作用,HTML的li有什么作用?
  6. VTK修炼之道59:图形基本操作进阶_纹理映射
  7. 图像及其表达与性质(上)
  8. 为什么国内软件行业普遍不如国外?
  9. PurgeComm()函数_清空缓冲区 收藏
  10. javascript事件驱动框架 收藏