Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >聊聊线程池的预热

聊聊线程池的预热

原创
作者头像
code4it
发布于 2023-10-23 13:02:53
发布于 2023-10-23 13:02:53
23200
代码可运行
举报
文章被收录于专栏:码匠的流水账码匠的流水账
运行总次数:0
代码可运行

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

prestartCoreThread

java/util/concurrent/ThreadPoolExecutor.java

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    /**
     * 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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    /**
     * 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方法用于启动所有的核心线程。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
线程池原理分析
先给出结论: 1. 当所提交的任务大于核心线程 并且小于最大线程的时候,多余的任务会加入到队列里,等待核心线程执行完成之后从 队列里面拿新的任务执行; 2. 当所提交的任务小于核心线程时,线程会直接执行这批任务; 3. 当所提交的任务大于最大队列+最大线程时,多余的任务会被执行拒绝策略;
简单的程序员
2020/04/20
4260
线程池技术之:ThreadPoolExecutor 源码解析
java中的所说的线程池,一般都是围绕着 ThreadPoolExecutor 来展开的。其他的实现基本都是基于它,或者模仿它的。所以只要理解 ThreadPoolExecutor, 就相当于完全理解了线程池的精髓。
烂猪皮
2021/03/16
3430
线程池技术之:ThreadPoolExecutor 源码解析
Java 多线程(7)----线程池(下)
在上篇文章:Java 多线程—线程池(上) 中我们看了一下 Java 中的阻塞队列,我们知道阻塞队列是一种可以对线程进行阻塞控制的队列,并且在前面我们也使用了阻塞队列来实现 生产者-消费者模型 。在文章最后,我们还看了一下 Future 接口和其中对应的方法,如果你对这些不熟悉,建议先去看一下上一篇文章。有了前面的知识作为基础之后,我们来正式看一下 Java 中的线程池。
指点
2019/01/18
5500
Java 多线程(7)----线程池(下)
Java线程池---addWorker方法解析
以上是addWorker方法的注释,大致意思如下: 该方法是用来创建,运行,清理Workers的。 检查是否一个新的Worker能否能够添加到当前状态以及给定的范围(包括corePoolSize以及maximumSize)的线程池中。如果可以的话,那么Worker的总数会根据添加的Worker来进行调整,并且如果可能的话,一个新的Worker会被创建,并且启动firstTask作为这个Worker的第一个任务。 当该方法返回false的时候,说明这个当前线程处于Stopped状态或者处于shut down状态或者创建线程失败的时候,会返回false。
None_Ling
2018/10/24
1.6K0
Java Review - 线程池使用FutureTask的小坑
线程池使用FutureTask时如果把拒绝策略设置为 DiscardPolicy和 DiscardOldestPolicy,并且在被拒绝的任务的Future对象上调用了无参get方法,那么调用线程会一直被阻塞。
小小工匠
2021/11/22
4970
Java Review - 线程池使用FutureTask的小坑
重写线程池 execute 方法导致线程池“失效” 问题
今天群里有个同学遇到一个看似很奇怪的问题,自定义 ThreadPoolTaskExecutor 子类,重写了 execute 方法,通过 execute 方法来执行任务时打印当前线程,日志显示任务一直在调用者线程里执行 (其实并不是),似乎线程池失效了。
明明如月学长
2022/09/21
5290
线程池 ThreadPoolExecutor 执行的业务流程
An ExecutorService that executes each submitted task using one of possibly several pooled threads, normally configured using Executors factory methods. Thread pools address two different problems: they usually provide improved performance when executing large numbers of asynchronous tasks, due to reduced per-task invocation overhead, and they provide a means of bounding and managing the resources, including threads, consumed when executing a collection of tasks. Each ThreadPoolExecutor also maintains some basic statistics, such as the number of completed tasks. To be useful across a wide range of contexts, this class provides many adjustable parameters and extensibility hooks. However, programmers are urged to use the more convenient Executors factory methods Executors.newCachedThreadPool (unbounded thread pool, with automatic thread reclamation), Executors.newFixedThreadPool (fixed size thread pool) and Executors.newSingleThreadExecutor (single background thread), that preconfigure settings for the most common usage scenarios. Otherwise, use the following guide when manually configuring and tuning this class: Core and maximum pool sizes A ThreadPoolExecutor will automatically adjust the pool size (see getPoolSize) according to the bounds set by corePoolSize (see getCorePoolSize) and maximumPoolSize (see getMaximumPoolSize). When a new task is submitted in method execute(Runnable), and fewer than corePoolSize threads are running, a new thread is created to handle the request, even if other worker threads are idle. If there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created only if the queue is full. By setting corePoolSize and maximumPoolSize the same, you create a fixed-size thread pool. By setting maximumPoolSize to an essentially unbounded value such as Integer.MAX_VALUE, you allow the pool to accommodate an arbitrary number of concurrent tasks. Most typically, core and maximum pool sizes are set only upon construction, but they may also be changed dynami
一个会写诗的程序员
2019/12/11
8090
细说线程池---高级篇
上一篇中已经讲了线程池的原理。这一次来说说源码执行过程。建议先看看细说线程池---入门篇 细说线程池---中级篇
田维常
2020/02/13
4910
细说线程池---高级篇
java线程池(三):ThreadPoolExecutor源码分析
在前面分析了Executors工厂方法类之后,我们来看看AbstractExecutorService的最主要的一种实现类,ThreadpoolExecutor。
冬天里的懒猫
2020/09/18
9110
java线程池(三):ThreadPoolExecutor源码分析
Java-线程池动态修改大小
corePoolSize:核心线程数大小,不管它们创建以后是不是空闲的。线程池需要保持 corePoolSize 数量的线程,除非设置了 allowCoreThreadTimeOut;
茶半香初
2021/11/26
2.7K0
Java-线程池动态修改大小
从入门到精通:Java线程池原理 3W 字长文全面指南
👋 你好,我是 Lorin 洛林,一位 Java 后端技术开发者!座右铭:Technology has the power to make the world a better place.
Lorin 洛林
2023/11/07
1K1
从入门到精通:Java线程池原理 3W 字长文全面指南
ThreadPoolExecutor源码分析(一):重要的成员变量
ThreadPoolExecutor部分重要成员变量: 1、AtomicInteger ctl 2、workQueue 3、corePoolSize 4、maximumPoolSize 5、keepAliveTime 6、handler
100000860378
2018/12/26
4430
Java线程池ThreadPoolExecutor使用和分析(二) - execute()原理
execute()是 java.util.concurrent.Executor接口中唯一的方法,JDK注释中的描述是“在未来的某一时刻执行命令command”,即向线程池中提交任务,在未来某个时刻执行,提交的任务必须实现Runnable接口,该提交方式不能获取返回值。下面是对execute()方法内部原理的分析,分析前先简单介绍线程池有哪些状态,在一系列执行过程中涉及线程池状态相关的判断。以下分析基于JDK 1.7
Janti
2019/05/25
2.5K0
【源码阅读计划】浅析 Java 线程池工作原理及核心源码
在 JDK 的 ThreadPoolExecutor 线程池中用一个原子整型来维护线程池的两个状态参数:
yhlin
2023/02/27
4350
【源码阅读计划】浅析 Java 线程池工作原理及核心源码
Tomcat 线程池学习总结
Tomcat线程池,源于JAVA JDK自带线程池。由于JAVA JDK线程池策略,比较适合处理 CPU 密集型任务,但是对于 I/O 密集型任务,如数据库查询,rpc 请求调用等,不是很友好,所以Tomcat在其基础上进行了扩展。
授客
2022/11/21
8980
Tomcat 线程池学习总结
tomcat的线程池机制
首先先在tomcat官网找到对应的tomcat线程池配置,具体定位在:Tomcat线程池
简单的程序员
2020/05/19
3.8K2
tomcat的线程池机制
Java高并发:线程池源码解析
线程池(Thread Pool)是一种基于池化思想管理线程的工具,一方面避免了处理任务时创建销毁线程开销的代价,另一方面避免了线程数量膨胀导致的过分调度问题,保证了对内核的充分利用。
冰寒火
2023/03/03
4460
线程池源码解读
线程池的在 Java并发中使用最多的一种手段,也是性能和易用性相对来说比较均衡的方式,下面我们就一起探索先线程池的原理。
付威
2023/10/17
1610
线程池源码解读
Java 线程线程池初探
所谓线程池,就是将多个线程放在一个池子里面(所谓池化技术),然后需要线程的时候不是创建一个线程,而是从线程池里面获取一个可用的线程,然后执行我们的任务。线程池的关键在于它为我们管理了多个线程,我们不需要关心如何创建线程,我们只需要关系我们的核心业务,然后需要线程来执行任务的时候从线程池中获取线程。任务执行完之后线程不会被销毁,而是会被重新放到池子里面,等待机会去执行任务。
纯洁的微笑
2019/09/05
8640
Java 线程线程池初探
站在架构的角度思考线程池的设计和原理
默认情况下,即使核心线程最初只是在新任务需要时才创建和启动的,也可以使用方法 prestartCoreThread()或 prestartAllCoreThreads() 对其进行动态重写。
架构探险之道
2023/03/04
5340
站在架构的角度思考线程池的设计和原理
相关推荐
线程池原理分析
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验