Skip to content

Commit 7e43774

Browse files
committed
Refactor ProcessDetails and PreemptivePriorityScheduling for consistency
1 parent 65e3264 commit 7e43774

File tree

3 files changed

+68
-42
lines changed

3 files changed

+68
-42
lines changed

src/main/java/com/thealgorithms/devutils/entities/ProcessDetails.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ public class ProcessDetails {
66
private int burstTime;
77
private int waitingTime;
88
private int turnAroundTime;
9+
private int priority;
10+
11+
public ProcessDetails(final String processId, final int arrivalTime, final int burstTime, int priority) {
12+
this.processId = processId;
13+
this.arrivalTime = arrivalTime;
14+
this.burstTime = burstTime;
15+
this.priority = priority;
16+
}
917

1018
public ProcessDetails(final String processId, final int arrivalTime, final int burstTime) {
1119
this.processId = processId;
@@ -33,6 +41,10 @@ public int getTurnAroundTimeTime() {
3341
return turnAroundTime;
3442
}
3543

44+
public int getPriority() {
45+
return priority;
46+
}
47+
3648
public void setProcessId(final String processId) {
3749
this.processId = processId;
3850
}
Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,47 @@
11
package com.thealgorithms.scheduling;
22

3+
import com.thealgorithms.devutils.entities.ProcessDetails;
4+
35
import java.util.ArrayList;
46
import java.util.Comparator;
57
import java.util.List;
68
import java.util.PriorityQueue;
79

810
/**
911
* Preemptive Priority Scheduling Algorithm
12+
*
1013
* @author [Bama Charan Chhandogi](https://www.github.com/BamaCharanChhandogi)
1114
*/
15+
public class PreemptivePriorityScheduling {
16+
protected final List<ProcessDetails> processes;
17+
protected final List<String> ganttChart;
1218

13-
class Process {
14-
String name;
15-
int arrivalTime;
16-
int burstTime;
17-
int priority;
18-
19-
Process(String name, int arrivalTime, int burstTime, int priority) {
20-
this.name = name;
21-
this.arrivalTime = arrivalTime;
22-
this.burstTime = burstTime;
23-
this.priority = priority;
19+
public PreemptivePriorityScheduling(List<ProcessDetails> processes) {
20+
this.processes = new ArrayList<>(processes);
21+
this.ganttChart = new ArrayList<>();
2422
}
25-
}
2623

27-
public final class PreemptivePriorityScheduling {
28-
private PreemptivePriorityScheduling() {
29-
}
30-
public static List<String> preemptivePriorityScheduling(List<Process> processes) {
31-
List<String> ganttChart = new ArrayList<>();
32-
PriorityQueue<Process> readyQueue = new PriorityQueue<>(Comparator.comparingInt(p -> - p.priority));
24+
public void scheduleProcesses() {
25+
PriorityQueue<ProcessDetails> readyQueue = new PriorityQueue<>(
26+
Comparator.comparingInt(ProcessDetails::getPriority).reversed()
27+
.thenComparingInt(ProcessDetails::getArrivalTime)
28+
);
3329

3430
int currentTime = 0;
31+
int processIndex = 0;
3532

36-
while (!processes.isEmpty() || !readyQueue.isEmpty()) {
37-
while (!processes.isEmpty() && processes.get(0).arrivalTime <= currentTime) {
38-
readyQueue.add(processes.remove(0));
33+
while (processIndex < processes.size() || !readyQueue.isEmpty()) {
34+
while (processIndex < processes.size() && processes.get(processIndex).getArrivalTime() <= currentTime) {
35+
readyQueue.add(processes.get(processIndex));
36+
processIndex++;
3937
}
4038

4139
if (!readyQueue.isEmpty()) {
42-
Process currentProcess = readyQueue.poll();
40+
ProcessDetails currentProcess = readyQueue.poll();
41+
ganttChart.add(currentProcess.getProcessId());
42+
currentProcess.setBurstTime(currentProcess.getBurstTime() - 1);
4343

44-
ganttChart.add(currentProcess.name);
45-
currentProcess.burstTime--;
46-
47-
if (currentProcess.burstTime > 0) {
44+
if (currentProcess.getBurstTime() > 0) {
4845
readyQueue.add(currentProcess);
4946
}
5047
} else {
@@ -53,7 +50,5 @@ public static List<String> preemptivePriorityScheduling(List<Process> processes)
5350

5451
currentTime++;
5552
}
56-
57-
return ganttChart;
5853
}
5954
}
Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,52 @@
11
package com.thealgorithms.scheduling;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
3+
import com.thealgorithms.devutils.entities.ProcessDetails;
4+
import org.junit.jupiter.api.Test;
45

56
import java.util.ArrayList;
6-
import java.util.Arrays;
77
import java.util.List;
8-
import org.junit.jupiter.api.Test;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
910

1011
/**
1112
* Test Cases of Preemptive Priority Scheduling Algorithm
13+
*
1214
* @author [Bama Charan Chhandogi](https://www.github.com/BamaCharanChhandogi)
1315
*/
1416
class PreemptivePrioritySchedulingTest {
1517

1618
@Test
1719
void testPreemptivePriorityScheduling() {
18-
// Arrange
19-
List<Process> processes = new ArrayList<>();
20-
processes.add(new Process("P1", 0, 5, 10));
21-
processes.add(new Process("P2", 1, 4, 20));
22-
processes.add(new Process("P3", 2, 2, 30));
23-
processes.add(new Process("P4", 4, 1, 40));
20+
List<ProcessDetails> processes = new ArrayList<>();
21+
processes.add(new ProcessDetails("P1", 0, 5, 2));
22+
processes.add(new ProcessDetails("P2", 1, 4, 4));
23+
processes.add(new ProcessDetails("P3", 2, 2, 6));
24+
processes.add(new ProcessDetails("P4", 4, 1, 8));
25+
26+
PreemptivePriorityScheduling scheduler = new PreemptivePriorityScheduling(processes);
27+
scheduler.scheduleProcesses();
28+
29+
List<String> expectedSchedule = List.of(
30+
"P1", "P2", "P3", "P3", "P4", "P2", "P2", "P2", "P1", "P1", "P1", "P1"
31+
);
32+
33+
assertEquals(expectedSchedule, scheduler.ganttChart);
34+
}
35+
36+
@Test
37+
void testPreemptivePrioritySchedulingWithIdleTime() {
38+
List<ProcessDetails> processes = new ArrayList<>();
39+
processes.add(new ProcessDetails("P1", 2, 5, 3));
40+
processes.add(new ProcessDetails("P2", 5, 3, 5));
41+
processes.add(new ProcessDetails("P3", 7, 1, 9));
2442

25-
List<String> expectedGanttChart = Arrays.asList("P1", "P2", "P3", "P3", "P4", "P2", "P2", "P2", "P1", "P1", "P1", "P1");
43+
PreemptivePriorityScheduling scheduler = new PreemptivePriorityScheduling(processes);
44+
scheduler.scheduleProcesses();
2645

27-
// Act
28-
List<String> actualGanttChart = PreemptivePriorityScheduling.preemptivePriorityScheduling(processes);
46+
List<String> expectedSchedule = List.of(
47+
"Idle", "Idle", "P1", "P1", "P1", "P2", "P2", "P3", "P2", "P1", "P1"
48+
);
2949

30-
// Assert
31-
assertEquals(expectedGanttChart, actualGanttChart);
50+
assertEquals(expectedSchedule, scheduler.ganttChart);
3251
}
3352
}

0 commit comments

Comments
 (0)