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


    DiscardOldestPolicy策略

    DiscardOldestPolicy策略是当任务添加到线程池中被拒绝时,线程池会放弃等待队列中最旧的未处理任务,将被拒绝的任务添加到等待队列中。
    线程执行代码如下:

    1. public class TheRunnable implements Runnable {
    2. public String getUsername() {
    3. return username;
    4. }
    5. public void setUsername(String username) {
    6. this.username = username;
    7. }
    8. private String username;
    9. public TheRunnable(String username){
    10. super();
    11. this.username=username;
    12. }
    13. @Override
    14. public void run() {
    15. try {
    16. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
    17. System.out.println("线程名:"+username+" 开始时间:"+simpleDateFormat.format(new Date()));
    18. Thread.sleep(2000);
    19. } catch (InterruptedException e) {
    20. e.printStackTrace();
    21. }
    22. }
    23. }

    运行类代码如下:

    1. @Slf4j
    2. public class DiscardOldestPolicyRun {
    3. public static void main(String[] args) {
    4. ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(2);
    5. ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 3, 10, TimeUnit.SECONDS, arrayBlockingQueue, new ThreadPoolExecutor.DiscardOldestPolicy());
    6. for (int i = 0; i < 5 ; i++) {
    7. TheRunnable runnable = new TheRunnable("Runnable " + i );
    8. threadPoolExecutor.execute(runnable);
    9. }
    10. try {
    11. Thread.sleep(500);
    12. Iterator iterator = arrayBlockingQueue.iterator();
    13. while (iterator.hasNext()){
    14. Object object = iterator.next();
    15. log.info("线程名:{}",((TheRunnable)object).getUsername());
    16. }
    17. threadPoolExecutor.execute(new TheRunnable("Runnable 6"));
    18. threadPoolExecutor.execute(new TheRunnable("Runnable 7"));
    19. iterator = arrayBlockingQueue.iterator();
    20. while(iterator.hasNext()){
    21. Object object = iterator.next();
    22. log.info("线程名:{}",((TheRunnable)object).getUsername());
    23. }
    24. } catch (InterruptedException e) {
    25. e.printStackTrace();
    26. }
    27. }
    28. }

    运行结果如下:

    线程名: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策略是当任务添加到线程池中被拒绝时,线程池将丢弃被拒绝的任务。

    运行类代码如下:

    1. @Slf4j
    2. public class DiscardPolicyRun {
    3. public static void main(String[] args) throws InterruptedException {
    4. Runnable runnable = new Runnable() {
    5. @Override
    6. public void run() {
    7. try {
    8. Thread.sleep(5000);
    9. log.info(Thread.currentThread().getName()+"执行结束");
    10. } catch (InterruptedException e) {
    11. e.printStackTrace();
    12. }
    13. }
    14. };
    15. ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(2);
    16. ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 3, 10, TimeUnit.SECONDS, arrayBlockingQueue, new ThreadPoolExecutor.DiscardPolicy());
    17. for (int i = 0; i < 8 ; i++) {
    18. threadPoolExecutor.execute(runnable);
    19. }
    20. Thread.sleep(8000);
    21. log.info("线程池中最大线程数:{},队列数:{}",threadPoolExecutor.getPoolSize(),arrayBlockingQueue.size());
    22. }
    23. }

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

  • 相关阅读:
    ES6 Promise、Generator与async简单介绍与应用
    python AssertionError: Torch not compiled with CUDA enabled
    设计模式之模版模式
    牛客NC357 矩阵第K小【中等 堆 Java、Go、PHP】
    Java 基于SpringBoot+Vue的社区医院管理系统的实现
    SpringMVC
    物联网AI MicroPython传感器学习 之 AS608指纹识别模块
    【服务器存储数据恢复】HP-Lefthand存储数据恢复案例
    引用计数法
    CMake入门教程【核心篇】设置和使用缓存变量
  • 原文地址:https://blog.csdn.net/LBWNB_Java/article/details/126068340