1 概述
线程池被创建时,只维护了核心线程数、最大线程数等参数,线程对象不会被创建。那么怎么实现线程池的预热呢?即怎么实现线程池中的线程对象在业务任务提到线程池之前就已经被创建好了?
2 线程池预热
实现线程池的预热,关键点是可以在服务启动过程中向线程池提交指定数量的空任务,从而达到提前创建线程对象的目的。具体实现案例如下所示。
2.1 创建线程池
创建线程池工具类。
@Slf4j
public class ThreadPoolUtils {
private static final int CORE_SIZE = Runtime.getRuntime().availableProcessors() * 2;
private static final int MAX_SIZE = CORE_SIZE + 10;
private static final int WORK_QUEUE_CAPACITY = 10000;
private static final long KEEP_ALIVE_TIME = 120;
private static final TimeUnit SECOND = TimeUnit.SECONDS;
private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_SIZE, MAX_SIZE, KEEP_ALIVE_TIME, SECOND,
new LinkedBlockingDeque<>(WORK_QUEUE_CAPACITY));
/**
* 提交任务到线程池
*/
public static void submitTask(String taskDesc, Runnable task) {
log.info("异步任务[{}]提交线程池....", taskDesc);
try {
executor.execute(task);
} catch (Exception e) {
log.error("异步任务[{}]提交线程池失败,error:", taskDesc, e);
}
}
public static int getCoreSize() {
return CORE_SIZE;
}
public static ThreadPoolExecutor getThreadPoolExecutor() {
return executor;
}
}
2.2 向线程池提交空任务
在服务启动过程中向线程池提交空任务。
@Slf4j
@Component
public class ThreadPoolPreHeat implements ApplicationRunner {
/**
* 通过向线程池提交空任务来进行线程池预热
*
* @param args
* @throws Exception
*/
@Override
public void run(ApplicationArguments args) throws Exception {
log.info("线程池预热前,当前线程对象的数量为:{}", ThreadPoolUtils.getThreadPoolExecutor().getPoolSize());
for (int i = 0; i < ThreadPoolUtils.getCoreSize() / 2; i++) {
ThreadPoolUtils.submitTask("线程池预热-" + i, () -> {});
}
log.info("线程池预热后,当前线程对象的数量为:{}", ThreadPoolUtils.getThreadPoolExecutor().getPoolSize());
}
}
Spring Boot项目可以通过创建实现了 ApplicationRunner 或 CommandLineRunner 的类来触发线程池预热(服务启动后执行)。
所有Spring项目可以通过用 @PostConstruct 修饰的方法触发线程池预热(方法所在的对象创建好之后执行)。