[Scheduler Pattern] (Fix) base on iluwatar's comments

This commit is contained in:
2023-10-16 20:47:05 +07:00
committed by tiennm5
parent d9512df57b
commit 3b46af6190
4 changed files with 70 additions and 10 deletions
+49 -2
View File
@@ -2,7 +2,8 @@
title: Scheduler Pattern
category: Creational
language: en
tag:
tag:
- Generic
---
## Name
@@ -12,7 +13,53 @@ Scheduler Design Pattern
The Scheduler Design Pattern is used to manage and coordinate the execution of tasks or jobs in a system. It provides a mechanism for scheduling and executing tasks at specific times, intervals, or in response to certain events. This pattern is especially useful when dealing with asynchronous operations, background processing, and resource allocation.
## Explanation
The Scheduler Design Pattern is designed to decouple task scheduling from the actual execution of tasks. It abstracts the scheduling logic, making it possible to change or extend the scheduling behavior without affecting the tasks themselves. This pattern allows for efficient resource utilization, load balancing, and prioritization of tasks.
### Real-world example
> Think of a restaurant kitchen with various dishes to be prepared the chef is like a scheduler who ensures that each dish is cooked at the right time and served to customers without letting the kitchen become chaotic or the food getting cold.
### In plain words
> The Scheduler Design Pattern ensures your tasks get done at the right time and order.
### Wikipedia says
> In computing, scheduling is the action of assigning resources to perform tasks. The resources may be processors, network links or expansion cards. The tasks may be threads, processes or data flows.
> The scheduling activity is carried out by a process called scheduler. Schedulers are often designed so as to keep all computer resources busy (as in load balancing), allow multiple users to share system resources effectively, or to achieve a target quality-of-service.
> Scheduling is fundamental to computation itself, and an intrinsic part of the execution model of a computer system; the concept of scheduling makes it possible to have computer multitasking with a single central processing unit (CPU).
### Programmatic example
In our demo, we will have a class `Task` with following attributes:
```java
public class Task {
int id;
int totalExecutionTime;
int priority = 0;
}
```
And a `Scheduler` class which will be responsible for scheduling the tasks:
```java
public interface TaskScheduler {
void scheduleTask(Task task);
}
```
Base on the strategy of the scheduler, we can have different implementations of `TaskScheduler` interface. For example, a `PriorityScheduler` which will schedule the tasks based on their priority:
```java
public class PriorityScheduler implements TaskScheduler {
private final Queue<Task> taskQueue =
new PriorityQueue<>(
(task1, task2) -> {
if (task2.priority != task1.priority) {
return task2.priority - task1.priority;
}
return task1.id - task2.id;
});
@Override
public void scheduleTask(Task task) {
taskQueue.add(task);
}
}
```
## Class diagram
![Scheduler Pattern](etc/scheduler.png)
-6
View File
@@ -11,16 +11,10 @@
<artifactId>scheduler</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
</dependencies>
@@ -0,0 +1,16 @@
package com.iluwatar.scheduler;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<Integer, List<Task>> tasks = new HashMap<>();
tasks.put(0, List.of(new Task(1, 3), new Task(2, 2), new Task(3, 4)));
TaskScheduler scheduler = new FirstComeFirstServedScheduler();
Simulator simulator = new Simulator(scheduler, tasks, 1, 100);
simulator.simulate();
}
}
@@ -37,8 +37,7 @@ public class Task {
elapsedTime += seconds;
// Uncomment the following line to see the execution of tasks
// System.out.printf("%d, %d/%d\n", id, elapsedTime, totalExecutionTime);
logTaskProgress();
if (elapsedTime >= totalExecutionTime) {
complete = true;
@@ -46,6 +45,10 @@ public class Task {
}
}
private void logTaskProgress() {
System.out.printf("Task %d is %d/%d complete\n", id, elapsedTime, totalExecutionTime);
}
public int getRemainingTime() {
return totalExecutionTime - elapsedTime;
}