forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Preemptive Priority Scheduling Algorithm (TheAlgorithms#4323)
- Loading branch information
1 parent
af80c80
commit 4bcddd3
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |