- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathSlackTimeScheduling.java
64 lines (55 loc) · 1.78 KB
/
SlackTimeScheduling.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
packagecom.thealgorithms.scheduling;
importjava.util.ArrayList;
importjava.util.Comparator;
importjava.util.List;
/**
* SlackTimeScheduling is an algorithm that prioritizes tasks based on their
* slack time, which is defined as the difference between the task's deadline
* and the time required to execute it. Tasks with less slack time are prioritized.
*
* Use Case: Real-time systems with hard deadlines, such as robotics or embedded systems.
*
* @author Hardvan
*/
publicclassSlackTimeScheduling {
staticclassTask {
Stringname;
intexecutionTime;
intdeadline;
Task(Stringname, intexecutionTime, intdeadline) {
this.name = name;
this.executionTime = executionTime;
this.deadline = deadline;
}
intgetSlackTime() {
returndeadline - executionTime;
}
}
privatefinalList<Task> tasks;
publicSlackTimeScheduling() {
tasks = newArrayList<>();
}
/**
* Adds a task to the scheduler.
*
* @param name the name of the task
* @param executionTime the time required to execute the task
* @param deadline the deadline by which the task must be completed
*/
publicvoidaddTask(Stringname, intexecutionTime, intdeadline) {
tasks.add(newTask(name, executionTime, deadline));
}
/**
* Schedules the tasks based on their slack time.
*
* @return the order in which the tasks should be executed
*/
publicList<String> scheduleTasks() {
tasks.sort(Comparator.comparingInt(Task::getSlackTime));
List<String> scheduledOrder = newArrayList<>();
for (Tasktask : tasks) {
scheduledOrder.add(task.name);
}
returnscheduledOrder;
}
}