Skip to content

CodesbyNeo/Task-scheduler

Repository files navigation

⚙️ Custom Thread Pool Task Scheduler

CI Java Maven License Tests

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.


🚀 Tech Stack

Layer Technology
Language Java 17
Concurrency Thread, PriorityBlockingQueue, AtomicLong
Scheduling Custom ScheduledTask runner
Testing JUnit 5
Build Maven

✨ Features

  • 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

🏗️ Architecture

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 running flag ensures safe visibility across threads
  • AtomicLong for stats — lock-free and thread-safe
  • Tasks implement Comparable<Task> for natural priority ordering

⚡ Quick Start

Prerequisites

  • 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 Coverage

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

📊 Sample Output

[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
=======================================

📄 License

MIT

About

Custom thread pool & task scheduler built from scratch in Java. Features priority queuing, graceful shutdown, scheduled tasks, and live stats tracking. No ExecutorService — pure concurrency fundamentals.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages