Concurrent task scheduler
viaGlassdoor
Problem Implement a concurrent task scheduler that runs tasks across a worker pool while respecting inter-task dependencies and avoiding race conditions.
Requirements
submit(task, dependsOn[]); a task may run only after all its dependencies complete.- Execute ready tasks in parallel across a fixed worker pool.
- Support graceful shutdown and surface task failures.
Core design
- Model tasks as a DAG; track each task's unmet-dependency count (in-degree).
- A thread-safe ready queue holds tasks with zero unmet dependencies; workers pull from it. On completion, decrement dependents and enqueue any that reach zero.
- Back the pool with an
ExecutorService/ thread pool; use aCountDownLatch/futures to await completion.
Discussion points
- Thread-safety of the dependency counts and ready queue (atomic decrement, concurrent collections).
- Backpressure when submissions outpace workers; bounded queues.
- Failure policy: cancel dependents, retry, or continue; cycle detection at submit time.
- Graceful shutdown: drain in-flight tasks, reject new submissions.
asked …