概述

比如去火车站买票, 有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参数分析的更多相关文章

  1. 线程池ThreadPoolExecutor参数设置

    线程池ThreadPoolExecutor参数设置 JDK1.5中引入了强大的concurrent包,其中最常用的莫过了线程池的实现ThreadPoolExecutor,它给我们带来了极大的方便,但同 ...

  2. 多线程学习笔记八之线程池ThreadPoolExecutor实现分析

    目录 简介 继承结构 实现分析 ThreadPoolExecutor类属性 线程池状态 构造方法 execute(Runnable command) addWorker(Runnable firstT ...

  3. Java 线程池(ThreadPoolExecutor)原理分析与使用

    在我们的开发中"池"的概念并不罕见,有数据库连接池.线程池.对象池.常量池等等.下面我们主要针对线程池来一步一步揭开线程池的面纱. 使用线程池的好处 1.降低资源消耗 可以重复利用 ...

  4. Java线程池(ThreadPoolExecutor)原理分析与使用

    在我们的开发中"池"的概念并不罕见,有数据库连接池.线程池.对象池.常量池等等.下面我们主要针对线程池来一步一步揭开线程池的面纱. 使用线程池的好处 1.降低资源消耗 可以重复利用 ...

  5. Java 线程池(ThreadPoolExecutor)原理分析与实际运用

    在我们的开发中"池"的概念并不罕见,有数据库连接池.线程池.对象池.常量池等等.下面我们主要针对线程池来一步一步揭开线程池的面纱. 有关java线程技术文章还可以推荐阅读:< ...

  6. Java入门系列之线程池ThreadPoolExecutor原理分析思考(十五)

    前言 关于线程池原理分析请参看<http://objcoding.com/2019/04/25/threadpool-running/>,建议对原理不太了解的童鞋先看下此文然后再来看本文, ...

  7. Java线程池ThreadPoolExecutor使用和分析(三) - 终止线程池原理

    相关文章目录: Java线程池ThreadPoolExecutor使用和分析(一) Java线程池ThreadPoolExecutor使用和分析(二) - execute()原理 Java线程池Thr ...

  8. Java线程池ThreadPoolExecutor使用和分析(二) - execute()原理

    相关文章目录: Java线程池ThreadPoolExecutor使用和分析(一) Java线程池ThreadPoolExecutor使用和分析(二) - execute()原理 Java线程池Thr ...

  9. Java线程池ThreadPoolExecutor使用和分析(一)

    相关文章目录: Java线程池ThreadPoolExecutor使用和分析(一) Java线程池ThreadPoolExecutor使用和分析(二) - execute()原理 Java线程池Thr ...

随机推荐

  1. jquery 获取radio被选中的值

    <html> <head> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"& ...

  2. html5--6-8 CSS选择器5

    html5--6-8 CSS选择器5 实例 <!DOCTYPE html> <html lang="zh-cn"> <head> <met ...

  3. codeforces round 422 div2 补题 CF 822 A-F

    A I'm bored with life 水题 #include<bits/stdc++.h> using namespace std; typedef long long int LL ...

  4. vue微信公众号、H5微信支付

    1.H5微信支付 后台会返回一个URL,前端直接跳转就OK(需要你传给后台一个ip,必须保证在同一域名下) 使用window.location.href =res.data;进行页面跳转到支付界面(r ...

  5. zabbix3.4自定义监控

    zabbix的服务器.客户端都已经部署完成,监控正常,用的是微信报警: 现在想监控一台Linux服务器(172.16.0.56)的剩余内存,在小于一定值的时候就报警: 1.在172.16.0.56上, ...

  6. UVa 11584 Partitioning by Palindromes (简单DP)

    题意:给定一个字符串,求出它最少可分成几个回文串. 析:dp[i] 表示前 i 个字符最少可分成几个回文串,dp[i] = min{ 1 + dp[j-1] | j-i是回文}. 代码如下: #pra ...

  7. Mac系统下的php扩展开发

    通常在开发PHP的时候,一些核心代码,比如加密函数或需要高效率执行的代码,此时可以用C语言写扩展.本文主要介绍了扩展的开发流程,具体的代码实现参考生成的文件说明. 当前PHP使用的是XAMPP 5.6 ...

  8. hdoj1028;他们说这题叫dp...

    #include<cstdio> #include<string> #include<iostream> #include<vector> #inclu ...

  9. bzoj 3771: Triple【生成函数+FFT+容斥原理】

    瞎搞居然1A,真是吃鲸 n的范围只有聪明人能看见--建议读题3遍 首先看计数就想到生成函数,列出多项式A(x),然后分别考虑123 对于选一个的直接计数即可: 对于选两个的,\( A(x)^2 \), ...

  10. hdu6195 cable cable cable(from 2017 ACM/ICPC Asia Regional Shenyang Online)

    最开始一直想不通,为什么推出这个公式,后来想了半天,终于想明白了. 题目大意是,有M个格子,有K个物品.我们希望在格子与物品之间连数量尽可能少的边,使得——不论是选出M个格子中的哪K个,都可以与K个物 ...