一、AbortPolicy
1.1 含义
超额提交的线程会被拒绝并抛出异常
1.2 源码
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());
}
}
二、DiscardPolicy
2.1 含义
默默丢弃超额提交的线程,“挥一挥衣袖,不带走一片云彩”。
2.2 源码
public static class DiscardPolicy implements RejectedExecutionHandler {
public DiscardPolicy() { }
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
}
}
三、DiscardOldestPolicy
3.1 含义
将最老的任务从队列中移除,把机会让给年轻人。
3.2 源码
public static class DiscardOldestPolicy implements RejectedExecutionHandler {
public DiscardOldestPolicy() { }
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
e.getQueue().poll();
e.execute(r);
}
}
}
四、CallerRunsPolicy
4.1 含义
调用线程者执行超额提交的任务。
4.2 源码
public static class CallerRunsPolicy implements RejectedExecutionHandler {
public CallerRunsPolicy() { }
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
r.run();
}
}
}
|