Implement a Thread Pool With Futures and Graceful Shutdown
Last updated: November 29, 2026
Quick Overview
Implement a fixed-size thread pool where submit returns a future whose result can be awaited, task exceptions are delivered to the caller, and shutdown can either drain queued work or cancel it.
Microsoft
November 29, 20260
0
0 solved
Implement a fixed-size thread pool where submit returns a future whose result can be awaited, task exceptions are delivered to the caller, and shutdown can either drain queued work or cancel it.
A deep concurrency question used in Microsoft onsites for backend and platform teams, similar in spirit to how .NET's thread pool and Task infrastructure behave. Interviewers watch for running user code outside the lock, propagating exceptions, a shutdown protocol with no lost or hung tasks, and clean worker exit.
What the Interviewer Expects
- Workers block on a condition variable rather than busy-waiting
- User tasks run outside the pool lock
- Exceptions are captured and re-raised from future.result()
- Shutdown rejects new work and lets workers exit once the queue drains
- Optional cancellation completes pending futures with a cancellation error
- Discusses sizing for CPU-bound versus IO-bound work
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would you add a bounded queue with back-pressure on submit?
- How would you support task cancellation for tasks already running?
- What changes if tasks can submit other tasks and wait on them (deadlock risk)?
- How would you grow and shrink the worker count dynamically?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
ThreadPool(k)starts k workers.submit(fn, *args)returns aFuture;future.result(timeout)blocks and returns the value or re-raises the task's exception.- `shutdown(wait=True, cancel_pen...
High-Level Design
A deque of tasks guarded by one Condition. Workers loop: wait until there is a task or shutdown is set; if shutdown is set and the queue is empty, exit. Pop a task, release the lock, run it, and...