Repository: TheAlgorithms/Java
Pull request #5813 (2024-10-14 13:23:44)

feat: Add `AgingScheduling` new algorithm with Junit tests
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5813?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 65.52%. Comparing base [(`bcf4034`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/bcf4034ce58ed965d1d885e1b0bf85a920f57e2d?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`e6eb0c4`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/e6eb0c4f076961bb792e1c962452f7adb3ed04ec?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5813      +/-   ##
============================================
+ Coverage     65.49%   65.52%   +0.02%     
- Complexity     4339     4344       +5     
============================================
  Files           599      600       +1     
  Lines         16742    16759      +17     
  Branches       3212     3212              
============================================
+ Hits          10965    10981      +16     
- Misses         5332     5333       +1     
  Partials        445      445              
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5813?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 774a1860..476308a3 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -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)
@@ -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)
diff --git a/src/main/java/com/thealgorithms/scheduling/AgingScheduling.java b/src/main/java/com/thealgorithms/scheduling/AgingScheduling.java
new file mode 100644
index 00000000..1e5512be
--- /dev/null
+++ b/src/main/java/com/thealgorithms/scheduling/AgingScheduling.java
@@ -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;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/scheduling/AgingSchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/AgingSchedulingTest.java
new file mode 100644
index 00000000..cab78e4d
--- /dev/null
+++ b/src/test/java/com/thealgorithms/scheduling/AgingSchedulingTest.java
@@ -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());
+    }
+}