Skip to content

Commit 648572a

Browse files
authored
Refactor ProcessDetails and PreemptivePriorityScheduling (#5448)
* Refactor ProcessDetails and PreemptivePriorityScheduling for consistency * fix formatting * fix formatting * Improve test readability and maintainability
1 parent 65e3264 commit 648572a

File tree

3 files changed

+55
-46
lines changed

3 files changed

+55
-46
lines changed

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

+12
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
}
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,42 @@
11
package com.thealgorithms.scheduling;
22

3+
import com.thealgorithms.devutils.entities.ProcessDetails;
34
import java.util.ArrayList;
45
import java.util.Comparator;
56
import java.util.List;
67
import java.util.PriorityQueue;
78

89
/**
910
* Preemptive Priority Scheduling Algorithm
11+
*
1012
* @author [Bama Charan Chhandogi](https://www.github.com/BamaCharanChhandogi)
1113
*/
14+
public class PreemptivePriorityScheduling {
15+
protected final List<ProcessDetails> processes;
16+
protected final List<String> ganttChart;
1217

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;
18+
public PreemptivePriorityScheduling(List<ProcessDetails> processes) {
19+
this.processes = new ArrayList<>(processes);
20+
this.ganttChart = new ArrayList<>();
2421
}
25-
}
2622

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));
23+
public void scheduleProcesses() {
24+
PriorityQueue<ProcessDetails> readyQueue = new PriorityQueue<>(Comparator.comparingInt(ProcessDetails::getPriority).reversed().thenComparingInt(ProcessDetails::getArrivalTime));
3325

3426
int currentTime = 0;
27+
List<ProcessDetails> arrivedProcesses = new ArrayList<>();
3528

3629
while (!processes.isEmpty() || !readyQueue.isEmpty()) {
37-
while (!processes.isEmpty() && processes.get(0).arrivalTime <= currentTime) {
38-
readyQueue.add(processes.remove(0));
39-
}
30+
updateArrivedProcesses(currentTime, arrivedProcesses);
31+
readyQueue.addAll(arrivedProcesses);
32+
arrivedProcesses.clear();
4033

4134
if (!readyQueue.isEmpty()) {
42-
Process currentProcess = readyQueue.poll();
35+
ProcessDetails currentProcess = readyQueue.poll();
36+
ganttChart.add(currentProcess.getProcessId());
37+
currentProcess.setBurstTime(currentProcess.getBurstTime() - 1);
4338

44-
ganttChart.add(currentProcess.name);
45-
currentProcess.burstTime--;
46-
47-
if (currentProcess.burstTime > 0) {
39+
if (currentProcess.getBurstTime() > 0) {
4840
readyQueue.add(currentProcess);
4941
}
5042
} else {
@@ -53,7 +45,15 @@ public static List<String> preemptivePriorityScheduling(List<Process> processes)
5345

5446
currentTime++;
5547
}
48+
}
5649

57-
return ganttChart;
50+
private void updateArrivedProcesses(int currentTime, List<ProcessDetails> arrivedProcesses) {
51+
processes.removeIf(process -> {
52+
if (process.getArrivalTime() <= currentTime) {
53+
arrivedProcesses.add(process);
54+
return true;
55+
}
56+
return false;
57+
});
5858
}
5959
}

src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java

+16-19
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,29 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import java.util.ArrayList;
6-
import java.util.Arrays;
5+
import com.thealgorithms.devutils.entities.ProcessDetails;
76
import java.util.List;
8-
import org.junit.jupiter.api.Test;
7+
import java.util.stream.Stream;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
911

1012
/**
1113
* Test Cases of Preemptive Priority Scheduling Algorithm
14+
*
1215
* @author [Bama Charan Chhandogi](https://www.github.com/BamaCharanChhandogi)
1316
*/
1417
class PreemptivePrioritySchedulingTest {
18+
@ParameterizedTest
19+
@MethodSource("provideProcessesAndExpectedSchedules")
20+
void testPreemptivePriorityScheduling(List<ProcessDetails> processes, List<String> expectedSchedule) {
21+
PreemptivePriorityScheduling scheduler = new PreemptivePriorityScheduling(processes);
22+
scheduler.scheduleProcesses();
23+
assertEquals(expectedSchedule, scheduler.ganttChart);
24+
}
1525

16-
@Test
17-
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));
24-
25-
List<String> expectedGanttChart = Arrays.asList("P1", "P2", "P3", "P3", "P4", "P2", "P2", "P2", "P1", "P1", "P1", "P1");
26-
27-
// Act
28-
List<String> actualGanttChart = PreemptivePriorityScheduling.preemptivePriorityScheduling(processes);
29-
30-
// Assert
31-
assertEquals(expectedGanttChart, actualGanttChart);
26+
static Stream<Arguments> provideProcessesAndExpectedSchedules() {
27+
return Stream.of(Arguments.of(List.of(new ProcessDetails("P1", 0, 5, 2), new ProcessDetails("P2", 1, 4, 4), new ProcessDetails("P3", 2, 2, 6), new ProcessDetails("P4", 4, 1, 8)), List.of("P1", "P2", "P3", "P3", "P4", "P2", "P2", "P2", "P1", "P1", "P1", "P1")),
28+
Arguments.of(List.of(new ProcessDetails("P1", 2, 5, 3), new ProcessDetails("P2", 5, 3, 5), new ProcessDetails("P3", 7, 1, 9)), List.of("Idle", "Idle", "P1", "P1", "P1", "P2", "P2", "P3", "P2", "P1", "P1")));
3229
}
3330
}

0 commit comments

Comments
 (0)