zoukankan      html  css  js  c++  java
  • Java线程池-拒绝策略

    线程池之拒绝策略

    当线程池中任务队列已满且最大线程数maximumPoolSize达到最大,此时如果还有任务过来,那么将会触发拒绝策略

    线程池一共内置了四种拒绝策略,线程池默认使用AbortPolicy

    private static final RejectedExecutionHandler defaultHandler =new AbortPolicy();

    ThreadPoolExecutor.AbortPolicy()

    丢弃任务并抛出RejectedExecutionException异常

     public static class AbortPolicy implements RejectedExecutionHandler {
            public AbortPolicy() { }
            public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
                throw new RejectedExecutionException("Task " + r.toString() +
                                                     " rejected from " +
                                                     e.toString());
            }
    }

    img.png

    ThreadPoolExecutor.DiscardPolicy()

    丢弃任务,但是不抛出异常。

        public static class DiscardPolicy implements RejectedExecutionHandler {
            public DiscardPolicy() { }
            public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            }
        }
     

    ThreadPoolExecutor.DiscardOldestPolicy()

    丢弃队列最前面的任务,然后重新提交被拒绝的任务,e.getQueue().poll()将最前面的任务弹出

       public static class DiscardOldestPolicy implements RejectedExecutionHandler {
            public DiscardOldestPolicy() { }
    
            public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
                if (!e.isShutdown()) {
                    e.getQueue().poll();
                    e.execute(r);
                }
            }
        }
     

    ThreadPoolExecutor.CallerRunsPolicy()

    由调用线程(提交任务的线程)处理该任务

       public static class CallerRunsPolicy implements RejectedExecutionHandler {
            public CallerRunsPolicy() { }
            public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
                if (!e.isShutdown()) {
                    r.run();
                }
            }
        }
     

    自定义拒绝策略

    当上面拒绝策略不能满足我们的需求,我们需要自定义拒绝策略,只需要实现RejectedExecutionHandler就行

    package thread.customthreadpool.reject;
    
    import java.util.concurrent.RejectedExecutionException;
    import java.util.concurrent.RejectedExecutionHandler;
    import java.util.concurrent.ThreadPoolExecutor;
    
    /**
     * 自定义拒绝策略
     */
    public class MyThreadRejectExecutionHandler implements RejectedExecutionHandler {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            /**
             * 加入自己的处理逻辑
             */
            throw new RejectedExecutionException("请求异常");
        }
    }
     
    生命不止,折腾不息
  • 相关阅读:
    list for循环中删除元素
    XMLFeedSpider例子
    myeclipse一直卡在loading workbench解决方法
    代码
    在Github上面搭建Hexo博客(一):部署到Github
    RegEX正则表达式截取字符串
    将后台值传单前台js接收
    C# List<T>泛型用法
    基于jQuery——TreeGrid
    在线编程学习网站
  • 原文地址:https://www.cnblogs.com/steakliu/p/15245800.html
Copyright © 2011-2022 走看看