Java多執行緒-任務拒絕策略
簡介
Rejected tasks
New tasks submitted in method execute(Runnable) will be rejected when the Executor has been shut down, and also when the Executor uses finite bounds for both maximum threads and work queue capacity, and is saturated. In either case, the execute method invokes the RejectedExecutionHandler.rejectedExecution(Runnable, ThreadPoolExecutor) method of its RejectedExecutionHandler. Four predefined handler policies are provided:
In the default ThreadPoolExecutor.AbortPolicy, the handler throws a runtime RejectedExecutionException upon rejection.
In ThreadPoolExecutor.CallerRunsPolicy, the thread that invokes execute itself runs the task. This provides a simple feedback control mechanism that will slow down the rate that new tasks are submitted.
In ThreadPoolExecutor.DiscardPolicy, a task that cannot be executed is simply dropped.
In ThreadPoolExecutor.DiscardOldestPolicy, if the executor is not shut down, the task at the head of the work queue is dropped, and then execution is retried (which can fail again, causing this to be repeated.)
It is possible to define and use other kinds of RejectedExecutionHandler classes. Doing so requires some care especially when policies are designed to work only under particular capacity or queuing policies.
在 Executor 被 shutdown 時 或 執行緒池已經飽和( 執行緒數超過 最大執行緒數+阻塞佇列限制長度 )時,Task 將被拒絕,
ThreadPoolExecutor 提供了總共有四種默認的拒絕策略:AbortPolicy (默認策略) 、CallerRunsPolicy 、DiscardPolicy 和 DiscardOldestPolicy ,
可以通過實作 RejectedExecutionHandler 介面實作自定義拒絕策略,但應謹慎處理,
RejectedExecutionHandler 拒絕策略父級介面
A handler for tasks that cannot be executed by a ThreadPoolExecutor.
RejectedExecutionHandler 為所有拒絕策略應實作的介面,
自定義拒絕策略時,實作此介面,并重寫 rejectedExecution() 方法,以便 ThreadPoolExecutor 及其子類在遇到無法執行的 task 時呼叫,
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);
}
AbortPolicy 策略(默認拒絕策略)
A handler for rejected tasks that throws a RejectedExecutionException. This is the default handler for ThreadPoolExecutor and ScheduledThreadPoolExecutor.
AbortPolicy 是 ThreadPoolExecutor 及其子類 ScheduledThreadPoolExecutor 的默認拒絕策略,它會在拒絕tasks 繼續執行時,拋出 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());
}
}
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.
若為 CallerRunsPolicy 策略,將直接在 正在被呼叫的執行緒 的 execute() 方法中 運行被拒絕的task,
除非進行執行緒呼叫的 executor 已經被 shut down ,此時將直接取消 task,
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();
}
}
}
DiscardPolicy 策略
A handler for rejected tasks that silently discards the rejected task.
若為 DiscardPolicy 策略,將 寂靜地(即什么都不做)取消被拒絕的 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) {
}
}
DiscardOldestPolicy 策略
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.
若為 DiscardOldestPolicy 策略,則將 task 阻塞佇列中最古老的(佇列頭部的)未執行 task 出隊(取消任務),再次嘗試呼叫 execute() 方法以執行 task 或將 task 放入阻塞佇列,
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);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/273656.html
標籤:其他
上一篇:一文帶你走進介面測驗
