JAVA使用@Async 注解后执行报错 :org.springframework.core.task.TaskRejectedException: Executo[Running, pool size = 5, active threads = 5, ]] aop.interceptor.AsyncExecutionInterceptor

 

org.springframework.core.task.TaskRejectedException: Executor [java.util.concurrent.ThreadPoolExecutor@31e1ddbc[Running, pool size = 5, active threads = 5, queued tasks = 10, completed tasks = 45]] did not accept task: org.springframework.aop.interceptor.AsyncExecutionInterceptor$$Lambda$960/1248130858@7a0c0aa3

这个是因为执行的数量超过@Async 注解的数量了,

 

建一个配置类 声明线程池

@Configuration
public class SpringAsyncConfig {

    @Bean
    public Executor asyncTaskExecutor() {
        // 创建线程池
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5); // 设置核心池大小
        executor.setMaxPoolSize(10); // 设置最大池大小,只有在缓冲队列满了之后才会申请超过核心线程数的线程
        executor.setQueueCapacity(100); // 设置队列容量
        executor.setKeepAliveSeconds(60); // 设置保持活动秒数,当超过了核心线程数之外的线程在空闲时间到达之后会被销毁
        executor.setThreadNamePrefix("async-"); // 设置线程名称前缀
        // 设置拒绝的执行处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }

}

 

然后方法执行的使用调用

@Async("asyncTaskExecutor")
    public void sync() {
       
        log.info("异步执行逻辑");
       
}

 

posted @ 2025-01-21 13:48  yvioo  阅读(55)  评论(0编辑  收藏  举报