标签:线程池拒绝策略 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);
}
}
}
看完源码是不是对线程池拒绝策略有了更深的认识了?
标签:线程池拒绝策略 RejectedExecution JDK拒绝策略 线程池 多线程
原文地址:http://blog.51cto.com/thinklili/2115661