mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 12:58:37 +00:00
[Scheduler Pattern] (Fix) check style problems
This commit is contained in:
@@ -5,6 +5,7 @@ import java.beans.PropertyChangeListener;
|
||||
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<>();
|
||||
|
||||
@@ -17,7 +18,9 @@ public class FirstComeFirstServedScheduler implements TaskScheduler, PropertyCha
|
||||
@Override
|
||||
public void update(int deltaTime) {
|
||||
Task task = taskQueue.peek();
|
||||
if (task == null) return;
|
||||
if (task == null) {
|
||||
return;
|
||||
}
|
||||
task.execute(deltaTime);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,12 +5,14 @@ import java.beans.PropertyChangeListener;
|
||||
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
|
||||
});
|
||||
|
||||
@@ -23,7 +25,9 @@ public class PriorityScheduler implements TaskScheduler, PropertyChangeListener
|
||||
@Override
|
||||
public void update(int deltaTime) {
|
||||
Task task = taskQueue.peek();
|
||||
if (task == null) return;
|
||||
if (task == null) {
|
||||
return;
|
||||
}
|
||||
task.execute(deltaTime);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,10 @@ package com.iluwatar.scheduler;
|
||||
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<>();
|
||||
|
||||
@@ -14,8 +18,12 @@ public class RoundRobinScheduler implements TaskScheduler {
|
||||
@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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+9
-4
@@ -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
|
||||
});
|
||||
|
||||
@@ -21,8 +22,12 @@ public class ShortestRemainingTimeFirstScheduler implements TaskScheduler {
|
||||
@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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.beans.PropertyChangeSupport;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/** Task that need to be scheduled. */
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public class Task {
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user