我们来看下ThreadPoolExecutor默认的四种拒绝策略:
DiscardOldestPolicy,DiscardPolicy,AbortPolicy,CallerRunsPolicy,源代码如下
![78c1a0ea95363986edf1d1536803d6a7.png](https://i-blog.csdnimg.cn/blog_migrate/280a0b16441aed28fc6ed5a2e8fb31f4.jpeg)
![263ff93a2b0c440f25925ad2f4549bf6.png](https://i-blog.csdnimg.cn/blog_migrate/f93b563bf773b4120b154d93ef296a33.jpeg)
![624115ae778384468700dbdece20a189.png](https://i-blog.csdnimg.cn/blog_migrate/f6422d7ba10dcec75794def2eacc2e2b.jpeg)
![7e13bd3c2cec04175fdcb3f1194d422d.png](https://i-blog.csdnimg.cn/blog_migrate/91f4ff12ffb48a7047e68182063ad91f.jpeg)
所以自定义拒绝策略实现起来也是相对简单些只需要实现RejectedExecutionHandler接口的rejectedExecution方法即可代码如下
import java.util.concurrent.RejectedExecutionHandler;import java.util.concurrent.ThreadPoolExecutor;public class MyRejectExecutionHandler implements RejectedExecutionHandler { /** * 打印丢弃的任务 * @param r * @param executor */ @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { System.out.println(""+r.toString()); }}