Skip to content

feat: add Scheduler pattern (#76) - #3631

Open
tiennm99 wants to merge 6 commits into
iluwatar:masterfrom
tiennm99:feat/scheduler_pattern
Open

tiennm99 wants to merge 6 commits into
iluwatar:masterfrom
tiennm99:feat/scheduler_pattern

Conversation

@tiennm99

Copy link
Copy Markdown
Contributor

Closes #76.

Adds the Scheduler pattern, which separates the work (tasks), the moment it becomes ready (triggers) and the order it runs in (scheduling strategies).

What is included

  • Task interface (Task) with an AbstractTask base that handles time accounting, and two task types: SimpleTask and SumTask, whose work pauses and resumes between time slices.
  • Triggers: TimeTrigger (once at a fixed time), IntervalTrigger (repeating, like a cron job) and ConditionTrigger (once, when a condition becomes true, such as another task completing). ScheduledJob pairs a trigger with a task factory.
  • Schedulers behind TaskScheduler: first come first served, priority, round robin and shortest remaining time first.
  • Execution context: Simulator advances a clock, fires triggers, hands tasks to the scheduler and records completion times.
  • App runs one workload that uses all three trigger types through every scheduler.
  • README following the pattern template, class diagram (etc/scheduler.urm.puml and .png), and 21 unit tests.

This supersedes #2584 and #2794 and addresses their review comments (tags and README template, parent-managed pom settings, App entry point).

Verification

./mvnw -pl scheduler clean verify license:check passes.

…uwatar#76)

Make Task an interface with an AbstractTask base and two task types,
add time, interval and condition triggers paired with tasks through
ScheduledJob, and let the Simulator act as the execution context.
Replace the Main class with App, align the module pom and README with
the current project template, and cover triggers, tasks, the simulator
and App with tests.
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

PR Summary

Introduces the Scheduler design pattern: decouples work (Task), readiness (Trigger), and execution order (TaskScheduler). Adds Task/AbstractTask, SimpleTask and SumTask, Time/Interval/Condition triggers, ScheduledJob pairings, multiple schedulers (FCFS, Priority, Round Robin, SRTF), and a Simulator execution context with App as entry point. Includes documentation, diagrams, and comprehensive tests.

Changes

File Summary
pom.xml Module build updated to include the new scheduler module in the parent Maven project.
scheduler/README.md New README detailing the Scheduler pattern: intent, components (Task, Trigger, Scheduler), examples, and test coverage.
scheduler/etc/scheduler.urm.png New UML diagram PNG illustrating Scheduler pattern structure and relationships.
scheduler/etc/scheduler.urm.puml New PlantUML diagram describing the Scheduler pattern architecture (tasks, triggers, schedulers, simulator).
scheduler/pom.xml New module pom for the scheduler submodule including dependencies and mainClass configuration for App.
scheduler/src/main/java/com/iluwatar/scheduler/AbstractTask.java New base class for tasks handling time accounting; concrete tasks implement doWork(int).
scheduler/src/main/java/com/iluwatar/scheduler/App.java New App as entry point; runs workload through all schedulers using Simulator.
scheduler/src/main/java/com/iluwatar/scheduler/ConditionTrigger.java New Trigger that fires once when a given condition becomes true.
scheduler/src/main/java/com/iluwatar/scheduler/FirstComeFirstServedScheduler.java New FCFS scheduler: executes tasks to completion in arrival order.
scheduler/src/main/java/com/iluwatar/scheduler/IntervalTrigger.java New trigger that fires starting at start and every period units; validates period > 0.
scheduler/src/main/java/com/iluwatar/scheduler/PriorityScheduler.java New priority-based scheduler using a max-priority queue; ties broken by creation order (id).
scheduler/src/main/java/com/iluwatar/scheduler/RoundRobinScheduler.java New RoundRobin scheduler cycling through tasks one time slice at a time.
scheduler/src/main/java/com/iluwatar/scheduler/ScheduledJob.java New pairing of a Trigger with a Task factory; factory used to create fresh tasks per trigger firing.
scheduler/src/main/java/com/iluwatar/scheduler/ShortestRemainingTimeFirstScheduler.java New SRTF scheduler using remaining time as queue key; ties broken by creation order.
scheduler/src/main/java/com/iluwatar/scheduler/SimpleTask.java New SimpleTask that reports progress; extends AbstractTask.
scheduler/src/main/java/com/iluwatar/scheduler/Simulator.java New Simulator as the execution context: advances time, fires triggers, schedules tasks and tracks completions.
scheduler/src/main/java/com/iluwatar/scheduler/SumTask.java New SumTask that sums 1..n with work spread across time slices.
scheduler/src/main/java/com/iluwatar/scheduler/Task.java New Task interface defining id, priority, remaining time, completion status and execute() method.
scheduler/src/main/java/com/iluwatar/scheduler/TaskScheduler.java New TaskScheduler interface with methods to schedule and update tasks.
scheduler/src/main/java/com/iluwatar/scheduler/TimeTrigger.java New TimeTrigger fires once at a fixed time.
scheduler/src/main/java/com/iluwatar/scheduler/Trigger.java New Trigger interface to decide when a job should hand a new task to the scheduler.
scheduler/src/test/java/com/iluwatar/scheduler/AppTest.java Test suite ensuring App runs without exceptions and workloads cover all trigger types.
scheduler/src/test/java/com/iluwatar/scheduler/FirstComeFirstServedSchedulerTest.java Tests verifying FCFS ordering and completion times.
scheduler/src/test/java/com/iluwatar/scheduler/PrioritySchedulerTest.java Tests verifying PriorityScheduler behavior and completion order.
scheduler/src/test/java/com/iluwatar/scheduler/RoundRobinSchedulerTest.java Tests verifying RoundRobin behavior and completion order.
scheduler/src/test/java/com/iluwatar/scheduler/ShortestRemainingTimeFirstSchedulerTest.java Tests verifying Shortest Remaining Time First behavior.
scheduler/src/test/java/com/iluwatar/scheduler/SimulatorTest.java Tests covering interval and condition triggers, and delta-time validation.
scheduler/src/test/java/com/iluwatar/scheduler/TaskTest.java Tests for task lifecycle, completion, and resume behavior (SumTask).
scheduler/src/test/java/com/iluwatar/scheduler/TriggerTest.java Tests for TimeTrigger, IntervalTrigger, and ConditionTrigger.

autogenerated by presubmit.ai

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ LGTM!

Review Summary

Commits Considered (6)
  • f748000: docs: add Scheduler pattern class diagram
  • b2dd9f9: feat: complete Scheduler pattern with task interface and triggers (#76)

Make Task an interface with an AbstractTask base and two task types,
add time, interval and condition triggers paired with tasks through
ScheduledJob, and let the Simulator act as the execution context.
Replace the Main class with App, align the module pom and README with
the current project template, and cover triggers, tasks, the simulator
and App with tests.

  • c95b6e9: [Scheduler] (Fix) SonarLint issues
  • 98cabae: [Scheduler Pattern] (Fix) base on iluwatar's comments
  • 30b4cba: [Scheduler Pattern] (Fix) check style problems
  • f1ca66b: [Scheduler Pattern] (Add) scheduler pattern
Files Processed (29)
  • pom.xml (1 hunk)
  • scheduler/README.md (1 hunk)
  • scheduler/etc/scheduler.urm.png (0 hunks)
  • scheduler/etc/scheduler.urm.puml (1 hunk)
  • scheduler/pom.xml (1 hunk)
  • scheduler/src/main/java/com/iluwatar/scheduler/AbstractTask.java (1 hunk)
  • scheduler/src/main/java/com/iluwatar/scheduler/App.java (1 hunk)
  • scheduler/src/main/java/com/iluwatar/scheduler/ConditionTrigger.java (1 hunk)
  • scheduler/src/main/java/com/iluwatar/scheduler/FirstComeFirstServedScheduler.java (1 hunk)
  • scheduler/src/main/java/com/iluwatar/scheduler/IntervalTrigger.java (1 hunk)
  • scheduler/src/main/java/com/iluwatar/scheduler/PriorityScheduler.java (1 hunk)
  • scheduler/src/main/java/com/iluwatar/scheduler/RoundRobinScheduler.java (1 hunk)
  • scheduler/src/main/java/com/iluwatar/scheduler/ScheduledJob.java (1 hunk)
  • scheduler/src/main/java/com/iluwatar/scheduler/ShortestRemainingTimeFirstScheduler.java (1 hunk)
  • scheduler/src/main/java/com/iluwatar/scheduler/SimpleTask.java (1 hunk)
  • scheduler/src/main/java/com/iluwatar/scheduler/Simulator.java (1 hunk)
  • scheduler/src/main/java/com/iluwatar/scheduler/SumTask.java (1 hunk)
  • scheduler/src/main/java/com/iluwatar/scheduler/Task.java (1 hunk)
  • scheduler/src/main/java/com/iluwatar/scheduler/TaskScheduler.java (1 hunk)
  • scheduler/src/main/java/com/iluwatar/scheduler/TimeTrigger.java (1 hunk)
  • scheduler/src/main/java/com/iluwatar/scheduler/Trigger.java (1 hunk)
  • scheduler/src/test/java/com/iluwatar/scheduler/AppTest.java (1 hunk)
  • scheduler/src/test/java/com/iluwatar/scheduler/FirstComeFirstServedSchedulerTest.java (1 hunk)
  • scheduler/src/test/java/com/iluwatar/scheduler/PrioritySchedulerTest.java (1 hunk)
  • scheduler/src/test/java/com/iluwatar/scheduler/RoundRobinSchedulerTest.java (1 hunk)
  • scheduler/src/test/java/com/iluwatar/scheduler/ShortestRemainingTimeFirstSchedulerTest.java (1 hunk)
  • scheduler/src/test/java/com/iluwatar/scheduler/SimulatorTest.java (1 hunk)
  • scheduler/src/test/java/com/iluwatar/scheduler/TaskTest.java (1 hunk)
  • scheduler/src/test/java/com/iluwatar/scheduler/TriggerTest.java (1 hunk)
Actionable Comments (0)
Skipped Comments (4)
  • scheduler/src/main/java/com/iluwatar/scheduler/App.java [53-53]

    readability: "Logger field naming consistency"

  • scheduler/src/main/java/com/iluwatar/scheduler/SimpleTask.java [43-45]

    readability: "Logging usage consistency in SimpleTask"

  • scheduler/src/main/java/com/iluwatar/scheduler/IntervalTrigger.java [31-35]

    best_practice: "Input validation in IntervalTrigger constructor"

  • scheduler/src/main/java/com/iluwatar/scheduler/Simulator.java [80-87]

    possible_bug: "Potential nondeterministic completion ordering"

@codecov

codecov Bot commented Nov 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.26471% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 84.15%. Comparing base (4cabb20) to head (f748000).

Files with missing lines Patch % Lines
...uler/src/main/java/com/iluwatar/scheduler/App.java 93.75% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3631      +/-   ##
============================================
+ Coverage     84.06%   84.15%   +0.09%     
- Complexity     4353     4413      +60     
============================================
  Files          1133     1146      +13     
  Lines         15400    15536     +136     
  Branches        739      754      +15     
============================================
+ Hits          12946    13075     +129     
- Misses         2158     2163       +5     
- Partials        296      298       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scheduler pattern

1 participant