spring boot 异步@Async使用线程池

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&
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值