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
Software Engineering Fundamentals
Software Engineer
Microsoft
November 29, 2026
Software Engineer
Onsite
Software Engineering Fundamentals
Hard

0

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
thread pools
futures
shutdown protocols
exception propagation
producer-consumer
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. 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 Problems
Sample Answer
Requirements
  • ThreadPool(k) starts k workers.
  • submit(fn, *args) returns a Future; 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...


Submit Your Answer
Markdown supported