当前位置:网站首页>[Concurrent programming] - Thread pool uses DiscardOldestPolicy strategy, DiscardPolicy strategy
[Concurrent programming] - Thread pool uses DiscardOldestPolicy strategy, DiscardPolicy strategy
2022-08-02 09:09:00 【Java technology stuff】
DiscardOldestPolicy策略
DiscardOldestPolicy策略是当任务添加到线程池中被拒绝时,线程池会放弃等待队列中最旧的未处理任务,Add rejected tasks to the waiting queue.
线程执行代码如下:
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
From the running results, it can be seen that two tasks put into the queue early are cancelled,Executed two tasks that were added later.
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执行结束
It can be seen from the running results that the redundant tasks are directly cancelled and executed.
边栏推荐
- C语言基础_结构体
- day_05_pickel 和 json
- 恋爱十不要
- Mysql Mac版下载安装教程
- PyQt5 (a) PyQt5 installation and configuration, read from the folder and display images, simulation to generate the sketch image
- In a recent build figure SLAM, and locate the progress
- pnpm的安装与使用
- Golang ORM框架 — GORM
- (Note) AXIS ACASIS DT-3608 Dual-bay Hard Disk Array Box RAID Setting
- day——05 迭代器,生成器
猜你喜欢
Postman download localization of installation and use
编程与哲学(2)——输出是为了更好的输入
PyQt5安装配置(PyCharm) 亲测可用
[Must read] Mylander valuation analysis, electrical stimulation products for pelvic and postpartum rehabilitation
深度学习汇报(4)
MySQL 中 count() 和 count(1) 有什么区别?哪个性能最好?
谈谈对Volatile的理解
Jenkins--基础--6.2--Pipeline--语法--声明式
HCIA静态路由综合练习
Redis分布式锁入门
随机推荐
Daily practice of dynamic programming (3)
EPSANet: An Efficient Pyramid Split Attention Block on Convolutional Neural Network
查看变量的数据格式
Jenkins--基础--6.1--Pipeline--介绍
动态规划每日一练(3)
redis-desktop-manager下载安装
自定义View实现波浪荡漾效果
MySQL读写分离与主从延迟
spark:热门品类中每个品类活跃的SessionID统计TOP10(案例)
spark:商品热门品类TOP10统计(案例)
AttributeError: module ‘clr‘ has no attribute ‘AddReference‘
spark:页面单跳转换率统计(案例)
PyQt5(一) PyQt5安装及配置,从文件夹读取图片并显示,模拟生成素描图像
Jenkins--部署--3.1--代码提交自动触发jenkins--方式1
Worship, Alibaba distributed system development and core principle analysis manual
etcd implements large-scale service governance application combat
破解wifi密码 暴力破解 保姆式教学
unity pdg 设置隐藏不需要的节点以及实现自动勾选自动加载项
shell中计算命令详解(expr、(())、 $[]、let、bc )
大厂外包,值得拥有吗?