1、在自定义线程池中异步
2、springboot支持定时任务线程池
配置文件中设置spring.task.scheduling.pool.size=10(默认是1,所以阻塞)但有些版本有bug不好使。
public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport
implements AsyncListenableTaskExecutor, SchedulingTaskExecutor {
private final Object poolSizeMonitor = new Object();
// 这里
private int corePoolSize = 1;
private int maxPoolSize = Integer.MAX_VALUE;
private int keepAliveSeconds = 60;
private int queueCapacity = Integer.MAX_VALUE;
private boolean allowCoreThreadTimeOut = false;
3、直接让定时任务以异步方式执行 – SpringBoot异步任务
类上@EnableAsync+方法上@Async
(其实就是调用TaskSchedulingAutoConfiguration,属性绑定在TaskExecutionProperties)