线程池ThreadPoolExecutor参数分析
概述
比如去火车站买票, 有7个(maximumPoolSize)售票窗口, 但只有3个(corePoolSize)窗口对外开放。那么对外开放的3个窗口称为核心线程数, 而最大线程数是7个窗口。
如果3个窗口都被占用, 那么后来的人就必须在售票厅(SynchronousQueue、LinkedBlockingQueue或者ArrayBlockingQueue)排队。
但后来售票厅人越来越多, 已经人满为患, 就类似于线程队列已满。这时候火车站站长下令, 把剩下的4个窗口也打开, 也就是目前已经有7个窗口同时运行。
后来又来了一批人,7个窗口也处理不过来了, 而且售票厅人已经满了, 这时候站长就下令封锁入口,不允许其他人再进来, 这就是线程拒绝策略。
线程存活时间指的是, 允许售票员休息的最长时间, 以此限制售票员偷懒的行为。
售票厅-SynchronousQueue
public static void main(String[] args) {
// 核心线程数
int corePoolSize = 3;
//最大线程数
int maximumPoolSize = 7;
// 非核心线程的最大空闲时间
long keepAliveTime = 10L;
// 队列种类
SynchronousQueue synchronousQueue = new SynchronousQueue<Runnable>();
//LinkedBlockingQueue linkedBlockingQueue = new LinkedBlockingQueue<Runnable>();
//ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<Runnable>(5);
ThreadPoolExecutor threadpool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, synchronousQueue);
for (int i = 0; i < 20; i++) {
threadpool.execute(() -> {
System.out.println("线程名称:" + Thread.currentThread().getName());
try {
Thread.sleep(50000L);
} catch (Exception ex) {
ex.printStackTrace();
}
});
System.out.println("当前线程数量:" + threadpool.getPoolSize());
System.out.println("当前队列大小:" + synchronousQueue.size());
}
}
输出结果
当前线程数量:1
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task org.tonny.threads.threadpool.ThreadPool$$Lambda$1/20132171@cc34f4d rejected from java.util.concurrent.ThreadPoolExecutor@17a7cec2[Running, pool size = 7, active threads = 7, queued tasks = 0, completed tasks = 0]
当前队列大小:0
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
线程名称:pool-1-thread-1
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
当前线程数量:2
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
当前队列大小:0
at org.tonny.threads.threadpool.ThreadPool.main(ThreadPool.java:19)
线程名称:pool-1-thread-2
当前线程数量:3
当前队列大小:0
线程名称:pool-1-thread-3
当前线程数量:4
当前队列大小:0
当前线程数量:5
当前队列大小:0
线程名称:pool-1-thread-4
当前线程数量:6
当前队列大小:0
当前线程数量:7
当前队列大小:0
线程名称:pool-1-thread-5
线程名称:pool-1-thread-7
线程名称:pool-1-thread-6 Process finished with exit code -1
拥有公平(FIFO)和非公平(LIFO)策略,非公平侧罗会导致一些数据永远无法被消费的情况。使用SynchronousQueue阻塞队列一般要求maximumPoolSizes为无界,避免线程拒绝执行操作。SynchronousQueue没有容量,是无缓冲等待队列,是一个不存储元素的阻塞队列,会直接将任务交给消费者,必须等队列中的添加元素被消费后才能继续添加新的元素。
售票厅- LinkedBlockingQueue
public static void main(String[] args) {
// 核心线程数
int corePoolSize = 3;
//最大线程数
int maximumPoolSize = 7;
// 非核心线程的最大空闲时间
long keepAliveTime = 10L;
// 队列种类
//SynchronousQueue synchronousQueue = new SynchronousQueue<Runnable>();
LinkedBlockingQueue linkedBlockingQueue = new LinkedBlockingQueue<Runnable>();
//ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<Runnable>(5);
ThreadPoolExecutor threadpool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, linkedBlockingQueue);
for (int i = 0; i < 20; i++) {
threadpool.execute(() -> {
System.out.println("线程名称:" + Thread.currentThread().getName());
try {
Thread.sleep(50000L);
} catch (Exception ex) {
ex.printStackTrace();
}
});
System.out.println("当前线程数量:" + threadpool.getPoolSize());
System.out.println("当前队列大小:" + linkedBlockingQueue.size());
}
}
输出结果
当前线程数量:1
当前队列大小:0
当前线程数量:2
当前队列大小:0
线程名称:pool-1-thread-1
线程名称:pool-1-thread-2
当前线程数量:3
当前队列大小:0
当前线程数量:3
当前队列大小:1
当前线程数量:3
当前队列大小:2
当前线程数量:3
当前队列大小:3
当前线程数量:3
当前队列大小:4
当前线程数量:3
当前队列大小:5
当前线程数量:3
当前队列大小:6
当前线程数量:3
当前队列大小:7
线程名称:pool-1-thread-3
当前线程数量:3
当前队列大小:8
当前线程数量:3
当前队列大小:9
当前线程数量:3
当前队列大小:10
当前线程数量:3
当前队列大小:11
当前线程数量:3
当前队列大小:12
当前线程数量:3
当前队列大小:13
当前线程数量:3
当前队列大小:14
当前线程数量:3
当前队列大小:15
当前线程数量:3
当前队列大小:16
当前线程数量:3
当前队列大小:17 Process finished with exit code -1
LinkedBlockingQueue是一个无界缓存等待队列。当前执行的线程数量达到corePoolSize的数量时,剩余的任务会在阻塞队列里等待。(所以在使用此阻塞队列时maximumPoolSizes就相当于无效了),每个线程完全独立于其他线程。生产者和消费者使用独立的锁来控制数据的同步,即在高并发的情况下可以并行操作队列中的数
售票厅- ArrayBlockingQueue
public static void main(String[] args) {
// 核心线程数
int corePoolSize = 3;
//最大线程数
int maximumPoolSize = 7;
// 非核心线程的最大空闲时间
long keepAliveTime = 10L;
// 队列种类
//SynchronousQueue synchronousQueue = new SynchronousQueue<Runnable>();
//LinkedBlockingQueue linkedBlockingQueue = new LinkedBlockingQueue<Runnable>();
ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<Runnable>(5);
ThreadPoolExecutor threadpool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, arrayBlockingQueue);
for (int i = 0; i < 20; i++) {
threadpool.execute(() -> {
System.out.println("线程名称:" + Thread.currentThread().getName());
try {
Thread.sleep(50000L);
} catch (Exception ex) {
ex.printStackTrace();
}
});
System.out.println("当前线程数量:" + threadpool.getPoolSize());
System.out.println("当前队列大小:" + arrayBlockingQueue.size());
}
}
输出结果
当前线程数量:1
当前队列大小:0
线程名称:pool-1-thread-1
当前线程数量:2
当前队列大小:0
当前线程数量:3
当前队列大小:0
当前线程数量:3
当前队列大小:1
当前线程数量:3
当前队列大小:2
当前线程数量:3
当前队列大小:3
线程名称:pool-1-thread-2
当前线程数量:3
当前队列大小:4
线程名称:pool-1-thread-3
当前线程数量:3
当前队列大小:5
当前线程数量:4
当前队列大小:5
当前线程数量:5
当前队列大小:5
线程名称:pool-1-thread-4
当前线程数量:6
当前队列大小:5
线程名称:pool-1-thread-5
当前线程数量:7
当前队列大小:5
线程名称:pool-1-thread-7
线程名称:pool-1-thread-6
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task org.tonny.threads.threadpool.ThreadPool$$Lambda$1/500977346@4769b07b rejected from java.util.concurrent.ThreadPoolExecutor@cc34f4d[Running, pool size = 7, active threads = 7, queued tasks = 5, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
at org.tonny.threads.threadpool.ThreadPool.main(ThreadPool.java:19) Process finished with exit code -1
ArrayBlockingQueue是一个有界缓存等待队列,可以指定缓存队列的大小,当正在执行的线程数等于corePoolSize时,多余的任务缓存在ArrayBlockingQueue队列中等待有空闲的线程时继续执行,当ArrayBlockingQueue已满时,会开启新的线程去执行,当线程数已经达到最大的maximumPoolSizes时,再有新的任务尝试加入ArrayBlockingQueue时会报错。
线程池ThreadPoolExecutor参数分析的更多相关文章
- 线程池ThreadPoolExecutor参数设置
线程池ThreadPoolExecutor参数设置 JDK1.5中引入了强大的concurrent包,其中最常用的莫过了线程池的实现ThreadPoolExecutor,它给我们带来了极大的方便,但同 ...
- 多线程学习笔记八之线程池ThreadPoolExecutor实现分析
目录 简介 继承结构 实现分析 ThreadPoolExecutor类属性 线程池状态 构造方法 execute(Runnable command) addWorker(Runnable firstT ...
- Java 线程池(ThreadPoolExecutor)原理分析与使用
在我们的开发中"池"的概念并不罕见,有数据库连接池.线程池.对象池.常量池等等.下面我们主要针对线程池来一步一步揭开线程池的面纱. 使用线程池的好处 1.降低资源消耗 可以重复利用 ...
- Java线程池(ThreadPoolExecutor)原理分析与使用
在我们的开发中"池"的概念并不罕见,有数据库连接池.线程池.对象池.常量池等等.下面我们主要针对线程池来一步一步揭开线程池的面纱. 使用线程池的好处 1.降低资源消耗 可以重复利用 ...
- Java 线程池(ThreadPoolExecutor)原理分析与实际运用
在我们的开发中"池"的概念并不罕见,有数据库连接池.线程池.对象池.常量池等等.下面我们主要针对线程池来一步一步揭开线程池的面纱. 有关java线程技术文章还可以推荐阅读:< ...
- Java入门系列之线程池ThreadPoolExecutor原理分析思考(十五)
前言 关于线程池原理分析请参看<http://objcoding.com/2019/04/25/threadpool-running/>,建议对原理不太了解的童鞋先看下此文然后再来看本文, ...
- Java线程池ThreadPoolExecutor使用和分析(三) - 终止线程池原理
相关文章目录: Java线程池ThreadPoolExecutor使用和分析(一) Java线程池ThreadPoolExecutor使用和分析(二) - execute()原理 Java线程池Thr ...
- Java线程池ThreadPoolExecutor使用和分析(二) - execute()原理
相关文章目录: Java线程池ThreadPoolExecutor使用和分析(一) Java线程池ThreadPoolExecutor使用和分析(二) - execute()原理 Java线程池Thr ...
- Java线程池ThreadPoolExecutor使用和分析(一)
相关文章目录: Java线程池ThreadPoolExecutor使用和分析(一) Java线程池ThreadPoolExecutor使用和分析(二) - execute()原理 Java线程池Thr ...
随机推荐
- ewasm项目初探
为了改进EVM1.0,以太坊的新一代虚拟机项目ewasm (github.com/ewasm)将支持WebAssembly(wasm),wasm在性能,扩展性,开发工具,社区都更有优势.除以太坊外,一 ...
- 调节音量的各个方法——AudioManager的使用
AudioManager类位于android.Media包中,该类提供访问控制音量和铃声模式的操作. //获取AudioManager实例对象 AudioManager audioManage = ( ...
- BZOJ_5311_贞鱼_决策单调性+带权二分
BZOJ_5311_贞鱼_决策单调性+带权二分 Description 众所周知,贞鱼是一种高智商水生动物.不过他们到了陆地上智商会减半. 这不?他们遇到了大麻烦! n只贞鱼到陆地上乘车,现在有k辆汽 ...
- MongoDB复制集安全认证
之前我有一篇博客写的是“node.js通过权限验证连接MongoDB”,这篇博客上提到如何在启动文件中通过配置auth参数来开启权限认证,但这种认证方式只适合单机节点,当我们使用复制集时应该怎么开启权 ...
- NOIP前的水题记录
CF147B Smile House 二分+矩阵快速幂,注意一下储存矩阵相乘结果的矩阵,初始化时,a[i][i]=-inf(而其他都可以a[i][i]=0,为了保证答案的可二分性). CF715B C ...
- Asp.net MVC 使用PagedList(新的已更名 为X.PagedList.Mvc) 分页
在asp.net mvc 中,可以bootstrap来作为界面,自己来写分页程序.也可以使用PagedList(作者已更名为 X.PagedList.Mvc)来分页. 1.首先,在NuGet程序包管理 ...
- kubeadm安装Kubernetes13.1集群-三
环境: master: 192.168.3.100 node01: 192.168.3.101 node02: 192.168.3.102 关闭所有主机防火墙,selinux: 配置主机互信: mas ...
- 008--linux 基础之网络配置和ssh服务
一.linux网络配置 ifconfig eno16777736 192.168.19.48/24 | eno16777736(网卡名) 192.168.19.48/24(临时IP地址) ...
- linux基于流的文件操作
1 打开流的函数 FIEL * fopen(const char * restrict pathname,const char* restrict type) FILE *fdopen(int fil ...
- git操作实战指南
1 背景 小白进入公司,进入日常多人开发,git的使用应该是新人要掌握的第一个技能.git是一个分布式数据存储库,分为远程存储和本地存储,本地存储的话,每一台计算机就相当于一个存储数据库,可以记录和存 ...