美文网首页
聊聊线程池的预热

聊聊线程池的预热

作者: go4it | 来源:发表于2023-10-22 20:59 被阅读0次

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

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

    相关文章

      网友评论

          本文标题:聊聊线程池的预热

          本文链接:https://www.haomeiwen.com/subject/zxddidtx.html