Executors and thread pools question 81 of 180 mid
How do you size a thread pool?
- Always the number of CPU cores
- Correct: For CPU-bound work, about the core count; for blocking work, cores * (1 + wait time / service time), then measure
- As many threads as concurrent requests
- Twice the number of cores, always
For CPU-bound tasks, more threads than cores only adds context switches: availableProcessors(), or
one less if a thread is needed elsewhere. For blocking tasks the threads are mostly asleep, so the
useful count scales with how much of the time is spent waiting: N = cores * utilisation * (1 + W/S).
A task that waits 90 ms per 10 ms of compute wants roughly ten threads per core.
Say the rest of it, because that is what distinguishes an engineer from a formula: the number is a
starting point to be measured, separate pools for separate workloads so a slow dependency cannot
starve fast ones, and availableProcessors() respects cgroup limits in a container only on a
reasonably modern JVM, so check what the pod actually reports.
Virtual threads change this: for blocking work the answer becomes "do not pool at all".
Quiz yourself on Executors and thread pools Suggest a correction