聊聊线程池的预热

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

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

相关推荐
折哥的程序人生 · 物流技术专研15 小时前
Java面试85题图解版 · 全系列总目录
java·开发语言·后端·面试·职场和发展
武子康15 小时前
Java-01 深入浅出 MyBatis 入门与核心原理:半自动 ORM 框架详解
java·后端·mybatis
gf132111115 小时前
飞书长连接_事件订阅(接收消息,审批任务状态变更)
开发语言·python·飞书
木易 士心15 小时前
Java 跳出多层循环
java·开发语言·后端
数电发票API15 小时前
数电发票接口对接流程详解:从认证到冲红的完整指南
java
乐观勇敢坚强的老彭15 小时前
day515C++信奥循环嵌套强化03
开发语言·c++
杜子不疼.15 小时前
【C++ AI 大模型接入 SDK】 - 环境搭建
开发语言·数据库·c++
怀旧,15 小时前
【C++项目】负载均衡式在线OJ
开发语言·c++·负载均衡
float_com15 小时前
【java进阶】------ 多线程【上】
java
吴声子夜歌15 小时前
Java——通用容器类
java·容器