当前位置:网站首页>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注解
边栏推荐
- panic: reflect: reflect.Value.SetString using value obtained using unexported field
- 基于深度学习的路面坑洞检测(详细教程)
- 2022年全网最全接口自动化测试框架搭建,没有之一
- npm基本操作及命令详解
- Laravel 实现redis分布式锁
- 年薪50W+的测试工程师都在用这个:Jmeter 脚本开发之——扩展函数
- 【3D建模制作技巧分享】Maya模型如何导入zbrush
- kernel问题定位手段总结
- OPENCV学习DAY8
- 文献阅读十——Detect Rumors on Twitter by Promoting Information Campaigns with Generative Adversarial Learn
猜你喜欢

功耗控制之DVFS介绍
![[Mock Interview - 10 Years of Work] Are more projects an advantage?](/img/fa/2652629d1ff4653aca0d626ac89bf8.jpg)
[Mock Interview - 10 Years of Work] Are more projects an advantage?

【3D建模制作技巧分享】在zbrush中如何雕刻头发 ZBrush头发雕刻小技巧

MySQL基础篇【聚合函数】

【字符串函数内功修炼】strcpy + strcat + strcmp(一)

养殖虚拟仿真软件提供高沉浸式的虚拟场景互动操作体验学习

现在学习次世代3D游戏建模还能找到高薪好工作吗

堪称奔驰“理财产品”,空间媲美宝马X5,采用了非常运动的外观

中国的顶级黑客在国际上是一个什么样的水平?

Pytorch分布式训练/多卡/多GPU训练DDP的torch.distributed.launch和torchrun
随机推荐
enumerate()函数
「津津乐道播客」#397 厂长来了:怎样用科技给法律赋能?
PHP(3)
Nuclei(二)进阶——深入理解workflows、Matchers和Extractors
365天深度学习训练营-学习线路
[Cultivation of internal skills of string functions] strncpy + strncat + strncmp (2)
NebulaGraph v3.2.0 Release Note,对查询最短路径的性能等多处优化
Literature reading ten - Detect Rumors on Twitter by Promoting Information Campaigns with Generative Adversarial Learn
MySQL基础篇【聚合函数】
Shell expect real cases
Ab3d.PowerToys and Ab3d.DXEngine Crack
TypeScript - the use of closure functions
堪称奔驰“理财产品”,空间媲美宝马X5,采用了非常运动的外观
【转载】kill掉垃圾进程(在资源管理器占用的情况下)
[Cultivation of internal skills of string functions] strcpy + strcat + strcmp (1)
PZK学C语言之字符串函数(一)
Reconfigure the ffmpeg plugin in chrome
赶紧进来!!!教你C语言实现扫雷小游戏(文章最后有源码!!!)
360市值四年蒸发3900亿,政企安全能救命吗?
d枚举生成位