【并发编程】- 线程池使用DiscardOldestPolicy策略、DiscardPolicy策略

本文详细介绍了Java线程池的两种拒绝策略:DiscardOldestPolicy和DiscardPolicy。通过示例代码展示了这两种策略在任务过多时如何处理,DiscardOldestPolicy会丢弃最旧的未处理任务,而DiscardPolicy则直接丢弃被拒绝的任务。运行结果清晰地说明了两种策略的行为差异。
摘要由CSDN通过智能技术生成

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执行结束
从运行结果看出多余的任务直接被取消执行。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值