Thread Pools
Table of Contents + −
A busy server handles thousands of requests, and each one needs a thread to do its work. The obvious idea is: every time a request arrives, make a new thread for it. Simple, right? But that idea breaks down fast:
- Making a new thread takes time and memory, every single time.
- If a million requests hit at once, you’d try to make a million threads, and the server would run out of memory and crash.
So creating a thread per task doesn’t scale. The fix is a thread pool. Let’s see how it works.
🎯 What is a Thread Pool
A thread pool is a fixed set of threads kept ready to do work. Instead of creating a new thread for each task, you reuse the threads you already have.
Here’s the idea:
- When the server starts, it creates a set number of threads, say 10, and keeps them waiting.
- Tasks come in and line up in a queue.
- A free thread picks up the next task, does it, and then goes back to grab another. Over and over.
So the threads are reused again and again, instead of being made and thrown away. Like a team of workers who finish one job and pick up the next, rather than hiring a new worker for every single task and firing them after.
Reuse, don't recreate
The whole point of a thread pool is reuse. You pay the cost of making the threads once, at the start. After that, those same threads handle task after task, which is much cheaper than making a new one each time.
🏗️ How It Works
Let’s walk the flow. Tasks arrive, wait in a queue, and the pool’s threads pull from it.
Reading that: tasks pile into a queue, the pool’s threads each grab one, finish it, and come straight back for the next. The same few threads keep cycling through the work.
🛡️ Why It Protects the Server
Here’s the part that matters most for system design. A thread pool puts a limit on how many things run at once. That limit is what keeps the server safe.
- With a pool of 10 threads, at most 10 tasks run at the same time. Extra tasks just wait politely in the queue.
- So even if a million requests flood in, the server doesn’t try to do a million things at once. It does 10 at a time and works through the line.
- This stops the server from being overwhelmed and crashing. It stays steady under heavy load.
Compare that to thread-per-task: a sudden flood would create endless threads and bring the whole thing down. The pool’s fixed size is a safety valve.
📏 Choosing the Pool Size
So how many threads should a pool have? It’s a balance.
- Too few and tasks wait a long time in the queue, even when the server could do more.
- Too many and the threads fight over the CPU and memory, and you lose the benefit.
The right number depends on the work and the machine. The point to remember is that the size is a deliberate choice, a knob you tune, not something left to chance.
The queue can fill up too
If tasks arrive faster than the pool can finish them, the waiting queue grows. Real systems set a limit on that queue too, so it doesn’t grow forever and eat all the memory. When it’s full, new tasks are rejected or slowed down on purpose.
📊 Thread Per Task vs Thread Pool
Here’s the comparison.
| Point | Thread Per Task | Thread Pool |
|---|---|---|
| Thread creation | New one every task (costly) | Made once, reused |
| Under a flood of tasks | Endless threads, can crash | Fixed limit, extras wait |
| Server stability | Fragile under load | Steady under load |
⚠️ Common Mistakes and Misconceptions
A few things to keep straight:
- “More threads always means faster.” No. Past a point, extra threads just fight over the CPU and slow everything down. The right size matters more than a big size.
- “A thread pool can handle unlimited load.” It can’t do unlimited work at once, on purpose. It limits how many run together. That’s the feature, not a bug. The waiting queue still needs a limit.
- “Thread pools remove the need for locks.” No. The pooled threads still share memory, so you still need locks around shared data.
🧩 What You’ve Learned
Nice work. Here’s the recap:
- ✅ Making a new thread per task is costly and can crash a server under a flood of requests.
- ✅ A thread pool keeps a fixed set of threads and reuses them for task after task.
- ✅ The fixed size limits how many tasks run at once, which keeps the server steady under heavy load.
- ✅ Pool size is a knob you tune: too few means waiting, too many means fighting over resources.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What is the main idea of a thread pool?
Why: A thread pool creates threads once and reuses them, which is much cheaper than making a new thread per task.
- 2
How does a thread pool protect a server under heavy load?
Why: A fixed pool size means only that many tasks run at once. A flood of requests just waits in line instead of crashing the server.
- 3
Why is making a new thread per task a problem?
Why: Each new thread costs time and memory. Under a sudden flood, endless thread creation can exhaust memory and crash the server.
- 4
Is bigger always better for pool size?
Why: Past a point, extra threads just compete for resources. The right size is a balance, not simply the biggest number.
🚀 What’s Next?
That wraps up concurrency. Let’s connect it back to the bigger system.
- Locks & Synchronization is what keeps pooled threads safe when they share data.
- What is Load Balancing? spreads work across many servers, the next level up from threads.
Get these down and you’ll understand how a system handles many tasks at once, from one machine up to many.