Repository: TheAlgorithms/Java
Pull request #4323 (2023-08-20 11:38:16)

Preemptive Priority Scheduling Algorithm added
<!-- 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.


BamaCharanChhandogi: Hey! I understand my Problem. But I can not understand what is the difference.
siriak: There is whitespace before the function declaration
BamaCharanChhandogi: Do I like this?
<img width="695" alt="image" src="https://github.com/TheAlgorithms/Java/assets/69814563/b24b72c1-7d99-4222-9bc6-1d630e7b0bbc">


Patch:
diff --git a/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java b/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java
new file mode 100644
index 00000000..bf1097fb
--- /dev/null
+++ b/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java
@@ -0,0 +1,54 @@
+package com.thealgorithms.scheduling;
+
+import java.util.*;
+
+/**
+ * Preemptive Priority Scheduling Algorithm
+ * @author [Bama Charan Chhandogi](https://www.github.com/BamaCharanChhandogi)
+ */
+
+class Process {
+    String name;
+    int arrivalTime;
+    int burstTime;
+    int priority;
+
+    public Process(String name, int arrivalTime, int burstTime, int priority) {
+        this.name = name;
+        this.arrivalTime = arrivalTime;
+        this.burstTime = burstTime;
+        this.priority = priority;
+    }
+}
+
+public class PreemptivePriorityScheduling {
+    public static List<String> preemptivePriorityScheduling(List<Process> processes) {
+        List<String> ganttChart = new ArrayList<>();
+        PriorityQueue<Process> readyQueue = new PriorityQueue<>(Comparator.comparingInt(p -> - p.priority));
+
+        int currentTime = 0;
+
+        while (!processes.isEmpty() || !readyQueue.isEmpty()) {
+            while (!processes.isEmpty() && processes.get(0).arrivalTime <= currentTime) {
+                readyQueue.add(processes.remove(0));
+            }
+
+            if (!readyQueue.isEmpty()) {
+                Process currentProcess = readyQueue.poll();
+
+                ganttChart.add(currentProcess.name);
+                currentProcess.burstTime--;
+
+                if (currentProcess.burstTime > 0) {
+                    readyQueue.add(currentProcess);
+                }
+            } else {
+                ganttChart.add("Idle");
+            }
+
+            currentTime++;
+        }
+
+        return ganttChart;
+    }
+}
\ No newline at end of file


Test patch:
diff --git a/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java
new file mode 100644
index 00000000..aa0b2195
--- /dev/null
+++ b/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java
@@ -0,0 +1,32 @@
+package com.thealgorithms.scheduling;
+
+/**
+ * Test Cases of Preemptive Priority Scheduling Algorithm
+ * @author [Bama Charan Chhandogi](https://www.github.com/BamaCharanChhandogi)
+ */
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.util.*;
+import org.junit.jupiter.api.Test;
+
+class PreemptivePrioritySchedulingTest {
+
+    @Test
+    void testPreemptivePriorityScheduling() {
+        // Arrange
+        List<Process> processes = new ArrayList<>();
+        processes.add(new Process("P1", 0, 5, 10));
+        processes.add(new Process("P2", 1, 4, 20));
+        processes.add(new Process("P3", 2, 2, 30));
+        processes.add(new Process("P4", 4, 1, 40));
+
+        List<String> expectedGanttChart = Arrays.asList("P1", "P2", "P3", "P3", "P4", "P2", "P2", "P2", "P1", "P1", "P1", "P1");
+
+        // Act
+        List<String> actualGanttChart = PreemptivePriorityScheduling.preemptivePriorityScheduling(processes);
+
+        // Assert
+        assertEquals(expectedGanttChart, actualGanttChart);
+    }
+}
\ No newline at end of file