java 线程池 策略_Java线程池拒绝策略

线程池拒绝策略的类图:大致有七种左右

273267356bfbb792a7731d1baa5572b8.png

具体看下每个类的源码:

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

}

}

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值