使用@Scheduled的注意事项
springboot中的@Scheduled的默认执行器是ThreadPoolTaskScheduler,它是一个poolSize=1的线程池,所以项目中所有的@Scheduled定时任务会串行执行,这显然是会有问题的。此时即使在方法上加入@Async也只是把任务异步提交给到ThreadPoolTaskScheduler去执行,最终还是串行执行
解决方法:自定义TaskScheduled
@Configuration
@EnableAsync
public class ScheduleConfig {
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(20);
return taskScheduler;
}
}
使用@Async时最好指定异步执行器
当使用@Async来做异步操作时,springboot默认的异步执行器是SimpleAsyncTaskExecutor,下面是它doExcute的方法
protected void doExecute(Runnable task) {
Thread thread = this.threadFactory != null ? this.threadFactory.newThread(task) : this.createThread(task);
thread.start();
}
它的核心参数如下:
-
concurrencyLimit:并发限制数,指定了允许同时执行的最大任务数。默认值为Integer.MAX_VALUE。
-
threadNamePrefix:线程名称前缀,用于给异步任务分配线程名称。默认值为"SimpleAsyncTaskExecutor-"。
-
threadPriority:线程优先级,用于设置异步任务所在线程的优先级。默认值为Thread.NORM_PRIORITY。
-
waitForTasksToCompleteOnShutdown:关闭时等待所有任务完成,指定是否在关闭时等待所有正在运行的任务完成。默认值为false。
-
exposeUnconfigurableExecutor:暴露不可配置的执行器,指定是否将SimpleAsyncTaskExecutor实例公开为不可配置的执行器。默认值为true
如果被@Async的方法突然调用量升高,则会导致线程暴涨导致内存溢出等不可挽回的问题
解决方法:自定义线程池
@Configuration
public class TaskExecutorConfig implements AsyncConfigurer {
private static final int CORE_POOL_SIZE = 2;
private static final int MAX_POOL_SIZE = 2;
private static final int QUEUE_CAPACITY = 10;
/**
* 通过重写getAsyncExecutor方法,制定默认的任务执行由该方法产生
*
* 配置类实现AsyncConfigurer接口并重写getAsyncExcutor方法,并返回一个ThreadPoolTaskExevutor
* 这样我们就获得了一个基于线程池的TaskExecutor
*/
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(CORE_POOL_SIZE);
taskExecutor.setMaxPoolSize(MAX_POOL_SIZE);
taskExecutor.setQueueCapacity(QUEUE_CAPACITY);
taskExecutor.initialize();
return taskExecutor;
}
}