1. 策略模式
提供一组算法/策略, 调用时遵照DIP原则, 调用抽象的策略接口, 实现类可自定义, 新增策略, 只需要新增扩展, 调用者可以选择自由切换
juc中线程池 TPE(ThreadPoolExecutor)的拒绝策略就是用了策略模式: 拒绝执行处理器
在TPE(ThreadPoolExecutor类中提供的4个策略实现):
2. 线程池的拒绝策略
RejectedExecutionHandler
2.1 AbortPolicy: 抛出异常
AbortPolicy // 抛出异常: RejectedExecutionException (拒绝执行异常)
/** * A handler for rejected tasks that throws a RejectedExecutionException. */ public static class AbortPolicy implements RejectedExecutionHandler { /** * Creates an {@code AbortPolicy}. */ public AbortPolicy() { } /** * 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()); } }
2.2. CallerRunsPolicy: 调用者代处理
/**
* A handler for rejected tasks that runs the rejected task directly in the calling thread of the execute method,
* unless the executor has been shut down, in which case the task is discarded.
*/
public static class CallerRunsPolicy implements RejectedExecutionHandler {
/**
* Creates a {@code CallerRunsPolicy}.
*/
public 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.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. DiscardOldestPolicy: 丢弃最早的未处理任务
丢弃最早的未处理请求,然后重试执行. 若executor已经被关闭,在这种情况下,该任务也被丢弃。
/**
* A handler for rejected tasks that discards the oldest unhandled request and then retries 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);
}
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。