当前位置:网站首页>【并发编程】- 线程池使用DiscardOldestPolicy策略、DiscardPolicy策略
【并发编程】- 线程池使用DiscardOldestPolicy策略、DiscardPolicy策略
2022-08-02 08:42:00 【Java技术那些事儿】
DiscardOldestPolicy策略
DiscardOldestPolicy策略是当任务添加到线程池中被拒绝时,线程池会放弃等待队列中最旧的未处理任务,将被拒绝的任务添加到等待队列中。
线程执行代码如下:
public class TheRunnable implements Runnable {
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
private String username;
public TheRunnable(String username){
super();
this.username=username;
}
@Override
public void run() {
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
System.out.println("线程名:"+username+" 开始时间:"+simpleDateFormat.format(new Date()));
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
运行类代码如下:
@Slf4j
public class DiscardOldestPolicyRun {
public static void main(String[] args) {
ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(2);
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 3, 10, TimeUnit.SECONDS, arrayBlockingQueue, new ThreadPoolExecutor.DiscardOldestPolicy());
for (int i = 0; i < 5 ; i++) {
TheRunnable runnable = new TheRunnable("Runnable " + i );
threadPoolExecutor.execute(runnable);
}
try {
Thread.sleep(500);
Iterator iterator = arrayBlockingQueue.iterator();
while (iterator.hasNext()){
Object object = iterator.next();
log.info("线程名:{}",((TheRunnable)object).getUsername());
}
threadPoolExecutor.execute(new TheRunnable("Runnable 6"));
threadPoolExecutor.execute(new TheRunnable("Runnable 7"));
iterator = arrayBlockingQueue.iterator();
while(iterator.hasNext()){
Object object = iterator.next();
log.info("线程名:{}",((TheRunnable)object).getUsername());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
运行结果如下:
线程名:Runnable 4 开始时间:16:47:54
线程名:Runnable 1 开始时间:16:47:54
线程名:Runnable 0 开始时间:16:47:54
16:47:55.509 [main] INFO com.ozx.concurrentprogram.executor.controller.DiscardOldestPolicyRun - 线程名:Runnable 2
16:47:55.516 [main] INFO com.ozx.concurrentprogram.executor.controller.DiscardOldestPolicyRun - 线程名:Runnable 3
16:47:55.517 [main] INFO com.ozx.concurrentprogram.executor.controller.DiscardOldestPolicyRun - 线程名:Runnable 6
16:47:55.517 [main] INFO com.ozx.concurrentprogram.executor.controller.DiscardOldestPolicyRun - 线程名:Runnable 7
线程名:Runnable 7 开始时间:16:47:56
线程名:Runnable 6 开始时间:16:47:56
从运行结果看出早期放入队列中两个任务被取消了,执行了后来添加的两个任务。
DiscardPolicy策略
DiscardPolicy策略是当任务添加到线程池中被拒绝时,线程池将丢弃被拒绝的任务。
运行类代码如下:
@Slf4j
public class DiscardPolicyRun {
public static void main(String[] args) throws InterruptedException {
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
log.info(Thread.currentThread().getName()+"执行结束");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(2);
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 3, 10, TimeUnit.SECONDS, arrayBlockingQueue, new ThreadPoolExecutor.DiscardPolicy());
for (int i = 0; i < 8 ; i++) {
threadPoolExecutor.execute(runnable);
}
Thread.sleep(8000);
log.info("线程池中最大线程数:{},队列数:{}",threadPoolExecutor.getPoolSize(),arrayBlockingQueue.size());
}
}
运行结果如下:
17:40:14.301 [pool-1-thread-1] INFO com.ozx.concurrentprogram.executor.controller.DiscardPolicyRun - pool-1-thread-1执行结束
17:40:14.301 [pool-1-thread-2] INFO com.ozx.concurrentprogram.executor.controller.DiscardPolicyRun - pool-1-thread-2执行结束
17:40:14.301 [pool-1-thread-3] INFO com.ozx.concurrentprogram.executor.controller.DiscardPolicyRun - pool-1-thread-3执行结束
17:40:17.278 [main] INFO com.ozx.concurrentprogram.executor.controller.DiscardPolicyRun - 线程池中最大线程数:3,队列数:0
17:40:19.326 [pool-1-thread-1] INFO com.ozx.concurrentprogram.executor.controller.DiscardPolicyRun - pool-1-thread-1执行结束
17:40:19.326 [pool-1-thread-2] INFO com.ozx.concurrentprogram.executor.controller.DiscardPolicyRun - pool-1-thread-2执行结束
从运行结果看出多余的任务直接被取消执行。
边栏推荐
- 天地图给多边形加标注
- 二分类和多分类
- Postman download localization of installation and use
- (Note)阿克西斯ACASIS DT-3608双盘位硬盘阵列盒RAID设置
- AttributeError: module ‘clr‘ has no attribute ‘AddReference‘
- High imitation [Huawei consumer business official website] and wonderful animation analysis: practice embedding JS code in low-code platform
- ip地址那点事(二)
- [OC学习笔记]weak的实现原理
- PyCharm使用教程(详细版 - 图文结合)
- 了解下C# 不安全代码
猜你喜欢

天地图给多边形加标注

Scala类型转换

PyCharm使用教程(较详细,图+文)

USACO美国信息学奥赛竞赛12月份开赛,中国学生备赛指南

PyCharm usage tutorial (detailed version - graphic and text combination)

Redis分布式锁

【电子电路】长按键拉低电平,适用在有休眠机制的MCU但是没有看门狗,一个按键多个功能场景下使用

SVN下载上传文件
![Three types of [OC learning notes] Block](/img/40/edf59e6e68891ea7c9ab0481fe7bfc.png)
Three types of [OC learning notes] Block

文章解读 -- FlowNet3D:Learning Scene Flow in 3D Point Clouds
随机推荐
Three types of [OC learning notes] Block
etcd implements large-scale service governance application combat
【Flink 问题】Flink 如何提交轻量jar包 依赖该如何存放 会遇到哪些问题
QT web development - Notes - 3
初学者怎么快速学会SQL
二分类和多分类
JSP中page指令的import命令具有什么功能呢?
Mysql Mac版下载安装教程
Bigder:41/100生产bug有哪些分类
为什么都推荐使用wordpress, 而不是 phpcms 这些国内的CMS呢?
day_05模块
十大免费cms建站系统介绍推荐
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之一:解题思路
编程与哲学(2)——输出是为了更好的输入
RestTemlate源码分析及工具类设计
R language plotly visualization: plotly visualizes the scatter plot of the actual value of the regression model and the predicted value of the regression, analyzes the prediction performance of the re
The custom table form
A little bit of knowledge - why do not usually cook with copper pots
那些年我们踩过的 Flink 坑系列
【电子电路】长按键拉低电平,适用在有休眠机制的MCU但是没有看门狗,一个按键多个功能场景下使用