Skip to content

Commit

Permalink
[Scheduler Pattern] (Fix) check style problems
Browse files Browse the repository at this point in the history
  • Loading branch information
tiennm99 committed Sep 10, 2023
1 parent e5095eb commit df26238
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.LinkedList;
import java.util.Queue;

/**Tasks are scheduled in the order they arrive. */
public class FirstComeFirstServedScheduler implements TaskScheduler, PropertyChangeListener {
private final Queue<Task> taskQueue = new LinkedList<>();

Expand All @@ -17,7 +18,9 @@ public void scheduleTask(Task task) {
@Override
public void update(int deltaTime) {
Task task = taskQueue.peek();
if (task == null) return;
if (task == null) {
return;
}
task.execute(deltaTime);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import java.util.PriorityQueue;
import java.util.Queue;

/** Tasks with higher priority values are executed before tasks with lower priority values. */
public class PriorityScheduler implements TaskScheduler, PropertyChangeListener {
private final Queue<Task> taskQueue =
new PriorityQueue<>(
(task1, task2) -> {
if (task2.getPriority() != task1.getPriority())
if (task2.getPriority() != task1.getPriority()) {
return task2.getPriority() - task1.getPriority();
}
return task1.getId() - task2.getId(); // lower id (earlier task) has higher priority
});

Expand All @@ -23,7 +25,9 @@ public void scheduleTask(Task task) {
@Override
public void update(int deltaTime) {
Task task = taskQueue.peek();
if (task == null) return;
if (task == null) {
return;
}
task.execute(deltaTime);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import java.util.LinkedList;
import java.util.Queue;

/**
* Round Robin technique. Tasks are executed in a cyclic order, with each task getting a fixed time
* quantum for execution.
*/
public class RoundRobinScheduler implements TaskScheduler {
private final Queue<Task> taskQueue = new LinkedList<>();

Expand All @@ -14,8 +18,12 @@ public void scheduleTask(Task task) {
@Override
public void update(int deltaTime) {
Task task = taskQueue.poll();
if (task == null) return;
if (task == null) {
return;
}
task.execute(deltaTime);
if (!task.isComplete()) taskQueue.add(task);
if (!task.isComplete()) {
taskQueue.add(task);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.iluwatar.scheduler;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

/** The task with the shortest remaining execution time is given priority. */
public class ShortestRemainingTimeFirstScheduler implements TaskScheduler {
private final Queue<Task> taskQueue =
new PriorityQueue<>(
(task1, task2) -> {
if (task2.getRemainingTime() != task1.getRemainingTime())
if (task2.getRemainingTime() != task1.getRemainingTime()) {
return task1.getRemainingTime() - task2.getRemainingTime();
}
return task1.getId() - task2.getId(); // lower id (earlier task) has higher priority
});

Expand All @@ -21,8 +22,12 @@ public void scheduleTask(Task task) {
@Override
public void update(int deltaTime) {
Task task = taskQueue.poll();
if (task == null) return;
if (task == null) {
return;
}
task.execute(deltaTime);
if (!task.isComplete()) taskQueue.add(task);
if (!task.isComplete()) {
taskQueue.add(task);
}
}
}
4 changes: 4 additions & 0 deletions scheduler/src/main/java/com/iluwatar/scheduler/Simulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public class Simulator implements PropertyChangeListener {
private final LinkedHashMap<Integer, Integer> taskCompletedOrder = new LinkedHashMap<>();
private int elapsedTime = 0;

/**
* Simulate scheduler schedule tasks, then return a LinkedHashMap present the completed order of
* tasks, which map task id to the time it completed.
*/
public LinkedHashMap<Integer, Integer> simulate() {
while (elapsedTime < simulateTime) {
if (tasks.containsKey(elapsedTime)) {
Expand Down
10 changes: 9 additions & 1 deletion scheduler/src/main/java/com/iluwatar/scheduler/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;

/** Task that need to be scheduled. */
@Getter
@RequiredArgsConstructor
public class Task {
Expand All @@ -15,17 +16,24 @@ public class Task {
/** The priority of the task. The higher the number, the higher the priority. */
private int priority = 0;

/** The time that the task run. */
private int elapsedTime = 0;

/** Whether the task is completed. */
private boolean complete = false;

/** Create a task with id, total execution time and priority. */
public Task(int id, int totalExecutionTime, int priority) {
this.id = id;
this.totalExecutionTime = totalExecutionTime;
this.priority = priority;
}

/** Execute the task for a given number of seconds. */
public void execute(int seconds) {
if (complete) throw new IllegalStateException("Task already completed");
if (complete) {
throw new IllegalStateException("Task already completed");
}

elapsedTime += seconds;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.iluwatar.scheduler;


/** The scheduler is responsible for scheduling tasks. */
public interface TaskScheduler {
/** Add task to the scheduler */
/** Add task to the scheduler. */
void scheduleTask(Task task);

/** Update to execute scheduled tasks */
/** Update to execute scheduled tasks. */
void update(int deltaTime);
}

0 comments on commit df26238

Please sign in to comment.