@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(); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?