Sub topic 12
Tuning ThreadPoolExecutor: Pool Sizes, Queues, and Rejection Policies
The Executors factory methods are quick to use, but they make choices for you. Sometimes you need to set those choices yourself. ThreadPoolExecutor is the class behind most of those factory methods, and you can build one directly to control how many threads it uses and what happens when it gets busy.
The Settings You Can Choose
A ThreadPoolExecutor takes several settings when you create it. Each one is a single idea:
| Setting | What it is |
|---|---|
| Core pool size | The number of threads the pool keeps ready, even when they have nothing to do. |
| Maximum pool size | The most threads the pool will ever create. |
| Keep-alive time | How long an extra thread (above the core size) may sit idle before the pool removes it. |
| Work queue | A waiting line where tasks sit until a thread is free. |
| Rejection policy | A rule for what to do with a task when the pool cannot accept it. |
A bounded queue is a queue with a fixed size limit. ArrayBlockingQueue is one. An unbounded queue has no real limit, so it can grow until your program runs out of memory.
How the Pool Decides Where a Task Goes
When you hand the pool a task, it follows these steps in order:
- If fewer than the core number of threads exist, the pool starts a new thread for the task.
- If not, the pool puts the task in the queue.
- If the queue is full, the pool starts an extra thread, as long as it stays at or below the maximum.
- If the queue is full and the maximum is reached, the pool rejects the task.
Rejection Policies
A rejection policy is the rule the pool follows when it has no room for a task. Java includes four:
| Policy | What happens to the rejected task |
|---|---|
| `AbortPolicy` (the default) | The pool throws a `RejectedExecutionException`. |
| `CallerRunsPolicy` | The thread that submitted the task runs it itself. |
| `DiscardPolicy` | The pool drops the task without telling you. |
| `DiscardOldestPolicy` | The pool drops the oldest task in the queue, then tries again. |
CallerRunsPolicy is a common choice. While the submitting thread is busy running a task, it cannot submit more tasks, so new work arrives more slowly and the pool gets time to catch up.
A Worked Example
This pool has 2 core threads, at most 4 threads, a 30-second keep-alive time, a queue that holds 100 tasks, and CallerRunsPolicy.
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class CustomPoolDemo {
public static void main(String[] args) {
ThreadPoolExecutor pool = new ThreadPoolExecutor(
2,
4,
30, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(100),
new ThreadPoolExecutor.CallerRunsPolicy());
for (int i = 0; i < 10; i++) {
int id = i;
pool.execute(() -> System.out.println("Task " + id + " on " + Thread.currentThread().getName()));
}
pool.shutdown();
}
}When you run it, you will see only two thread names, such as pool-1-thread-1 and pool-1-thread-2. Ten tasks fit easily in a queue of 100, so the pool never needs extra threads. To see extra threads or the rejection policy, make the queue much smaller and make each task slower, for example by sleeping inside it.
Exercise
A pool has core size 2, maximum size 4, and an ArrayBlockingQueue of size 3. Every task runs for a long time. You submit 8 tasks quickly. How many threads run, how many tasks wait in the queue, and what happens to the 8th task?
Show answer
Tasks 1 and 2 start the 2 core threads. Tasks 3, 4, and 5 fill the queue. Tasks 6 and 7 start 2 extra threads, bringing the pool to its maximum of 4. That gives 4 running threads and 3 queued tasks. Task 8 finds the pool and the queue both full, so the rejection policy runs. With AbortPolicy it throws RejectedExecutionException. With CallerRunsPolicy the submitting thread runs it.
Exercise
Why does a ThreadPoolExecutor with an unbounded queue never use its maximum pool size?
Show answer
The pool only starts extra threads when the queue is full. An unbounded queue never fills, so the pool stays at its core size.