聊聊线程池的预热

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

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

相关推荐
星空椰5 小时前
Python 面向对象高级:继承与类定义详解
开发语言·python
橙淮5 小时前
并发编程(六)
java·jvm
拽着尾巴的鱼儿5 小时前
springboot openfeign 自定义feign 接口重试机制
java·spring boot·后端
白露与泡影5 小时前
2026大厂Java面试题大全!牛客网最新版
java·开发语言
凯瑟琳.奥古斯特5 小时前
高阶子查询题目精炼
开发语言·数据库·python·职场和发展·数据库开发
雪度娃娃5 小时前
转向现代C++——在意为改写的函数添加 override
开发语言·c++
EntyIU6 小时前
JVM内存与GC笔记
java·jvm·笔记
XS0301066 小时前
并发编程 六
java·后端
yaoxin5211236 小时前
419. 现代 Java IO 最佳实践 - 写入文本文件
java·windows·python
雪宫街道6 小时前
synchronized 锁的范围:对象锁、类锁与代码块锁
java·jvm·后端·面试