Admission, capacity and rejection
Definition:
A bounded executor has a policy for admitting, queueing and rejecting work under load.
Explanation:
ThreadPoolExecutor prefers queueing once core workers exist.
Illustration image:
Example:
var pool = new java.util.concurrent.ThreadPoolExecutor(
2,4,30,java.util.concurrent.TimeUnit.SECONDS,
new java.util.concurrent.ArrayBlockingQueue<>(8),
new java.util.concurrent.ThreadPoolExecutor.AbortPolicy());
pool.shutdown();Expected behavior. The configured upper bounds are four workers and eight queued tasks; submission after shutdown is rejected.
Internal working:
With an unbounded queue, max workers is largely irrelevant and queue age rises when arrival rate exceeds service rate. Use a bounded queue, reserve capacity for other paths, and translate rejection at the proper request boundary. CallerRunsPolicy is unsafe on a nonblocking event loop because it executes work inside the caller.
Interview questions:
Why can an unbounded queue make maximumPoolSize ineffective?
After the core workers exist, queue offers keep succeeding, so the executor rarely grows past core.