当前位置:网站首页>[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.
边栏推荐
- TiFlash 存储层概览
- 天地图给多边形加标注
- Redisson报异常attempt to unlock lock, not locked by current thread by node id解决方案
- day_05模块
- 【论文阅读】Distilling the Knowledge in a Neural Network
- Nodejs3day(express简介,express创建基本Web服务器,托管静态资源,nodemon下载及出现的问题,中间件,编写GET,POST,JSONP接口)
- OneinStack多版本PHP共存
- EPSANet: An Efficient Pyramid Split Attention Block on Convolutional Neural Network
- A little bit of knowledge - why do not usually cook with copper pots
- pycharm的基本使用教程(1)
猜你喜欢
随机推荐
leetcode:639. 解码方法 II
houdini 求出曲线的法向 切线以及副法线
十、 网络管理
Codeforces Round #811 (Div. 3)无DF
Jenkins--部署--3.1--代码提交自动触发jenkins--方式1
小康股份更名赛力斯,如何走出一条高端产品的“丝绸之路”?
sql concat(),如何才能拼接表的名字
【Flink 问题】Flink 如何提交轻量jar包 依赖该如何存放 会遇到哪些问题
Redisson报异常attempt to unlock lock, not locked by current thread by node id解决方案
【特别提醒】订阅此专栏的用户请先阅读本文再决定是否需要购买此专栏
XML简介
Postman download localization of installation and use
HCIP笔记第十三天
单机部署flink,创建oracle19c rac的连接表时报错 ORA-12505 ,怎么回事?
How to use postman
抓包工具Charles修改Response步骤
C语言_条件编译
PyQt5 (a) PyQt5 installation and configuration, read from the folder and display images, simulation to generate the sketch image
【论文阅读】Distilling the Knowledge in a Neural Network
LeetCode_2358_分组的最大数量