A thread pool and task scheduler built from scratch in Java — no ExecutorService doing the heavy lifting. Demonstrates deep understanding of concurrency, synchronization, and systems design.
| Layer | Technology |
|---|---|
| Language | Java 17 |
| Concurrency | Thread, PriorityBlockingQueue, AtomicLong |
| Scheduling | Custom ScheduledTask runner |
| Testing | JUnit 5 |
| Build | Maven |
- ✅ Custom ThreadPool — manages a fixed pool of worker threads
- ✅ Priority Queue — HIGH priority tasks execute before MEDIUM and LOW
- ✅ Graceful Shutdown — drains the queue before stopping workers
- ✅ Forced Shutdown — interrupts all workers immediately
- ✅ Scheduled Tasks — run tasks after a delay or at a fixed rate
- ✅ Stats Tracking — completed tasks, failed tasks, avg wait & exec time
- ✅ Thread-safe — uses atomic operations throughout, no data races
- ✅ 7 JUnit tests covering execution, priority, concurrency, and failure
ThreadPool
├── TaskQueue (PriorityBlockingQueue)
│ └── Task (name, runnable, priority, submittedAt)
├── WorkerThread × N (poll queue → execute task → record stats)
├── ThreadPoolStats (AtomicLong counters)
└── ScheduledTask (fires tasks into pool after delay/interval)
Key design decisions:
- Workers block on
queue.poll(500ms)— no busy-waiting, CPU friendly volatile boolean runningflag ensures safe visibility across threadsAtomicLongfor stats — lock-free and thread-safe- Tasks implement
Comparable<Task>for natural priority ordering
- Java 17+
- Maven 3.8+
git clone https://github.com/YOUR_USERNAME/task-scheduler.git
cd task-scheduler
# Run the demo
mvn compile exec:java -Dexec.mainClass="com.taskscheduler.Main"
# Run tests
mvn test
# Build JAR
mvn package
java -jar target/task-scheduler.jar| Test | What it verifies |
|---|---|
testTasksAreExecuted |
All submitted tasks run to completion |
testStatsTracking |
Completed task count is accurate |
testFailedTaskStats |
Exceptions are caught and counted |
testTaskPriorityOrdering |
HIGH > MEDIUM > LOW ordering |
testRejectionAfterShutdown |
No tasks accepted post-shutdown |
testQueueCapacity |
Queue enforces capacity limit |
testConcurrentExecution |
Multiple tasks run simultaneously |
[ThreadPool] Started with 3 worker threads.
[TaskQueue] Submitted Task[id=1, name='BasicTask-1', priority=MEDIUM]
[Worker-1] Executing Task[id=1, name='BasicTask-1', priority=MEDIUM] (waited 2ms)
[Worker-2] Executing Task[id=2, name='BasicTask-2', priority=MEDIUM] (waited 1ms)
...
========== Thread Pool Stats ==========
Tasks completed : 50
Tasks failed : 0
Avg wait time : 4.32 ms
Avg exec time : 51.20 ms
=======================================
MIT