聊聊线程池的预热

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

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

相关推荐
caimouse8 分钟前
ReactOS 窗口系统分析(5):可见区域与窗口 DC — vis.c + windc.c
c语言·开发语言
OPEN-F15 分钟前
Python进阶教程:装饰器与生成器深入
开发语言·python
Zane199416 分钟前
HashMap 为什么要在长度16、容量必须是2的幂这些细节上较劲
java·后端
长谷深风11130 分钟前
为什么你的 Tool 总被模型选错?
java·大数据·ai·llm·ai agent·工具设计·agent设计
千里码aicood1 小时前
基于Python的电商数据采集系统(大数据+爬虫)
开发语言·python
莫得感情 o2 小时前
并发 14 · 收官:虚拟线程与结构化并发
java·并发
聚美智数2 小时前
图片水印-图片剪裁-图片缩放API接口介绍
java·服务器·数据库
陈皮波比茶2 小时前
Python项目实战-网络机器人(爬虫)
开发语言·网络·爬虫·python·机器人
xierui1231233 小时前
AI Agent 隐私架构:本地化重点为什么是登录态与执行权限
java·人工智能·网络安全·架构
cfm_29143 小时前
Spring AI Tool 调用架构全解
java·人工智能·spring