| | |
| | | import org.springframework.scheduling.annotation.SchedulingConfigurer; |
| | | import org.springframework.scheduling.config.ScheduledTaskRegistrar; |
| | | |
| | | import java.util.concurrent.Executors; |
| | | import java.util.concurrent.*; |
| | | |
| | | @Configuration |
| | | public class ScheduleConfig implements SchedulingConfigurer { |
| | | |
| | | @Override |
| | | public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { |
| | | taskRegistrar.setScheduler(Executors.newScheduledThreadPool(12)); |
| | | //采用定长的线程池,防止定时任务异常时导致大量线程被占用; |
| | | |
| | | // 创建一个 ScheduledThreadPoolExecutor,核心线程数为 12,最大线程数为 24 |
| | | ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(12, new ThreadPoolExecutor.DiscardPolicy()); |
| | | // 设置最大线程数为 50 |
| | | executor.setMaximumPoolSize(24); |
| | | taskRegistrar.setScheduler(executor); |
| | | } |
| | | } |