聊聊线程池的预热

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

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

相关推荐
Sunshine for you几秒前
高性能压缩库实现
开发语言·c++·算法
Chase_______1 分钟前
【2026最新保姆级】Python 安装与PyCharm安装配置指南 (Window版)
开发语言·python·pycharm
Sunshine for you2 分钟前
C++中的表达式模板
开发语言·c++·算法
qwehjk20082 分钟前
C++中的状态模式
开发语言·c++·算法
csbysj20204 分钟前
XSLT `<sort>` 元素详解
开发语言
杰克尼4 分钟前
知识点总结--day07(Spring-MVC框架)
java·spring·mvc
沐知全栈开发4 分钟前
Bootstrap 按钮
开发语言
第一程序员5 分钟前
GitHub Copilot:Python开发者的AI助手
开发语言·python·github
2401_873544925 分钟前
Python深度学习入门:TensorFlow 2.0/Keras实战
jvm·数据库·python
xyq20245 分钟前
SQL LEN() 函数详解
开发语言