聊聊线程池的预热

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

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

相关推荐
BUG研究员_15 小时前
Runnable与LCEL
开发语言·人工智能·python
码农颜15 小时前
5.4.1 锁分类
java·数据库·mysql
牛艺翔16 小时前
C++基础
开发语言·c++
键盘会跳舞16 小时前
C++ :容器适配器stack源码级拆解
开发语言·c++··stack·先进后出
linux-hzh16 小时前
百日算法修炼 · Day 03
java·算法
美味蛋炒饭.17 小时前
Git 版本控制(下)
开发语言·git·学习·总结·后端开发
我星期八休息17 小时前
网络编程—网络层
开发语言·前端·网络·人工智能·智能路由器
问商十三载17 小时前
RAG 检索效果差怎么排查?2026 五层诊断法完整指南
开发语言·人工智能·windows·python·算法
不负岁月无痕17 小时前
简单理解操作系统结构
java·linux·c语言·开发语言·c++·面试
mister_guo17 小时前
Java线程池参数应该如何设置(ThreadPoolExecutor)
java