Part 4 of 6 · 2 min read
Thread pools
Submit tasks and let the pool run them.
Starting a new thread for each task is costly, and with no limit a burst of requests can start thousands. A thread pool keeps a fixed set of worker threads that take tasks from a queue. This is the producer-consumer pattern, with the pool as the consumers. JCIP points out that a pool of workers fed by a work queue is the best-known producer-consumer arrangement.
EJ Item 80 says to prefer executors and tasks to raw threads. A hand-made work queue takes a page of delicate code, and the executor framework gives a better one in a line. The key interface is Executor, which separates submitting a task from deciding how it runs.
ExecutorService pool = Executors.newFixedThreadPool(3);
List<Future<Integer>> fs = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
final int n = i;
fs.add(pool.submit(() -> n * n)); // a Callable that returns a value
}
int sum = 0;
for (Future<Integer> f : fs) sum += f.get(); // waits for each result
// sum is 55
pool.shutdown(); // stop accepting tasks, finish queued ones
pool.awaitTermination(1, TimeUnit.SECONDS);
Five tasks run on three threads, and the squares 1, 4, 9, 16, 25 add to 55. submit returns a Future, which is a ticket for a result that may not be ready. get() waits until it is.
A failure inside a task does not crash the pool. It is stored in the future, and get() throws ExecutionException with the real error as its cause. A task that throws IllegalStateException("boom") gives a caller who catches ExecutionException and reads getCause().getMessage() the text "boom". If nobody calls get(), the failure goes unseen, so always read the futures that matter, or log inside the task.
Always call shutdown(). EJ notes that without it the JVM will probably not exit, because the worker threads stay alive.
The module 1 rules still apply. A task should check its input, translate exceptions and leave state clean. Tasks should also be independent. JCIP says that tasks that depend on other tasks can deadlock a bounded pool, for example when every thread waits for a task that is stuck in the queue.
Thread pool
A thread pool is a fixed group of worker threads that repeatedly take tasks from a queue and run them.
Example: Executors.newFixedThreadPool(3).
Check yourself:
1. What does submit return, and what does get() do?
2. Where does an exception thrown inside a task go?
3. What happens if shutdown() is never called?