thread pool
**Thread Pool** — a collection of pre-created worker threads that execute tasks from a shared queue, avoiding the overhead of creating and destroying threads for each task.
**Why Thread Pools?**
- Thread creation is expensive (~10-100 microseconds per thread)
- Too many threads → context switch overhead, memory waste (each thread needs ~1MB stack)
- Thread pool: Create N threads once, reuse them forever
**Architecture**
```
[Task Queue] → [Worker Thread 1] → execute task → return to pool
→ [Worker Thread 2] → execute task → return to pool
→ [Worker Thread N] → ...
```
**Sizing**
- CPU-bound tasks: threads = number of CPU cores
- I/O-bound tasks: threads = cores x (1 + wait_time/compute_time) — more threads because they spend time waiting
- Over-sizing: Wasted memory and context switches
- Under-sizing: Tasks queue up, throughput drops
**Implementations**
- Java: `ExecutorService`, `ForkJoinPool`
- C++: Custom or libraries (TBB, BS::thread_pool)
- Python: `concurrent.futures.ThreadPoolExecutor`
- Go: Goroutines (runtime-managed green thread pool)
**Thread pools** are the standard pattern for handling concurrent workloads in servers, web applications, and parallel algorithms.