Design and implement a thread-safe Delayed Queue that can schedule and execute tasks after a specified delay. The queue must support the following operations:
schedule(Runnable task, long delay, TimeUnit unit) – Schedules a task to run after the given delay. Each task has a unique string ID; if a duplicate ID is submitted, the previous task is cancelled and the new one is scheduled instead.
start() – Starts a background worker thread that continuously checks the queue and executes tasks whose delays have expired.
stop() – Gracefully shuts down the worker thread.
Internally, use a min-heap (priority queue) ordered by the task’s execution timestamp. Protect shared state with a mutex and use a condition variable to efficiently wake the worker when a new task arrives or an earlier task is scheduled. Handle edge cases such as tasks scheduled in the past (execute immediately), duplicate task IDs (cancel previous), and spurious wakeups. Ensure that the worker thread re-checks the heap after waking up in case an earlier task was added.