AlgoViz

Thread pool

medium 70 min

A few threads that live long, one queue with a limit, and a loop: take, run, repeat.

Watch lesson5:06

Where it shows up

The same pattern appears across everyday software.

  • Java ThreadPoolExecutor

    Core and maximum size, any blocking queue, four ready rejection policies, hooks before and after each task. Core threads start when tasks arrive, unless they are prestarted.

  • Java ForkJoinPool

    Work stealing. A process-wide common pool serves tasks that are not given to another pool. Its threads are daemons.

  • Python concurrent.futures

    `ThreadPoolExecutor` uses min(32, process CPUs + 4) workers by default. Its documentation shows how a task that waits for another one of the same pool deadlocks.

  • Apache Tomcat

    Request threads grow up to `maxThreads`, 200 by default. Beyond that, connections are accepted up to a limit and then wait in the operating system's queue of `acceptCount` places.

  • nginx

    Serves requests with event loops in worker processes, not a thread per request. A named `thread_pool` takes over blocking file reads: 32 threads and a queue of 65,536 by default, and an error when it overflows.

  • libuv and Node.js

    File system calls and name lookups run on a pool of 4 threads by default, changed with `UV_THREADPOOL_SIZE`, at most 1,024, shared by all event loops of the process.

  • Node.js worker_threads

    Real threads for work that computes. The manual advises a pool of workers, because creating one per task would cost more than it brings.

  • Tokio (Rust)

    The multi-thread runtime steals work: one worker per core by default, a global queue and a local queue each. Blocking code goes to a separate set of threads with `spawn_blocking`.

  • .NET thread pool

    Creates threads on demand up to a minimum, then may create more or wait for tasks to finish, with throughput as its goal.

  • Grand Central Dispatch

    Apple's system: you put blocks of work on queues, and the system runs them on threads that it manages.

  • Celery and Sidekiq

    Background jobs from a broker. Celery's workers use a pool, processes by default (prefork). A Sidekiq process runs 5 threads by default.

  • Chromium

    Exactly one thread pool per process with shared task queues. Its guide says you should pretty much never create your own thread, and asks for sequences instead of locks.

  • Data engines and consumers

    A Spark executor runs tasks on its cores. A Kafka consumer is not thread-safe: one thread polls, and the records may be handed to a pool. Game servers use a pool for work that a tick can split.