- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathSelfAdjustingScheduling.java
63 lines (52 loc) · 1.65 KB
/
SelfAdjustingScheduling.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
packagecom.thealgorithms.scheduling;
importjava.util.PriorityQueue;
/**
* SelfAdjustingScheduling is an algorithm where tasks dynamically adjust
* their priority based on real-time feedback, such as wait time and CPU usage.
* Tasks that wait longer will automatically increase their priority,
* allowing for better responsiveness and fairness in task handling.
*
* Use Case: Real-time systems that require dynamic prioritization
* of tasks to maintain system responsiveness and fairness.
*
* @author Hardvan
*/
publicfinalclassSelfAdjustingScheduling {
privatestaticclassTaskimplementsComparable<Task> {
Stringname;
intwaitTime;
intpriority;
Task(Stringname, intpriority) {
this.name = name;
this.waitTime = 0;
this.priority = priority;
}
voidincrementWaitTime() {
waitTime++;
priority = priority + waitTime;
}
@Override
publicintcompareTo(Taskother) {
returnInteger.compare(this.priority, other.priority);
}
}
privatefinalPriorityQueue<Task> taskQueue;
publicSelfAdjustingScheduling() {
taskQueue = newPriorityQueue<>();
}
publicvoidaddTask(Stringname, intpriority) {
taskQueue.offer(newTask(name, priority));
}
publicStringscheduleNext() {
if (taskQueue.isEmpty()) {
returnnull;
}
TasknextTask = taskQueue.poll();
nextTask.incrementWaitTime();
taskQueue.offer(nextTask);
returnnextTask.name;
}
publicbooleanisEmpty() {
returntaskQueue.isEmpty();
}
}