首页 > 编程语言 > 详细

Java 之ThreadPoolExecutor.RejectedExecutionHandler

时间:2015-01-22 02:06:45      阅读:519      评论:0      收藏:0      [点我收藏+]

?

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

?

/**
 *返回给调用者的饱和策略
 * @author zhangwei_david
 * @version $Id: CallerRunsTestClient.java, v 0.1 2014年11月13日 下午3:14:58 zhangwei_david Exp $
 */
public class CallerRunsTestClient {

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        //等待队列
        final LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(5);
        
        ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 5, 2, TimeUnit.SECONDS, queue);

        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

        for (int i = 0; i < 15; i++) {
            executor.execute(new Runnable() {

                public void run() {
                    System.out.println("run-" + Thread.currentThread().getName() + " queue:"
                                       + queue.size());
                    try {
                        TimeUnit.SECONDS.sleep(20);
                    } catch (InterruptedException e) {
                        //logger.error("", e);
                    }
                }
            });
        }
    }

}

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

run-pool-1-thread-1 queue:5
run-pool-1-thread-2 queue:5
run-pool-1-thread-3 queue:5
run-main queue:5
run-pool-1-thread-4 queue:5
run-pool-1-thread-5 queue:5
run-pool-1-thread-1 queue:4
run-pool-1-thread-3 queue:2
run-pool-1-thread-2 queue:3
run-main queue:5
run-pool-1-thread-5 queue:4
run-pool-1-thread-4 queue:3
run-pool-1-thread-2 queue:1
run-pool-1-thread-1 queue:1
run-pool-1-thread-3 queue:0

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

/**
 *终止饱和策略
 * @author zhangwei_david
 * @version $Id: AbortTestClient.java, v 0.1 2014年11月13日 下午3:03:47 zhangwei_david Exp $
 */
public class AbortTestClient {

    public static void main(String[] args) {

        final ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 3, 2, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(5));

        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());

        executor.setThreadFactory(new MyThreadFactory("Test"));

        for (int i = 0; i < 10; i++) {
            try {
                executor.execute(new Runnable() {

                    public void run() {
                        //doNothing
                    }
                });
            } catch (RejectedExecutionException e) {
                System.out.println("第" + i + "次提交线程被拒绝!  当前活动线程数:" + executor.getActiveCount()
                    + " 队列长度:" + executor.getQueue().size());
            }
        }
    }
}

?运行的结果:

第8次提交线程被拒绝!  当前活动线程数:3 队列长度:5
第9次提交线程被拒绝!  当前活动线程数:3 队列长度:5
一月 21, 2015 9:11:03 下午 com.cathy.demo.concurrency.executor.threadFactory.MyAppThread run
信息: Created Test-2
一月 21, 2015 9:11:03 下午 com.cathy.demo.concurrency.executor.threadFactory.MyAppThread run
信息: Created Test-1
一月 21, 2015 9:11:03 下午 com.cathy.demo.concurrency.executor.threadFactory.MyAppThread run
信息: Created Test-3
一月 21, 2015 9:11:05 下午 com.cathy.demo.concurrency.executor.threadFactory.MyAppThread run
信息: Exiting Test-3
一月 21, 2015 9:11:05 下午 com.cathy.demo.concurrency.executor.threadFactory.MyAppThread run
信息: Exiting Test-1

?//后续待更新

Java 之ThreadPoolExecutor.RejectedExecutionHandler

原文:http://zhangwei-david.iteye.com/blog/2178212

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!