What are the general guidelines for sizing a thread pool?
A widely used starting point, popularized by Brian Goetz, distinguishes CPU-bound from I/O-bound workloads. For CPU-bound work, the recommended pool size is roughly the number of CPU cores plus one, with the extra thread accounting for occasional page faults or brief pauses. For I/O-bound work, the formula scales with how much time each task spends waiting versus computing: number of threads equals number of CPUs multiplied by (1 + wait time / compute time). For example, with 4 CPUs and a task that spends 9ms waiting for every 1ms of actual computation, that works out to 4 x 10 = 40 threads. With virtual threads, introduced in Java 21, this sizing exercise mostly goes away for I/O-bound work -- instead of carefully sizing a pool, you simply use Executors.newVirtualThreadPerTaskExecutor() and let the JVM handle scheduling. In all cases, these formulas are only starting points, and the actual right size should be confirmed with real load testing.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response