聊聊线程池的预热

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

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

相关推荐
小猫咪怎么会有坏心思呢2 分钟前
华为OD机考-数字游戏-逻辑分析(JAVA 2025B卷)
java·游戏·华为od
Aesopcmc6 分钟前
idea 启动jar程序并调试
java·intellij-idea·jar
鸢时望巧14 分钟前
Shell循环(二)
运维·开发语言
景彡先生21 分钟前
C++ 中的 iostream 库:cin/cout 基本用法
开发语言·c++
vvilkim25 分钟前
Flutter 核心概念:深入理解 StatelessWidget 与 StatefulWidget
开发语言·javascript·flutter
sunly_27 分钟前
Flutter:导航背景固定在顶部,下拉分页布局
开发语言·javascript·flutter
南极Ou35 分钟前
Mybatis逆向工程详解(附源码文件)动态创建实体类、条件扩展类、Mapper接口、Mapper.xml映射文件
xml·java·mybatis
Moshow郑锴41 分钟前
IDEA为何一直无法使用超过4g内存
java·ide·intellij-idea
木头左42 分钟前
Docker容器化镜像分层原理及优化策略
java·eureka
李少兄43 分钟前
IntelliJ IDEA代码提示忽略大小写设置详解
java·ide·intellij-idea