聊聊线程池的预热

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

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

相关推荐
基哥的奋斗历程6 分钟前
Kotlin_Flow_完整使用指南
android·开发语言·kotlin
心之伊始6 分钟前
Java synchronized 锁升级全过程深度解析:从 Mark Word 到偏向锁、轻量级锁与重量级锁的 HotSpot 实现
java·开发语言·word
q***062931 分钟前
搭建Golang gRPC环境:protoc、protoc-gen-go 和 protoc-gen-go-grpc 工具安装教程
开发语言·后端·golang
阿Y加油吧1 小时前
Java SE核心面试题总结——day 01
java
布丁写代码1 小时前
GESP C++ 一级 2025年09月真题解析
开发语言·c++·程序人生·学习方法
2021_fc1 小时前
Flink入门指南:使用Java构建第一个Flink应用
java·大数据·flink
GOTXX1 小时前
用Rust实现一个简易的rsync(远程文件同步)工具
开发语言·后端·rust
诸葛亮的芭蕉扇1 小时前
抓图巡检-底图支持绘制
开发语言·前端·javascript
Java开发追求者1 小时前
vscode导入springboot项目
java·ide·spring boot·vscode
麦烤楽鸡翅1 小时前
坚持60s (攻防世界)
java·网络安全·jar·ctf·misc·反编译·攻防世界