关于设计模式:jdk中的设计模式策略模式RejectedExecutionHandler

120次阅读

共计 2591 个字符,预计需要花费 7 分钟才能阅读完成。

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

正文完
 0