聊聊线程池的预热

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

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

相关推荐
望眼欲穿的程序猿3 分钟前
Vscode Clangd 无法索引 C++17 或者以上标准
java·c++·vscode
Lenyiin5 分钟前
《Python 修炼全景指南:一》从环境搭建到第一个程序
开发语言·python
带刺的坐椅13 分钟前
Spring-AI 与 Solon-AI 深度对比分析报告
java·spring·ai·llm·solon·spring-ai·solon-ai
爱码少年21 分钟前
JAVA获取客户端真实IP地址经典写法与Lambda写法对比
java
涛声依旧3931624 分钟前
Python项目实战:学生信息管理系统
开发语言·python·数据挖掘
做个文艺程序员28 分钟前
Spring AI + Qwen3.5 实现多步 Agent:从工具调用到自主任务拆解的踩坑全记录
java·人工智能·spring
gentle_ice35 分钟前
初入社会的我该何去何从
java
企鹅的蚂蚁1 小时前
【ESP32-S3开发踩坑】C++野指针引发的LoadProhibited死机与CMake依赖锁死排查
开发语言·c++
XiaoQiao6669991 小时前
python 简单题目练手【详解版】【1】
开发语言·python
Kiling_07041 小时前
Java多态、final与抽象类:面向对象编程进阶指南
java·开发语言