Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add AgingScheduling new algorithm with Junit tests #5813

Merged
merged 5 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@
* Recursion
* [GenerateSubsets](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java)
* scheduling
* [AgingScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/AgingScheduling.java)
* diskscheduling
* [CircularLookScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/diskscheduling/CircularLookScheduling.java)
* [CircularScanScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/diskscheduling/CircularScanScheduling.java)
Expand Down Expand Up @@ -1123,6 +1124,7 @@
* Recursion
* [GenerateSubsetsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java)
* scheduling
* [AgingSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/AgingSchedulingTest.java)
* diskscheduling
* [CircularLookSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/diskscheduling/CircularLookSchedulingTest.java)
* [CircularScanSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/diskscheduling/CircularScanSchedulingTest.java)
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/com/thealgorithms/scheduling/AgingScheduling.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.thealgorithms.scheduling;

import java.util.LinkedList;
import java.util.Queue;

/**
* AgingScheduling is an algorithm designed to prevent starvation
* by gradually increasing the priority of waiting tasks.
* The longer a process waits, the higher its priority becomes.
*
* Use Case: Useful in systems with mixed workloads to avoid
* lower-priority tasks being starved by higher-priority tasks.
*
* @author Hardvan
*/
public final class AgingScheduling {

static class Task {
String name;
int waitTime;
int priority;

Task(String name, int priority) {
this.name = name;
this.priority = priority;
this.waitTime = 0;
}
}

private final Queue<Task> taskQueue;

public AgingScheduling() {
taskQueue = new LinkedList<>();
}

/**
* Adds a task to the scheduler with a given priority.
*
* @param name name of the task
* @param priority priority of the task
*/
public void addTask(String name, int priority) {
taskQueue.offer(new Task(name, priority));
}

/**
* Schedules the next task based on the priority and wait time.
* The priority of a task increases with the time it spends waiting.
*
* @return name of the next task to be executed
*/
public String scheduleNext() {
if (taskQueue.isEmpty()) {
return null;
}
Task nextTask = taskQueue.poll();
nextTask.waitTime++;
nextTask.priority += nextTask.waitTime;
taskQueue.offer(nextTask);
return nextTask.name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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 AgingSchedulingTest {

private AgingScheduling scheduler;

@BeforeEach
public void setup() {
scheduler = new AgingScheduling();
}

@Test
public void testAddAndScheduleSingleTask() {
scheduler.addTask("Task1", 5);
assertEquals("Task1", scheduler.scheduleNext());
}

@Test
public void testAddMultipleTasks() {
scheduler.addTask("Task1", 1);
scheduler.addTask("Task2", 1);
assertEquals("Task1", scheduler.scheduleNext());
assertEquals("Task2", scheduler.scheduleNext());
}

@Test
public void testPriorityAdjustmentWithWait() {
scheduler.addTask("Task1", 1);
scheduler.addTask("Task2", 1);
scheduler.scheduleNext();
scheduler.scheduleNext();
assertEquals("Task1", scheduler.scheduleNext());
}

@Test
public void testEmptyScheduler() {
assertNull(scheduler.scheduleNext());
}

@Test
public void testMultipleRounds() {
scheduler.addTask("Task1", 1);
scheduler.addTask("Task2", 2);
scheduler.scheduleNext();
scheduler.scheduleNext();
assertEquals("Task1", scheduler.scheduleNext());
}
}