龙空技术网

java面试之线程池忙碌且阻塞队列也满的时候怎么处理

猿小嗨的小钟 335

前言:

现在各位老铁们对“线程池满了怎么处理”可能比较看重,兄弟们都想要知道一些“线程池满了怎么处理”的相关文章。那么小编在网络上网罗了一些关于“线程池满了怎么处理””的相关内容,希望我们能喜欢,咱们快快来学习一下吧!

线程池忙碌且阻塞队列也满的时候给一个据接的详细报告;

在出现上述情况的时候,会执行ThreadPoolExecutor.AbortPolicy类中的rejectedExecution方法;

我们自定义一个类继承它,并实现rejectedExecution即可;例如

public class AbortPolicyWithReport extends ThreadPoolExecutor.AbortPolicy {

protected static final Logger logger = LoggerFactory.getLogger(AbortPolicyWithReport.class);

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("Provider端线程池满!" +

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

}

}

标签: #线程池满了怎么处理