当前位置:网站首页>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注解
边栏推荐
- Shell编程之循环语句与函数的使用
- typeScript-闭包函数的使用
- 一点点读懂regulator(二)
- MySQL的安装与卸载
- 365天深度学习训练营-学习线路
- 文献阅读十——Detect Rumors on Twitter by Promoting Information Campaigns with Generative Adversarial Learn
- 年薪50W+的测试工程师都在用这个:Jmeter 脚本开发之——扩展函数
- Shell expect 实战案例
- Implementing class target method exception using proxy object execution
- Community Sharing|Tencent Overseas Games builds game security operation capabilities based on JumpServer
猜你喜欢
随机推荐
文章占位 文章占位
亿流量大考(3):不加机器,如何抗住每天百亿级高并发流量?
一点点读懂regulator(四)
【3D建模制作技巧分享】ZBrush纹理贴图怎么导入
【字符串函数内功修炼】strcpy + strcat + strcmp(一)
typeScript-部分应用函数
Web安全开发 | 青训营笔记
吐槽 | 参加IT培训的正确姿势
中国的顶级黑客在国际上是一个什么样的水平?
【游戏建模模型制作全流程】使用ZBrush制作骷髅王
年薪50W+的测试工程师都在用这个:Jmeter 脚本开发之——扩展函数
[Cultivation of internal skills of string functions] strncpy + strncat + strncmp (2)
Service Mesh落地路径
golang打开文件和读写文件
3D建模师为了让甲方爸爸过稿,还可以这么做,就是在赚血汗钱啊
一点点读懂Thremal(二)
2022年全网最全接口自动化测试框架搭建,没有之一
【无标题】
npm基本操作及命令详解
一点点读懂regulator(三)









