Repository: TheAlgorithms/Java
Pull request #5811 (2024-10-14 13:16:10)

feat: Add `SelfAdjustingScheduling` 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/5811?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `95.23810%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 65.53%. 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 [(`cb39b75`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/cb39b7567f94fcbae430aa5d761b46e717be66d6?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5811?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...algorithms/scheduling/SelfAdjustingScheduling.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5811?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fscheduling%2FSelfAdjustingScheduling.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9zY2hlZHVsaW5nL1NlbGZBZGp1c3RpbmdTY2hlZHVsaW5nLmphdmE=) | 95.23% | [1 Missing :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5811?src=pr&el=tree&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    #5811      +/-   ##
============================================
+ Coverage     65.49%   65.53%   +0.04%     
- Complexity     4339     4344       +5     
============================================
  Files           599      600       +1     
  Lines         16742    16763      +21     
  Branches       3212     3212              
============================================
+ Hits          10965    10986      +21     
- Misses         5332     5333       +1     
+ Partials        445      444       -1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5811?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 a1bbbf9a..f6e7ebe4 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -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)
diff --git a/src/main/java/com/thealgorithms/scheduling/SelfAdjustingScheduling.java b/src/main/java/com/thealgorithms/scheduling/SelfAdjustingScheduling.java
new file mode 100644
index 00000000..15159a07
--- /dev/null
+++ b/src/main/java/com/thealgorithms/scheduling/SelfAdjustingScheduling.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();
+    }
+}


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