Java 之ThreadPoolExecutor.RejectedExecutionHandler

  • ThreadPoolExecutor.AbortPolicy()抛出java.util.concurrent.RejectedExecutionException异常 终止策略是默认的饱和策略;
  • ThreadPoolExecutor.CallerRunsPolicy()当抛出RejectedExecutionException异常时,会调rejectedExecution方法 调用者运行策略实现了一种调节机制,该策略既不会抛弃任务也不会爆出异常,而是将任务退回给调用者,从而降低新任务的流量
  • ThreadPoolExecutor.DiscardOldestPolicy()抛弃旧的任务;当新提交的任务无法保存到队列中等待执行时将抛弃最旧的任务,然后尝试提交新任务。如果等待队列是一个优先级队列,抛弃最旧的策略将导致抛弃优先级最高的任务,因此AbortPolicy最好不要和优先级队列一起使用。
  • ThreadPoolExecutor.DiscardPolicy()抛弃当前的任务

 

Java代码   收藏代码
  1. /** 
  2.  *返回给调用者的饱和策略 
  3.  * @author zhangwei_david 
  4.  * @version $Id: CallerRunsTestClient.java, v 0.1 2014年11月13日 下午3:14:58 zhangwei_david Exp $ 
  5.  */  
  6. public class CallerRunsTestClient {  
  7.   
  8.     /** 
  9.      * 
  10.      * @param args 
  11.      */  
  12.     public static void main(String[] args) {  
  13.         //等待队列  
  14.         final LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(5);  
  15.           
  16.         ThreadPoolExecutor executor = new ThreadPoolExecutor(352, TimeUnit.SECONDS, queue);  
  17.   
  18.         executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());  
  19.   
  20.         for (int i = 0; i < 15; i++) {  
  21.             executor.execute(new Runnable() {  
  22.   
  23.                 public void run() {  
  24.                     System.out.println("run-" + Thread.currentThread().getName() + " queue:"  
  25.                                        + queue.size());  
  26.                     try {  
  27.                         TimeUnit.SECONDS.sleep(20);  
  28.                     } catch (InterruptedException e) {  
  29.                         //logger.error("", e);  
  30.                     }  
  31.                 }  
  32.             });  
  33.         }  
  34.     }  
  35.   
  36. }  

 运行的结果是:可以在日志中看有两次是在主线程中运行的

Java代码   收藏代码
  1. run-pool-1-thread-1 queue:5  
  2. run-pool-1-thread-2 queue:5  
  3. run-pool-1-thread-3 queue:5  
  4. run-main queue:5  
  5. run-pool-1-thread-4 queue:5  
  6. run-pool-1-thread-5 queue:5  
  7. run-pool-1-thread-1 queue:4  
  8. run-pool-1-thread-3 queue:2  
  9. run-pool-1-thread-2 queue:3  
  10. run-main queue:5  
  11. run-pool-1-thread-5 queue:4  
  12. run-pool-1-thread-4 queue:3  
  13. run-pool-1-thread-2 queue:1  
  14. run-pool-1-thread-1 queue:1  
  15. run-pool-1-thread-3 queue:0  

 终止饱和策略是,当提交的任务无法进入等待队列且线程池中创建的线程数量已经达到了最大线程数量的限制,则会拒绝新提交的任务。

Java代码   收藏代码
  1. /** 
  2.  *终止饱和策略 
  3.  * @author zhangwei_david 
  4.  * @version $Id: AbortTestClient.java, v 0.1 2014年11月13日 下午3:03:47 zhangwei_david Exp $ 
  5.  */  
  6. public class AbortTestClient {  
  7.   
  8.     public static void main(String[] args) {  
  9.   
  10.         final ThreadPoolExecutor executor = new ThreadPoolExecutor(132, TimeUnit.SECONDS,  
  11.             new LinkedBlockingQueue<Runnable>(5));  
  12.   
  13.         executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());  
  14.   
  15.         executor.setThreadFactory(new MyThreadFactory("Test"));  
  16.   
  17.         for (int i = 0; i < 10; i++) {  
  18.             try {  
  19.                 executor.execute(new Runnable() {  
  20.   
  21.                     public void run() {  
  22.                         //doNothing  
  23.                     }  
  24.                 });  
  25.             } catch (RejectedExecutionException e) {  
  26.                 System.out.println("第" + i + "次提交线程被拒绝!  当前活动线程数:" + executor.getActiveCount()  
  27.                     + " 队列长度:" + executor.getQueue().size());  
  28.             }  
  29.         }  
  30.     }  
  31. }  

 运行的结果:

Java代码   收藏代码
  1. 8次提交线程被拒绝!  当前活动线程数:3 队列长度:5  
  2. 9次提交线程被拒绝!  当前活动线程数:3 队列长度:5  
  3. 一月 212015 9:11:03 下午 com.cathy.demo.concurrency.executor.threadFactory.MyAppThread run  
  4. 信息: Created Test-2  
  5. 一月 212015 9:11:03 下午 com.cathy.demo.concurrency.executor.threadFactory.MyAppThread run  
  6. 信息: Created Test-1  
  7. 一月 212015 9:11:03 下午 com.cathy.demo.concurrency.executor.threadFactory.MyAppThread run  
  8. 信息: Created Test-3  
  9. 一月 212015 9:11:05 下午 com.cathy.demo.concurrency.executor.threadFactory.MyAppThread run  
  10. 信息: Exiting Test-3  
  11. 一月 212015 9:11:05 下午 com.cathy.demo.concurrency.executor.threadFactory.MyAppThread run  
  12. 信息: Exiting Test-1  

DiscardPolicy 是抛弃当前任务

 

Java代码   收藏代码
  1. /** 
  2.  * 
  3.  * @author zhangwei_david 
  4.  * @version $Id: DiscardTestClient.java, v 0.1 2014年11月13日 下午3:16:28 zhangwei_david Exp $ 
  5.  */  
  6. public class DiscardTestClient {  
  7.   
  8.     /** 
  9.      * 
  10.      * @param args 
  11.      */  
  12.     public static void main(String[] args) {  
  13.         final ThreadPoolExecutor executor = new ThreadPoolExecutor(132, TimeUnit.SECONDS,  
  14.             new LinkedBlockingQueue<Runnable>(5));  
  15.   
  16.         executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());  
  17.   
  18.         for (int i = 0; i < 10; i++) {  
  19.             System.out.println(MessageFormat.format("第{0}次提交任务,当前等待队列长度{1}", i, executor.getQueue()  
  20.                 .size()));  
  21.             executor.execute(new Runnable() {  
  22.   
  23.                 public void run() {  
  24.                     System.out.println(Thread.currentThread().getName());  
  25.                     try {  
  26.                         TimeUnit.SECONDS.sleep(10);  
  27.                     } catch (InterruptedException e) {  
  28.                         //logger.error("", e);  
  29.                     }  
  30.                 }  
  31.             });  
  32.   
  33.         }  
  34.         executor.shutdown();  
  35.   
  36.     }  
  37.   
  38. }  
 运行结果是:
Java代码   收藏代码
  1. 0次提交任务,当前等待队列长度0  
  2. 1次提交任务,当前等待队列长度0  
  3. pool-1-thread-1  
  4. 2次提交任务,当前等待队列长度1  
  5. 3次提交任务,当前等待队列长度2  
  6. 4次提交任务,当前等待队列长度3  
  7. 5次提交任务,当前等待队列长度4  
  8. 6次提交任务,当前等待队列长度5  
  9. pool-1-thread-2  
  10. 7次提交任务,当前等待队列长度5  
  11. pool-1-thread-3  
  12. 8次提交任务,当前等待队列长度5  
  13. 9次提交任务,当前等待队列长度5  
  14. pool-1-thread-1  
  15. pool-1-thread-2  
  16. pool-1-thread-3  
  17. pool-1-thread-1  
  18. pool-1-thread-3  
 通过结果可以发现,提交了10个任务最终只有8个任务被执行!其中有两次被抛弃了!
DiscardOldestPolicy :抛弃最旧的任务

 

 

 

 

Java代码   收藏代码
  1. /** 
  2.  * 
  3.  * @author zhangwei_david 
  4.  * @version $Id: DiscardOldestTest.java, v 0.1 2015年1月22日 上午9:42:21 zhangwei_david Exp $ 
  5.  */  
  6. public class DiscardOldestTest {  
  7.   
  8.     /** 
  9.      * 
  10.      * @param args 
  11.      */  
  12.     public static void main(String[] args) {  
  13.         final ThreadPoolExecutor executor = new ThreadPoolExecutor(132, TimeUnit.SECONDS,  
  14.             new LinkedBlockingQueue<Runnable>(5));  
  15.   
  16.         executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());  
  17.   
  18.         for (int i = 0; i < 10; i++) {  
  19.             System.out.println(MessageFormat.format("第{0}次提交任务,当前等待队列长度{1}", i, executor.getQueue()  
  20.                 .size()));  
  21.             executor.execute(new MyTask(String.valueOf(i)));  
  22.   
  23.         }  
  24.         executor.shutdown();  
  25.   
  26.     }  
  27.   
  28.     public static class MyTask implements Runnable {  
  29.   
  30.         private String name;  
  31.   
  32.         /** 
  33.          * @see java.lang.Runnable#run() 
  34.          */  
  35.         public void run() {  
  36.             System.out.println(name);  
  37.             try {  
  38.                 TimeUnit.SECONDS.sleep(10);  
  39.             } catch (InterruptedException e) {  
  40.                 //logger.error("", e);  
  41.             }  
  42.         }  
  43.   
  44.         public MyTask(String name) {  
  45.             super();  
  46.             this.name = name;  
  47.         }  
  48.   
  49.     }  
  50.   
  51. }  
 

 

 

 

Java代码   收藏代码
  1. 0次提交任务,当前等待队列长度0  
  2. 0  
  3. 1次提交任务,当前等待队列长度0  
  4. 2次提交任务,当前等待队列长度1  
  5. 3次提交任务,当前等待队列长度2  
  6. 4次提交任务,当前等待队列长度3  
  7. 5次提交任务,当前等待队列长度4  
  8. 6次提交任务,当前等待队列长度5  
  9. 7次提交任务,当前等待队列长度5  
  10. 8次提交任务,当前等待队列长度5  
  11. 9次提交任务,当前等待队列长度5  
  12. 6  
  13. 7  
  14. 3  
  15. 4  
  16. 5  
  17. 8  
  18. 9  
 发现第二次和第三次提交的任务被抛弃了!

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值