聊聊线程池的预热

本文主要研究一下线程池的预热

prestartCoreThread

java/util/concurrent/ThreadPoolExecutor.java

复制代码
    /**
     * Starts a core thread, causing it to idly wait for work. This
     * overrides the default policy of starting core threads only when
     * new tasks are executed. This method will return {@code false}
     * if all core threads have already been started.
     *
     * @return {@code true} if a thread was started
     */
    public boolean prestartCoreThread() {
        return workerCountOf(ctl.get()) < corePoolSize &&
            addWorker(null, true);
    }

ThreadPoolExecutor定义了prestartCoreThread,用于启动一个核心线程

prestartAllCoreThreads

java/util/concurrent/ThreadPoolExecutor.java

复制代码
    /**
     * Starts all core threads, causing them to idly wait for work. This
     * overrides the default policy of starting core threads only when
     * new tasks are executed.
     *
     * @return the number of threads started
     */
    public int prestartAllCoreThreads() {
        int n = 0;
        while (addWorker(null, true))
            ++n;
        return n;
    }

prestartAllCoreThreads用于启动所有的核心线程

小结

ThreadPoolExecutor提供了prestartCoreThread方法,用于启动一个核心线程,提供了prestartAllCoreThreads方法用于启动所有的核心线程。

相关推荐
一只大袋鼠21 小时前
并发编程(二十二):单例模式:从基础实现到 Spring Web 实战
java·spring·单例模式·并发编程
yzx99101321 小时前
使用Python构建交易回撤分析器:Trad与Claw模块实战
开发语言·python
小王不爱笑13221 小时前
Java Map 三大核心实现类详解:HashMap、TreeMap、Hashtable
java·开发语言·哈希算法
1104.北光c°21 小时前
双令牌机制:让认证更安全、体验更流畅
java·开发语言·笔记·后端·安全·token·双令牌
独自破碎E21 小时前
【面试真题拆解】Java文件操作的异常类型与受检_非受检异常
java·面试·职场和发展
q54314708721 小时前
Spring TransactionTemplate 深入解析与高级用法
java·数据库·spring
工一木子21 小时前
String.format 替换踩坑记:从遇坑、读源码到手写实现
java·jdk源码
江沉晚呤时21 小时前
RabbitMQ 延迟队列实战指南:C# 版订单超时与定时任务解决方案
开发语言·后端·ruby
源码师傅21 小时前
2026最新AI短剧创作系统源码 开发语言:PHP+MYSQL 无限SAAS 含图文搭建教程
开发语言·php·ai短剧创作系统源码·短剧创作系统·短剧创作源码