线程池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 ...
随机推荐
- 【CQ18阶梯赛第8场】题解
[A:HDU2032 杨辉三角]: 简单的递推,或者是基础的DP: 但是只有杨润东一个人1A,整体准确率只有8/37,具体原因不详. 经验:提交前一定要试一下比较特殊的数据或者最大的数据.其次,为了保 ...
- [SoapUI] Read data from response , use it to update parameter
import com.eviware.soapui.support.GroovyUtils def groovyUtils = new GroovyUtils( context ) def holde ...
- MongoDB复制集安全认证
之前我有一篇博客写的是“node.js通过权限验证连接MongoDB”,这篇博客上提到如何在启动文件中通过配置auth参数来开启权限认证,但这种认证方式只适合单机节点,当我们使用复制集时应该怎么开启权 ...
- CS231n 2016 通关 第二章-KNN
课程内容全纪录: 1.讲解图像分类的难点 1.光照强度 2.主体变形 3.主体与背景咬合 4.主体与背景相接近 5.同类别间存在区别 2.KNN 1.最近邻算法 2.Knn 3.hyperpara ...
- 国外1.5免费空间000webhost申请方法
空间大小:1500M 支持语言:PHP 数 据 库:MYSQL 国家/地区:国外 申请地址:http://www.000webhost.com/ 1500M/100GB/PHP/MYSQL/FTP ...
- UI:KVO、KVC
什么是KVC 什么是 KVO ? KVC:(NSKey ValueCoding)”键-值 编码“是一种间接的访问对象属性(字符串表征)的机制.对象的属性都可以通过使用KVC机制用相同的方式访问.我们 ...
- http://www.cnblogs.com/Javame/p/3632473.html
http://www.cnblogs.com/Javame/p/3632473.html
- Not enough free disk space on disk '/boot'(转载)
转自:http://m.oschina.net/blog/277224 # 解决 出现此情况是因为你的boot分区是单独分区的,像我只给了100M,以前装ubuntu时没有出现,所以当出现这个提示时, ...
- iOS 优雅地隐藏导航栏NavigationBar (Objc)
@interface FSViewController () <UINavigationControllerDelegate> @end @implementation FSViewCon ...
- Hexo - 修改永久链接的默认格式
Hexo的永久链接的默认格式是 :year/:month/:day/:title/,比如访问站点下某一篇文章时,其路径是 2018/04/12/xxxx/,如果我们的文章标题是中文的,那么该路径就会出 ...