线程池基本使用和ThreadPoolExecutor核心原理讲解
原文地址:https://www.jianshu.com/p/ec5b8cccd87d
java和spring都提供了线程池的框架
java提供的是Executors;
spring提供的是ThreadPoolTaskExecutor;
一、基本使用
Executors提供了4个线程池,
- FixedThreadPool
- SingleThreadExecutor
- CachedThreadPool
- ScheduledThreadPool
FixedThreadPool-固定线程池
public class FixPool {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(5);
for (int i =0;i<10;i++){
executorService.execute(()->{
Thread wt = Thread.currentThread();
String format = String.format("当前线程名称:%s,当前时间:%s", wt.getName(), LocalDateTime.now());
System.out.println(format);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
executorService.shutdown();
}
}
效果如下:

可以看到5个线程在接任务,接完5个任务之后就停止了1秒,完成之后继续接任务;
SingleThreadExecutor-单一线程池,线程数为1的固定线程池
为什么要创造这么一个线程池出来呢?
因为有些时候需要用到线程池的队列任务机制,又不想多线程并发。此时就需要用单一线程池了。
以下两种写法完全一样的效果
ExecutorService executorService = Executors.newSingleThreadExecutor();
ExecutorService executorService = Executors.newFixedThreadPool(1);
CachedThreadPool-缓存线程池,线程数为无穷大的一个线程池
当有空闲线程的时候就让空闲线程去做任务;
当没空闲线程的时候就新建一个线程去任务;
public class CashPool {
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i =0;;i++){
executorService.execute(()->{
Thread wt = Thread.currentThread();
String format = String.format("缓存线程池,当前线程名称:%s,当前时间:%s", wt.getName(), LocalDateTime.now());
System.out.println(format);
try {
double d = Math.random();
int sleep = (int)(d*5000);
Thread.sleep(sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread.sleep(100);
}
}
}
效果如下:

由于任务耗时不确定,所以线程池会动态根据情况去判断是否创建新的线程;
ScheduledThreadPool-调度线程池,线程会根据一定的时间规律去消化任务
分别有3个
- schedule(固定延时才执行任务)
- scheduleAtFixedRate(一定的间隔执行一次任务,执行时长不影响间隔时间)
- scheduleWithFixedDelay(一定的间隔执行一次任务,从任务执行接触才开始计算,执行时长影响间隔时间)
public class ScheduledPool {
public static void main(String[] args) {
ScheduledExecutorService pool = Executors.newScheduledThreadPool(5);
String begin = String.format("调度线程池-begin,当前时间:%s", LocalDateTime.now());
System.out.println(begin);
// schedule(pool);
scheduleAtFixedRate(pool);
// scheduleWithFixedDelay(pool);
}
private static void scheduleAtFixedRate(ScheduledExecutorService pool){
pool.scheduleAtFixedRate(()->{
Thread wt = Thread.currentThread();
String format = String.format("scheduleAtFixedRate-调度线程池,当前线程名称:%s,当前时间:%s", wt.getName(), LocalDateTime.now());
System.out.println(format);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
},1,1, TimeUnit.SECONDS);
}
private static void scheduleWithFixedDelay(ScheduledExecutorService pool){
pool.scheduleWithFixedDelay(()->{
Thread wt = Thread.currentThread();
String format = String.format("scheduleWithFixedDelay-调度线程池,当前线程名称:%s,当前时间:%s", wt.getName(), LocalDateTime.now());
System.out.println(format);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
},1,1, TimeUnit.SECONDS);
}
private static void schedule(ScheduledExecutorService pool){
for (int i =0;i<10;i++){
pool.schedule(()->{
Thread wt = Thread.currentThread();
String format = String.format("调度线程池,当前线程名称:%s,当前时间:%s", wt.getName(), LocalDateTime.now());
System.out.println(format);
},5, TimeUnit.SECONDS);
}
pool.shutdown();
}
}
二、原理讲解
上面介绍的4个线程池工具,都是基于一个类ThreadPoolExecutor
ThreadPoolExecutor有几个重要的参数
- corePoolSize,核心线程数
- maximumPoolSize,最大线程数
- keepAliveTime,线程空闲时间,和4组合使用
- unit
- workQueue,工作队列,是各个工具的机制策略的核心
- threadFactory ,生成线程的工厂,可以代理run方法,还有给thread命名
- handler,拒绝策略,当任务太多的时候会执行的方法,框架有4个实现好的策略,可以自己写自己的策略。
对于fixPool线程池,corePoolSize=maximumPoolSize=n,keepAliveTime=0,workQueue=LinkedBlockingQueue,threadFactory和handler都是默认的。

对于cashPool线程池,corePoolSize=0,maximumPoolSize=2^32,keepAliveTime=60s,workQueue=SynchronousQueue,threadFactory和handler都是默认的。

我们先看一看execute方法

其中ctl参数是一个核心参数,保存着线程池的运行状态和线程数量,通过workerCountOf()获取当前的工作线程数量。
execute整个过程分成3个部分。
- 当工作线程数少于核心线程数,创建一个工作线程去消化任务。
- 当线程池在运行状态而且能把任务放到队列,则接受任务,调用addWorker让线程去消化队列的任务。
- 让线程获取消化任务失败,拒绝任务。

核心一 workQueue.offer
对于fixPool,由于workQueue是LinkedBlockingQueue,所以offer方法基本会返回true。
对于cashpool,workQueue是SynchronousQueue,如果没有消费者在take,则会立马返回false,然后立马新建一个线程。
核心二 getTask
每个线程都被包装成worker对象,worker对象会执行自己的runWorker方法,方法在死循环不停得调用getTask方法去消化任务。

getTask里面最核心的是take和poll方法,这个是跟你传入的队列特性有关。

对于spring提供的ThreadPoolTaskExecutor,其实也是对ThreadPoolExecutor的一个封装。
具体看initializeExecutor方法

在执行execute方法的时候,也是执行ThreadPoolExecutor的execute方法。
结论,线程池的核心是ThreadPoolExecutor类,根据传入的workQueue的offer、poll、take方法特性的不同而诞生缓存线程池,固定线程池,调度线程等各种线程策略。
github地址:https://github.com/hd-eujian/threadpool.git
码云地址: https://gitee.com/guoeryyj/threadpool.git
线程池基本使用和ThreadPoolExecutor核心原理讲解的更多相关文章
- Java线程池的使用方式,核心运行原理、以及注意事项
为什么需要线程池 java中为了提高并发度,可以使用多线程共同执行,但是如果有大量线程短时间之内被创建和销毁,会占用大量的系统时间,影响系统效率. 为了解决上面的问题,java中引入了线程池,可以使创 ...
- 并发编程系列:Java线程池的使用方式,核心运行原理、以及注意事项
并发编程系列: 高并发编程系列:4种常用Java线程锁的特点,性能比较.使用场景 线程池的缘由 java中为了提高并发度,可以使用多线程共同执行,但是如果有大量线程短时间之内被创建和销毁,会占用大量的 ...
- JDK ThreadPoolExecutor核心原理与实践
一.内容概括 本文内容主要围绕JDK中的ThreadPoolExecutor展开,首先描述了ThreadPoolExecutor的构造流程以及内部状态管理的机理,随后用大量篇幅深入源码探究了Threa ...
- 手写线程池,对照学习ThreadPoolExecutor线程池实现原理!
作者:小傅哥 博客:https://bugstack.cn Github:https://github.com/fuzhengwei/CodeGuide/wiki 沉淀.分享.成长,让自己和他人都能有 ...
- Java线程池使用和分析(二) - execute()原理
相关文章目录: Java线程池使用和分析(一) Java线程池使用和分析(二) - execute()原理 execute()是 java.util.concurrent.Executor接口中唯一的 ...
- 线程池技术之:ThreadPoolExecutor 源码解析
java中的所说的线程池,一般都是围绕着 ThreadPoolExecutor 来展开的.其他的实现基本都是基于它,或者模仿它的.所以只要理解 ThreadPoolExecutor, 就相当于完全理解 ...
- Java并发包源码学习之线程池(一)ThreadPoolExecutor源码分析
Java中使用线程池技术一般都是使用Executors这个工厂类,它提供了非常简单方法来创建各种类型的线程池: public static ExecutorService newFixedThread ...
- 线程池的使用及ThreadPoolExecutor的分析(一)
说明:本作者是文章的原创作者,转载请注明出处:本文地址:http://www.cnblogs.com/qm-article/p/7821602.html 一.线程池的介绍 在开发中,频繁的创建和销毁一 ...
- Executor框架(三)线程池详细介绍与ThreadPoolExecutor
本文将介绍线程池的设计细节,这些细节与 ThreadPoolExecutor类的参数一一对应,所以,将直接通过此类介绍线程池. ThreadPoolExecutor类 简介 java.uitl.c ...
随机推荐
- 【Linux】一些文件限制配置
linux资源限制配置文件是/etc/security/limits.conf:限制用户进程的数量对于linux系统的稳定性非常重要. limits.conf文件限制着用户可以使用的最大文件数,最大线 ...
- 基础篇:深入解析JAVA泛型和Type类型体系
目录 1 JAVA的Type类型体系 2 泛型的概念 3 泛型类和泛型方法的示例 4 类型擦除 5 参数化类型ParameterizedType 6 泛型的继承 7 泛型变量TypeVariable ...
- 【题解】[LNOI2014]LCA
题目戳我 \(\text{Solution:}\) 这题的转化思想很巧妙-- 考虑把\(dep\)给拆掉. 首先可以明确的是,每一个\(LCA\)一定在\(root\to z\)的路径上. 考虑一个\ ...
- 《穷查理年鉴》金钱 & 生意 & 律师(关于金钱)
金钱 025.钱还得快才会借得快. 030.钱和人有着复杂的友谊:人能让钱变坏,钱也能让人变坏. 034.绝望增加债务,勤奋偿还债务. 037.只有一无所有的人才会没有烦恼. 049.穷人为他的胃找食 ...
- JavaScript动态生成表格
要求: HTML标签只写一行表头 通过JS来写动态的表格(有多少组数据,就自动创建多少行表格) 为学习和演示,采用固定的数据,不涉及调用后台数据 代码实现: HTML内容: <table cel ...
- C#数据结构-双向链表
链表的概念以及链表与数组的差异不做过多的叙述,相信大家都耳熟能详,这里以c#语言实现简单的双向链表,作为备用,记录下~ public class Node<T> { private Nod ...
- day26 Pyhton 复习re模块和序列化模块
# re # 正则表达式 # 元字符 # 量词 # 贪婪匹配与惰性匹配 # 元字符量词 # 元字符量词? 在量词规范内,遇到一个x就停下来 # .*?x (.如果是第一个元素,那么它一定会从第一个元素 ...
- spring boot:用itextpdf处理pdf表格文件(spring boot 2.3.2)
一,什么是itextpdf? 1,itextpdf的用途 itextpdf是用来生成PDF文档的一个java类库, 通过iText可以生成PDF文档, 还可以把XML/Html文件转化为PDF文件 2 ...
- centos8安装zookeeper(单机方式)
一,下载zookeeper: 1,官网地址 http://zookeeper.apache.org/ 找到这个地址: https://mirrors.tuna.tsinghua.edu.cn/apac ...
- Python-selenium:鼠标键盘事件
鼠标事件 # 每个模拟事件后需加.perform() 才会执行 # context_click() 右击 # double_click() 双击 # drag_and_drop(source, tar ...