NOTE
6.36 RejectedExecutionHandler
1. What is RejectedExecutionHandler? When corePoolSize is full, blockingQueue is full, and maxPoolSize is also full, how should a new task be handled? This is determined by RejectedExecutionHandler. 2. Types: CallerRunsPolicy, AbortPolicy, DiscardPolicy, DiscardOldestPolicy.
This is a historical learning note and may contain outdated or incomplete understanding.
1. What Is RejectedExecutionHandler?
When corePoolSize is full, the blockingQueue is also full, and maxPoolSize is full as well, how should a new task be handled? This is determined by RejectedExecutionHandler.
2. Types
2.1. CallerRunsPolicy
Handling strategy: regardless of the state of the thread pool, when a task arrives, execute it directly without considering whether the thread pool itself can actually execute it.
/**
* A handler for rejected tasks that runs the rejected task
* directly in the calling thread of the {@code 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) {
// As long as the thread pool has not been shut down, execute it directly
if (!e.isShutdown()) {
r.run();
}
}
}
2.2. AbortPolicy
Handling strategy: throw an exception. This is the default handling strategy.
/**
* A handler for rejected tasks that throws a
* {@code 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 an exception directly
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());
}
}
2.3. DiscardPolicy
Handling strategy: do nothing. This is equivalent to giving up execution of the current task.
/**
* 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) {
// Ignore this new task
}
}
2.4. DiscardOldestPolicy
Handling strategy: remove the task at the head of the task queue, and then execute the current task. This is equivalent to giving up the task at the head of the task queue.
/**
* 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 the thread pool has not been shut down, remove the head of the queue
if (!e.isShutdown()) {
e.getQueue().poll();
e.execute(r);
}
}
}
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub