1.业务中遇到一些场景: 做一个告警策略的生成的逻辑,需要对用户定义的告警信息进行循环判断,业务要求1000条要在15秒内处理完成.通过日志打印单条生成耗时0.08秒左右,粗略估算,单线程1秒能处理10条,如果有10个线程同时处理的话,那么理想状态10秒内是可以完成的;
在启动类中设置初始化线程,启动类 中使用@EnableAsync 开启异步支持
/** Set the ThreadPoolExecutor's core pool size. */
private int corePoolSize = 10;
/** Set the ThreadPoolExecutor's maximum pool size. */
private int maxPoolSize = 20;
/** Set the capacity for the ThreadPoolExecutor's BlockingQueue. */
private int queueCapacity = 200;
@Bean ("threadPoolTaskExecutor")
public TaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setThreadNamePrefix("AlarmExecutor-");
executor.setKeepAliveSeconds(60);
// rejection-policy&