mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 17:56:02 +08:00
Add SelfAdjustingScheduling
algorithm (#5811)
This commit is contained in:
@ -539,6 +539,7 @@
|
||||
* [NonPreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java)
|
||||
* [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java)
|
||||
* [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java)
|
||||
* [SelfAdjustingScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SelfAdjustingScheduling.java)
|
||||
* [SJFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SJFScheduling.java)
|
||||
* [SlackTimeScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SlackTimeScheduling.java)
|
||||
* [SRTFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SRTFScheduling.java)
|
||||
@ -1136,6 +1137,7 @@
|
||||
* [NonPreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java)
|
||||
* [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
|
||||
* [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java)
|
||||
* [SelfAdjustingSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SelfAdjustingSchedulingTest.java)
|
||||
* [SJFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SJFSchedulingTest.java)
|
||||
* [SlackTimeSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SlackTimeSchedulingTest.java)
|
||||
* [SRTFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SRTFSchedulingTest.java)
|
||||
|
@ -0,0 +1,63 @@
|
||||
package com.thealgorithms.scheduling;
|
||||
|
||||
import java.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
|
||||
*/
|
||||
public final class SelfAdjustingScheduling {
|
||||
|
||||
private static class Task implements Comparable<Task> {
|
||||
String name;
|
||||
int waitTime;
|
||||
int priority;
|
||||
|
||||
Task(String name, int priority) {
|
||||
this.name = name;
|
||||
this.waitTime = 0;
|
||||
this.priority = priority;
|
||||
}
|
||||
|
||||
void incrementWaitTime() {
|
||||
waitTime++;
|
||||
priority = priority + waitTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Task other) {
|
||||
return Integer.compare(this.priority, other.priority);
|
||||
}
|
||||
}
|
||||
|
||||
private final PriorityQueue<Task> taskQueue;
|
||||
|
||||
public SelfAdjustingScheduling() {
|
||||
taskQueue = new PriorityQueue<>();
|
||||
}
|
||||
|
||||
public void addTask(String name, int priority) {
|
||||
taskQueue.offer(new Task(name, priority));
|
||||
}
|
||||
|
||||
public String scheduleNext() {
|
||||
if (taskQueue.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
Task nextTask = taskQueue.poll();
|
||||
nextTask.incrementWaitTime();
|
||||
taskQueue.offer(nextTask);
|
||||
return nextTask.name;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return taskQueue.isEmpty();
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package com.thealgorithms.scheduling;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SelfAdjustingSchedulingTest {
|
||||
|
||||
private SelfAdjustingScheduling scheduler;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
scheduler = new SelfAdjustingScheduling();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddAndScheduleSingleTask() {
|
||||
scheduler.addTask("Task1", 5);
|
||||
assertEquals("Task1", scheduler.scheduleNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddMultipleTasks() {
|
||||
scheduler.addTask("Task1", 5);
|
||||
scheduler.addTask("Task2", 1);
|
||||
scheduler.addTask("Task3", 3);
|
||||
assertEquals("Task2", scheduler.scheduleNext());
|
||||
assertEquals("Task2", scheduler.scheduleNext());
|
||||
assertEquals("Task3", scheduler.scheduleNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPriorityAdjustment() {
|
||||
scheduler.addTask("Task1", 1);
|
||||
scheduler.addTask("Task2", 1);
|
||||
scheduler.scheduleNext();
|
||||
scheduler.scheduleNext();
|
||||
scheduler.scheduleNext();
|
||||
assertEquals("Task2", scheduler.scheduleNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyScheduler() {
|
||||
assertNull(scheduler.scheduleNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTaskReschedulingAfterWait() {
|
||||
scheduler.addTask("Task1", 1);
|
||||
scheduler.addTask("Task2", 2);
|
||||
scheduler.scheduleNext();
|
||||
scheduler.scheduleNext();
|
||||
assertEquals("Task1", scheduler.scheduleNext());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user