Design a Thread Pool
🧵 What is a thread pool? It is a small team of long-lived worker threads plus a queue of tasks. Callers drop tasks into the queue and get a receipt (a future) back right away. Each worker takes the next task, runs it, puts the result on the receipt, and goes back for more. Starting a thread is slow and every thread costs memory, so a pool starts a few once and reuses them for thousands of tasks.
Java's ExecutorService, Python's concurrent.futures.ThreadPoolExecutor and the worker pools behind most web servers all follow this design. In an interview the request usually comes as "design a thread pool" or "implement an executor service", and the follow-ups are always the same four: how does submit hand back a result, what happens to a task that throws, what happens when the queue is full, and how do you shut it down without losing work. Each of those is a requirement below.
The class count is small, like the bounded blocking queue this design is built around. What the interviewer grades is whether your workers sleep and wake correctly, whether you know what code runs inside the lock and what runs outside it, and whether shutdown really finishes.
Requirements
"Design a thread pool. Callers submit tasks, a fixed number of threads run them, and callers can get the results."
Clarifying Questions
You: Is the number of worker threads fixed, or should the pool grow under load?
Interviewer: Fixed, set in the constructor. Growing is a follow-up.
You: How does a caller get a task's result?
Interviewer: submit returns a future right away. Calling get() on it waits until the task is done and returns the value.
You: What if a task throws an exception?
Interviewer: The caller should see the error when they call get(). And the pool must keep working.
You: Is the waiting queue bounded? What happens when it is full?
Interviewer: Yes, it has a capacity. When it is full, either reject the task, or have the caller run it themselves. We want to choose that per pool, and we may add other policies later.
You: What does shutdown mean? Do queued tasks still run?
Interviewer: After shutdown, no new tasks are accepted, but everything already submitted still runs. There should also be a way to wait until all workers have finished.
You: Do we need priorities, delayed tasks or cancellation?
Interviewer: Not now.
You: Can I use the standard library executor?
Interviewer: No, build it from threads, a lock and condition variables.
The fourth answer is the one people skip. An unbounded queue hides problems until memory runs out, and a bounded one forces you to decide what happens when it fills up. The interviewer also said "may add other policies later", which is a hint about where an interface belongs.
💡 Interview tip: Ask about failing tasks early. "A task throws and the worker thread dies" is the most common real bug in hand-written pools: after a few bad tasks, there are no workers left and every future waits forever. Saying this out loud shows you have seen pools fail in practice.
Final Requirements
- The pool starts a fixed number of worker threads when it is created.
submit(task)puts the task in a queue and returns a future at once. A task is a function with no arguments that returns a value.- Each accepted task runs exactly once, on some worker.
future.get()waits until its task has run and returns the result. If the task threw,get()raises an error instead. A failing task never stops its worker.- At most
capacitytasks can wait in the queue. When it is full, a rejection policy decides:ABORTrefuses the task (submitreturns nothing),CALLER_RUNSruns the task in the submitting thread. New policies must be easy to add. shutdown()returns immediately. After it,submitrefuses every task, but tasks already queued or running finish. Then the workers exit.await_termination()waits until every worker has exited.- Idle workers sleep; they use no CPU while waiting for work.
Out of scope
- Changing the number of workers at runtime (a follow-up).
- Priorities and delayed or periodic tasks (see /low-level-design/job-scheduler and /low-level-design/delayed-scheduler).
- Cancelling tasks and timeouts on
get()(follow-ups).