怎么实现线程池的预热

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 修饰的方法触发线程池预热(方法所在的对象创建好之后执行)。

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值