1.线程池拒绝策略的触发
ThreadPoolExecutor创建线程池时传递的参数有7个,其中4个对拒绝策略有影响,他们分别是:
- corePoolSize核心线程数
- MaxPoolSize最大线程数
- BlockingQueue阻塞队列
- RejectedExecutionHandler拒绝策略
当提交的任务数大于corePoolSize时,会优先放到阻塞队列中,当阻塞队列被填充满之后,判断当前线程池的线程数量是否大于MaxPoolSize,如果不大于,创建新的线程执行任务,如果大于时就会触发拒绝策略,其实就是当未执行到任务数大于(MaxPoolSize + BlockingQueue容量)时触发拒绝策略。
2.JDK中内置的4种线程池拒绝策略
JDK定义到拒绝策略接口如下:
public interface RejectedExecutionHandler {
/**
* Method that may be invoked by a {@link ThreadPoolExecutor} when
* {@link ThreadPoolExecutor#execute execute} cannot accept a
* task. This may occur when no more threads or queue slots are
* available because their bounds would be exceeded, or upon
* shutdown of the Executor.
*
* <p>In the absence of other alternatives, the method may throw
* an unchecked {@link RejectedExecutionException}, which will be
* propagated to the caller of {@code execute}.
*
* @param r the runnable task requested to be executed
* @param executor the executor attempting to execute this task
* @throws RejectedExecutionException if there is no remedy
*/
void rejectedExecution(Runnable r, ThreadPoolExecutor executor);
}
接口中只有一个方法,rejectedExecution(),当触发拒绝策略时,线程池会调用你设置到具体到策略,不同的场景拒绝策略的执行逻辑不同,可以根据自己的业务逻辑实现该接口,在创建线程池时传入自己的拒绝策略。
2.1 CallerRunsPolicy调用者运行策略
/**
* Executes task r in the caller's thread, unless the executor
* has been shut down, in which case the task is discarded.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
r.run();
}
}
功能:当触发决绝策略时,只要线程池没有关闭,就有提交任务的线程处理。
使用场景:改策略一般用在不允许任务失败,对性能要求不高,且并发量较小到场景,线程池一般情况下不会关闭,意味着提交到任务一定会被执行,但是当触发拒绝策略时,任务是当前线程自己执行到,当提交的任务多了,系统运行缓慢,阻塞后续任务的执行。
2.2AbortPolicy终止策略
/**
* Always throws RejectedExecutionException.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
* @throws RejectedExecutionException always
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());
}
功能:当触发拒绝策略时,直接抛出异常,决绝执行当前任务
使用场景:该策略为默认策略,ExecutorService预定义的4种线程池默认的拒绝策略就是这个,但是ExecutorService中到线程池实际上线程池队列都是无界的,也就是说把内存消耗完也不会执行拒绝策略,当自己定义线程池时使用该策略需要处理好异常信息,任务被中断。
2.3 DiscardPolicy丢弃策略
/**
* A handler for rejected tasks that silently discards the
* rejected task.
*/
public static class DiscardPolicy implements RejectedExecutionHandler {
/**
* Creates a {@code DiscardPolicy}.
*/
public DiscardPolicy() { }
/**
* Does nothing, which has the effect of discarding task r.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
}
}
功能:直接丢弃任务,不会触发任何操作
场景:如果提交的任务无关紧要,可以使用此策略,时间场景不怎么实用。
2.4 DiscardOldPolicy丢弃老策略
/**
* A handler for rejected tasks that discards the oldest unhandled
* request and then retries {@code execute}, unless the executor
* is shut down, in which case the task is discarded.
*/
public static class DiscardOldestPolicy implements RejectedExecutionHandler {
/**
* Creates a {@code DiscardOldestPolicy} for the given executor.
*/
public DiscardOldestPolicy() { }
/**
* Obtains and ignores the next task that the executor
* would otherwise execute, if one is immediately available,
* and then retries execution of task r, unless the executor
* is shut down, in which case task r is instead discarded.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
e.getQueue().poll();
e.execute(r);
}
}
}
功能:如果线程池未关闭,弹出队列头部到元素,然后尝试执行
场景:这个策略也是会丢弃任务,但丢弃的是未执行的老任务。