当前位置:网站首页>[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.
边栏推荐
猜你喜欢
随机推荐
腾讯T8架构师,教你学中小研发团队架构实践PDF,高级架构师捷径
Qt读取文件中内容(通过判断GBK UTF-8格式进行读取显示)
Jenkins--部署--3.1--代码提交自动触发jenkins--方式1
PyCharm usage tutorial (more detailed, picture + text)
十、 网络管理
【微信小程序】本地服务页面案例实现
XML简介
谈谈对Volatile的理解
mysqldump --set-gtid-purged=OFF
Bigder:41/100生产bug有哪些分类
普林斯顿微积分读本03第二章--编程实现函数图像绘制、三角学回顾
JSP页面中page指令contentPage/pageEncoding具有什么功能呢?
JSP中page指令的import命令具有什么功能呢?
利用minlm比较句子之间的相似度
sql concat(),如何才能拼接表的名字
SVN下载上传文件
tf.where使用
Detailed explanation of calculation commands in shell (expr, (()), $[], let, bc )
The packet capture tool Charles modifies the Response step
shell中计算命令详解(expr、(())、 $[]、let、bc )









