Executors and thread pools question 79 of 180 mid
Walk through what ThreadPoolExecutor does with a submitted task.
- It always queues the task and a thread picks it up
- Correct: Below core size it starts a thread; otherwise it queues; only if the queue rejects does it grow to maximum; then it rejects
- It starts a thread up to maximum, then queues
- It runs the task on the calling thread
The order is the part people get backwards, and it explains most misconfigured pools:
- Fewer than
corePoolSizethreads: start a new thread, even if others are idle. - Otherwise offer to the queue. If it accepts, done.
- Only if the queue refuses (a bounded queue that is full) start threads up to
maximumPoolSize. - If that fails too, hand the task to the
RejectedExecutionHandler.
The consequence: with an unbounded queue, step 3 never happens, so maximumPoolSize is dead
configuration and the pool never grows past core. People set a maximum of 200, watch the pool stay at
10, and conclude the setting is broken.
Quiz yourself on Executors and thread pools Suggest a correction