Java线程池拒绝策略

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java线程池拒绝策略相关的知识,希望对你有一定的参考价值。

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

具体看下每个类的源码:

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线程池拒绝策略的主要内容,如果未能解决你的问题,请参考以下文章

Java 线程池 ThreadPoolExecutor 八种拒绝策略浅析

Java线程池的拒绝策略

Java线程池拒绝策略

Java八股系列——线程池拒绝策略

Java 并发编程线程池机制 ( 线程池阻塞队列 | 线程池拒绝策略 | 使用 ThreadPoolExecutor 自定义线程池参数 )

java多线程系类:JUC线程池:05之线程池原理(转)