码迷,mamicode.com
首页 > 编程语言 > 详细

Java线程池拒绝策略

时间:2018-05-13 12:07:19      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:线程池拒绝策略   RejectedExecution   JDK拒绝策略   线程池   多线程   

线程池拒绝策略的类图:大致有七种左右
技术分享图片

具体看下每个类的源码:

AbortPolicy(直接抛异常)

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());
        }
    }

AbortPolicyWithReport(记录日志)

public class AbortPolicyWithReport extends ThreadPoolExecutor.AbortPolicy {
    static private final Logger logger = LoggerInit.LOGGER; 
    private final String threadName;
    private final URL url;
    public AbortPolicyWithReport(String threadName, URL url) {
        this.threadName = threadName;
        this.url = url;
    }
    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
        String msg = String.format("Thread pool is EXHAUSTED!" +
                " Thread Name: %s, Pool Size: %d (active: %d, core: %d, max: %d, largest: %d), Task: %d (completed: %d)," +
                " Executor status:(isShutdown:%s, isTerminated:%s, isTerminating:%s), in %s://%s:%d!" ,
                threadName, e.getPoolSize(), e.getActiveCount(), e.getCorePoolSize(), e.getMaximumPoolSize(), e.getLargestPoolSize(),
                e.getTaskCount(), e.getCompletedTaskCount(), e.isShutdown(), e.isTerminated(), e.isTerminating(),
                url.getProtocol(), url.getIp(), url.getPort());
        logger.warn(msg);
        throw new RejectedExecutionException(msg);
    }
}

CallerRunsPolicy(调用者执行)

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

DiscardOldestPolicy(抛弃线程池中最旧的未处理的任务)

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

DiscardPolicy(直接拒绝,不作处理)

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

IgnoreRunsPolicy(忽略运行测试,dump jvm信息)

private static class IgnoreRunsPolicy implements RejectedExecutionHandler {
        private final static Logger LOGGER = LoggerInit.LOGGER;
        public volatile boolean hasDump = false;
        public IgnoreRunsPolicy() {
        }
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            dumpJVMInfo();  ?//方法太长不予展示,就是这个dump jvm信息的意思
            // ignore it when thread full and it will be timeout response for client
            throw new RejectedExecutionException();
        }

NewThreadRunsPolicy(在MemoryAwareThreadPoolExecutor里面新建一个临时线程去处理)

private static final class NewThreadRunsPolicy implements RejectedExecutionHandler {
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            try {
                final Thread t = new Thread(r, "Temporary task executor");
                t.start();
            } catch (Throwable e) {
                throw new RejectedExecutionException(
                        "Failed to start a new thread", e);
            }
        }
    }

看完源码是不是对线程池拒绝策略有了更深的认识了?

Java线程池拒绝策略

标签:线程池拒绝策略   RejectedExecution   JDK拒绝策略   线程池   多线程   

原文地址:http://blog.51cto.com/thinklili/2115661

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!