Card 01/ 09
Thread Pools: Handing Work to a Fixed Set of Threads
Ten thousand short tasks through four long-lived threads, instead of ten thousand threads.
A thread pool is a fixed set of threads that stay alive and take tasks from a queue, one after another. In Java it is an ExecutorService, the interface for running submitted tasks on a pool: you submit tasks to it, and it decides which of its threads runs each one and when. Executors.newFixedThreadPool(4) creates a pool of four threads, and submit adds a task to its queue.
Ten thousand short tasks, one new Thread each: the program spends more time creating and destroying threads than doing the work, and if the tasks overlap, every one of them holds a thread and its stack at once. A thread runs once and cannot be restarted, so a thread per task really is a new thread for every task.
ExecutorService pool = Executors.newFixedThreadPool(4);
for (Path file : files) {
pool.submit(() -> process(file));
}| You decide | The pool decides |
|---|---|
| What work there is | Which thread runs it |
| When to submit it | When it starts |
| How many threads exist | Which thread is free |
Four threads handle ten thousand tasks, each taking the next one whenever it finishes the last. Nothing is created per task except the lambda, and the number of threads no longer depends on the amount of work.
So a pool is a decision about capacity rather than scheduling: you choose how much of the machine this work may use, and the pool takes care of the rest.
← → move through sections and cards