Thread pool là tập hợp các worker threads được tạo sẵn và tái sử dụng, thay vì tạo/hủy thread mới cho mỗi task — vì tạo thread tốn ~1MB stack, time để OS allocate, và context switch overhead.
- Hoạt động: task queue nhận công việc, worker thread nhàn rỗi lấy task từ queue, thực thi, rồi chờ task tiếp theo.
- ThreadPoolExecutor (Java),
worker_threadspool (Node.js), Goroutine worker pattern (Go). - Sizing là bài toán quan trọng: với CPU-bound tasks, pool size = số CPU cores (thêm thread không giúp, chỉ tốn context switch); với I/O-bound tasks, pool size lớn hơn số CPU (vì thread chờ I/O không chiếm CPU) — formula tham khảo:
threads = cores * (1 + wait_time/service_time). - Pool quá nhỏ: task queue dài, high latency.
- Pool quá lớn: memory tốn kém, nhiều context switch, performance giảm.
- Java Tomcat default pool size 200; Node.js libuv I/O thread pool default 4 (UV_THREADPOOL_SIZE).
A thread pool is a set of pre-created worker threads that are reused, rather than spawning and destroying a new thread for every task — because thread creation costs ~1MB of stack space, OS allocation time, and context-switch overhead.
How it works: a task queue receives incoming work, an idle worker thread picks up a task, executes it, then waits for the next one. Examples: ThreadPoolExecutor (Java), worker_threads pool (Node.js), goroutine worker pattern (Go). Sizing is critical: for CPU-bound tasks, pool size = number of CPU cores (adding more threads only increases context switching with no gain); for I/O-bound tasks, the pool can be larger than the core count (since threads waiting on I/O do not consume CPU) — a reference formula is threads = cores * (1 + wait_time / service_time). Too small a pool: long task queues and high latency. Too large a pool: wasted memory, excessive context switching, and degraded performance. Java Tomcat defaults to a pool size of 200; Node.js libuv's I/O thread pool defaults to 4 (configurable via UV_THREADPOOL_SIZE).