@Schedule定时任务和异步注解@Async时推荐自定义线程池

1.原因

@Schedule定时任务和异步注解@Async使用的默认线程池时, 池中允许的最大线程数和最大任务等待队列都是Integer.MAX_VALUE

2.解决

2.1、可以手动异步编排,交给某个线程池来执行。

首先我们先向Spring中注入一个我们自己编写的线程池,参数自己设置即可,我这里比较随意。

复制代码
 @Configuration
 public class MyTheadPoolConfig {
 
     @Bean
     public TaskExecutor taskExecutor() {
         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
         //设置核心线程数
         executor.setCorePoolSize(10);
         //设置最大线程数
         executor.setMaxPoolSize(20);
         //缓冲队列200:用来缓冲执行任务的队列
         executor.setQueueCapacity(200);
         //线程活路时间 60 秒
         executor.setKeepAliveSeconds(60);
         //线程池名的前缀:设置好了之后可以方便我们定位处理任务所在的线程池
         // 这里我继续沿用 scheduling 默认的线程名前缀
         executor.setThreadNamePrefix("nzc-create-scheduling-");
         //设置拒绝策略
         executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
         executor.setWaitForTasksToCompleteOnShutdown(true);
         return executor;
     }
 
 }
复制代码

然后在定时任务这里注入进去:

复制代码
 @Slf4j
 @Component
 @EnableScheduling
 public class ScheduleService {
 
     @Autowired
     TaskExecutor taskExecutor;
 
     @Scheduled(cron = "0/5 * * * * ? ")
     public void testSchedule() {
         CompletableFuture.runAsync(()->{
             try {
                 Thread.sleep(10000);
                 log.info("当前执行任务的线程号ID===>{}", Thread.currentThread().getId());
             } catch (Exception e) {
                 e.printStackTrace();
             } 
         },taskExecutor);
     }
 }
复制代码

2.2、将定时任务加上异步注解@Async,将其改为异步的定时任务,另外自定义一个系统通用的线程池,让异步任务使用该线程执行任务~

异步定时任务其实和上面的方式原理是一样的,不过实现稍稍不同罢了。

在定时任务的类上再加一个@EnableAsync注解,给方法添加一个@Async即可。

不过一般@Async都会指定线程池,比如写成这样@Async(value = "taskExecutor"),

复制代码
 @Slf4j
 @Component
 @EnableAsync
 @EnableScheduling
 public class ScheduleService {
 
     @Autowired
     TaskExecutor taskExecutor;
 
     @Async(value = "taskExecutor")
     @Scheduled(cron = "0/5 * * * * ? ")
     public void testSchedule() {
             try {
                 Thread.sleep(10000);
                 log.info("当前执行任务的线程号ID===>{}", Thread.currentThread().getId());
             } catch (Exception e) {
                 e.printStackTrace();
             } 
     }
 }
复制代码

 

posted @   佳沃  阅读(111)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示