Preparing your design workspace

Loading the problem and your saved attempt…

Design and Implement a Thread-Safe In-Memory Task Queue

Build a bounded task queue with FIFO claim order, race-safe cancellation, retained status, callback isolation, and explicit shutdown behavior.

Design and implement a thread-safe in-memory task queue for one application process. The source reports a task-queue design-and-code exercise with follow-ups.

For this exercise, assume multiple producers submit one-shot Runnable tasks to a bounded queue and a configurable set of workers claim pending tasks in FIFO submission order; callback start and completion order may differ. Producers may wait only up to a caller-supplied timeout when capacity is full.

A returned handle can cancel work only before a worker claims it, and task status remains queryable for a bounded retention period after completion. User callbacks never run while queue state is synchronized; callback exceptions become failed outcomes without killing the queue.

Graceful shutdown rejects new tasks and drains accepted work, while immediate shutdown cancels pending work but does not claim it can forcibly stop callbacks already running. Durable restart recovery, delayed or recurring work, priorities, distributed workers, and exactly-once external side effects are outside scope.