In-Memory Delayed Task Scheduler & Priority Job Queue
Task schedulers test your mastery of ordered priority queues, condition variables to prevent CPU spinning, cancellation mechanisms, and retry exponential backoffs.
Clarifying Concurrency & Execution Models:
“Should tasks execute synchronously on a single dispatcher thread or be handed off to a thread pool to avoid blocking other tasks?”
“When two tasks share the exact same scheduled timestamp, should priority break ties, followed by insertion order?”
“If an executed task throws an unhandled exception, should it retry with exponential backoff or be moved directly to a Dead Letter Queue?”
“Do we need support for `cancel(taskId)` before the task triggers?”
The Anti-Pattern: Busy-Spinning with `while (true) sleep(100)`
Polling the queue every 100 milliseconds is an immediate red flag in a leadership loop. It consumes idle CPU cycles and introduces up to 100ms of latency jitter.
Use a Min-Heap (PriorityQueue) ordered by `runAtTimestamp`. The background worker thread peeks at the head of the heap. If the scheduled time is in the future, it sleeps on a Condition variable with a timeout exactly equal to `runAt - now`. If a new earlier task is inserted, it notifies the condition to wake the worker immediately.
Full 200-line code is collapsed to preserve screen focus. Click expand to inspect min-heap comparator math, thread worker logic, and unit tests.