当前位置:网站首页>The role of @Async annotation and how to implement asynchronous listening mechanism
The role of @Async annotation and how to implement asynchronous listening mechanism
2022-08-04 23:19:00 【burst】
使用@Async标注在方法上,可以使该方法异步的调用执行.而所有异步方法的实际执行是交给TaskExecutor的.
Annotate on classes that need to be executed [email protected],比如,We now have an event to listen for
public class HelloEven extends ApplicationEvent {
private String name;
public HelloEven(Object source,String name) {
super(source);
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Asynchronous listener implementation
@Component
public class HelloListen {
@EventListener
// Defined thread pool name
@Async("taskExecutor")
public void run(HelloEven even) {
System.out.println("==== " + Thread.currentThread().getName() + " ====" + even.getName());
}
}
// 输出结果
// ==== Async-Service-1 ====hello,baby
定义线程池
/** * 默认情况下,在创建了线程池后,线程池中的线程数为0,当有任务来之后,就会创建一个线程去执行任务, * 当线程池中的线程数目达到corePoolSize后,就会把到达的任务放到缓存队列当中; * 当队列满了,就继续创建线程,当线程数量大于等于maxPoolSize后,开始使用拒绝策略拒绝 */
@Configuration
@EnableAsync
public class ThreadPoolTaskConfig {
/** * 核心线程数(默认线程数) */
private static final int corePoolSize = 20;
/** * 最大线程数 */
private static final int maxPoolSize = 100;
/** * 允许线程空闲时间(单位:默认为秒) */
private static final int keepAliveTime = 10;
/** * 缓冲队列大小 */
private static final int queueCapacity = 200;
/** * 线程池名前缀 */
private static final String threadNamePrefix = "Async-Service-";
@Bean("taskExecutor") // bean的名称,默认为首字母小写的方法名
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setKeepAliveSeconds(keepAliveTime);
executor.setThreadNamePrefix(threadNamePrefix);
// 线程池对拒绝任务的处理策略
// CallerRunsPolicy:由调用线程(提交任务的线程)处理该任务
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 初始化
executor.initialize();
return executor;
}
}
如下方式会使@Async失效
- 异步方法使用static修饰
- 异步类没有使用@Component注解(或其他注解)导致spring无法扫描到异步类
- 异步方法不能与被调用的异步方法在同一个类中
- 类中需要使用@Autowired或@Resource等注解自动注入,不能自己手动new对象
- 如果使用SpringBoot框架必须在启动类中增加@EnableAsync注解
边栏推荐
- Implementing class target method exception using proxy object execution
- truffle
- [Paper Notes KDD2021] MixGCF: An Improved Training Method for Graph Neural Network-based Recommender Systems
- SRv6网络的安全解决方案
- 零基础如何入门软件测试?再到测开(小编心得)
- 365天深度学习训练营-学习线路
- 【3D建模制作技巧分享】zbrush贴图映射小技巧
- [Cultivation of internal skills of string functions] strlen + strstr + strtok + strerror (3)
- Go 语言快速入门指南:什么是 TSL 安全传输层
- C5750X7R2E105K230KA(电容器)MSP430F5249IRGCR微控制器资料
猜你喜欢
随机推荐
2022年全网最全接口自动化测试框架搭建,没有之一
typeScript-promise
逆序对的数量
@Import注解的作用以及如何使用
生产者消费者问题
线性DP(下)
2022/8/3
SQL Server calls WebService
The Go Programming Language (Introduction)
js中小数四则运算精度问题原因及解决办法
自从新来了个字节20K出来的,就见识到了什么是天花板
node中package解析、npm 命令行npm详解,node中的common模块化,npm、nrm两种方式查看源和切换镜像
MySQL增删改查基础
MYS-6ULX-IOT 开发板测评——使用 Yocto 添加软件包
Qt中的常用控件
The market value of 360 has evaporated by 390 billion in four years. Can government and enterprise security save lives?
MySQL基础篇【聚合函数】
地面高度检测/平面提取与检测(Fast Plane Extraction in Organized Point Clouds Using Agglomerative Hierarchical Clu)
测试技术:关于上下文驱动测试的总结
OPENCV学习DAY8