The labs on this site need JavaScript to run. The same content is written out below.
Learn › Java › Threads and concurrency
Adding threads does not keep making things faster
Why it exists — If a new thread is created every time a task arrives, the number of threads grows without limit when requests pile up. Threads added after the CPU is already fully busy add no throughput and only take up memory, and creating and destroying threads costs something for every task. A thread pool creates a fixed number of threads in advance and reuses them, so the number of threads running at the same time can be capped to suit the kind of work.
Where it's used today — Tomcat handles requests with threads from a thread pool (server.tomcat.threads.max), and in Java code, tasks such as image conversion or external API calls are handed to a pool with ExecutorService. For computation-heavy tasks the pool size is set near the number of cores, and for tasks that wait for a database or an external response it is set much larger. For tasks that mostly wait, virtual threads are sometimes used instead.
A CPU core runs only one thread at any given moment. When more threads want to run than there are cores, the operating system runs them in turn, each for a short time (context switching). Throughput then stops growing, and only the time for one task to finish gets longer. The right number of threads is completely different depending on whether the tasks mostly compute or mostly wait. The numbers on screen are values actually measured on Linux with JDK 21.
Checked against real execution · Values actually measured for all 54 combinations with JDK 21 on Linux
int cores = Runtime.getRuntime().availableProcessors(); // 4
// mostly computation: as many threads as cores is enough
ExecutorService pool = Executors.newFixedThreadPool(16);
for (Photo photo : photos) {
pool.submit(() -> resize(photo)); // one task = about 10ms of computation
}
// Photo, photos, and resize() are names made up for this example (only the needed part is shown)
The numbers on screen are not calculated by a model. All of them are measured values. A program that queues tasks in a fixed-size thread pool, as in the code on screen, was run in 54 combinations in a Linux container (Docker Desktop, kernel 6.18.33.2-microsoft-standard-WSL2, JDK 21.0.13.1 from the official image eclipse-temurin:21-jre). The number of cores was set by pinning the process to 1, 2, or 4 different physical cores with the --cpuset-cpus option of Docker (the CPU is AMD Ryzen 5 5600 6-Core Processor). A task that mostly computes is a fixed amount of computation (about 10ms on one core when nothing interferes), and for a task that mostly waits, Thread.sleep(90) after the same computation stands in for waiting for an external response. Other programs were running on the same computer during the measurement. So for each combination, 6 seconds were measured and the values of the 3 consecutive seconds with the most finished tasks were taken, this was repeated 4 times, and the values of the repetition with the highest throughput are shown on screen. Some variation still remains (for example, the throughput per second of 4 threads that mostly compute on 4 cores was 312.7, 302.3, 348.0, 346.7 across the repetitions), so small differences between nearby values carry no meaning, and the numbers vary with the computer and from run to run. CPU usage is the CPU time used by the process divided by the measurement time and the number of cores, so it also includes time used by JVM threads other than the worker threads (in some combinations with only one or two threads it is higher than the calculation), and the number of context switches is the sum of voluntary_ctxt_switches and nonvoluntary_ctxt_switches, which the Linux kernel counts for each thread and reports in /proc. The numbers of threads in each state in the diagram are averages calculated from the measured CPU usage and throughput. "Number of cores × (1 + wait time ÷ compute time)" is a widely cited guideline in the field of Java concurrency. The default maximum of 200 threads for the embedded Tomcat in Spring Boot was not verified in this run. It is the value in the Spring Boot documentation.
Previous lab: Thread pool · Next lab: Sequential and parallel calls
Struck-through text was removed from the first version of the code; highlighted text was added.