聊聊线程池的预热

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

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方法用于启动所有的核心线程。

相关推荐
Rabitebla几秒前
C++11 新特性详解(一):列表初始化、initializer_list 与右值引用
java·开发语言·数据结构·c++·算法·leetcode·list
洋不写bug6 分钟前
JavaComparable、Comparator、Cloneable接口解析,深拷贝浅拷贝详细解析
android·java·开发语言
吴声子夜歌11 分钟前
正则指引——Golang
开发语言·golang·正则表达式
坐吃山猪18 分钟前
WebFlux_学习Subscriber总结
java·开发语言·学习·webflux
白露与泡影27 分钟前
Java面试题及答案整理(2026年金九银十最新版,持续更新)
java·开发语言
少控科技31 分钟前
农场设备管理代码(1)
开发语言·c#
❀搜不到32 分钟前
isat-sam分割导出掩码图
开发语言·python
geovindu42 分钟前
java: Memento Pattern
java·开发语言·后端·备忘录模式·行为模式
xcLeigh1 小时前
Unity基础:Start与Update方法——Unity脚本生命周期初探
java·unity·游戏引擎·教程
zephyr051 小时前
链表实现O(n log n)时间复杂度的排序——归并排序详解
java·数据结构·算法