Refactor ProcessDetails and PreemptivePriorityScheduling (#5448)

* Refactor ProcessDetails and PreemptivePriorityScheduling for consistency

* fix formatting

* fix formatting

* Improve test readability and maintainability
This commit is contained in:
doxxx 2024-09-11 21:49:36 +09:00 committed by GitHub
parent 65e32641fc
commit 648572a8c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 55 additions and 46 deletions

View File

@ -6,6 +6,14 @@ public class ProcessDetails {
private int burstTime; private int burstTime;
private int waitingTime; private int waitingTime;
private int turnAroundTime; private int turnAroundTime;
private int priority;
public ProcessDetails(final String processId, final int arrivalTime, final int burstTime, int priority) {
this.processId = processId;
this.arrivalTime = arrivalTime;
this.burstTime = burstTime;
this.priority = priority;
}
public ProcessDetails(final String processId, final int arrivalTime, final int burstTime) { public ProcessDetails(final String processId, final int arrivalTime, final int burstTime) {
this.processId = processId; this.processId = processId;
@ -33,6 +41,10 @@ public class ProcessDetails {
return turnAroundTime; return turnAroundTime;
} }
public int getPriority() {
return priority;
}
public void setProcessId(final String processId) { public void setProcessId(final String processId) {
this.processId = processId; this.processId = processId;
} }

View File

@ -1,5 +1,6 @@
package com.thealgorithms.scheduling; package com.thealgorithms.scheduling;
import com.thealgorithms.devutils.entities.ProcessDetails;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
@ -7,44 +8,35 @@ import java.util.PriorityQueue;
/** /**
* Preemptive Priority Scheduling Algorithm * Preemptive Priority Scheduling Algorithm
*
* @author [Bama Charan Chhandogi](https://www.github.com/BamaCharanChhandogi) * @author [Bama Charan Chhandogi](https://www.github.com/BamaCharanChhandogi)
*/ */
public class PreemptivePriorityScheduling {
protected final List<ProcessDetails> processes;
protected final List<String> ganttChart;
class Process { public PreemptivePriorityScheduling(List<ProcessDetails> processes) {
String name; this.processes = new ArrayList<>(processes);
int arrivalTime; this.ganttChart = new ArrayList<>();
int burstTime;
int priority;
Process(String name, int arrivalTime, int burstTime, int priority) {
this.name = name;
this.arrivalTime = arrivalTime;
this.burstTime = burstTime;
this.priority = priority;
} }
}
public final class PreemptivePriorityScheduling { public void scheduleProcesses() {
private PreemptivePriorityScheduling() { PriorityQueue<ProcessDetails> readyQueue = new PriorityQueue<>(Comparator.comparingInt(ProcessDetails::getPriority).reversed().thenComparingInt(ProcessDetails::getArrivalTime));
}
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; int currentTime = 0;
List<ProcessDetails> arrivedProcesses = new ArrayList<>();
while (!processes.isEmpty() || !readyQueue.isEmpty()) { while (!processes.isEmpty() || !readyQueue.isEmpty()) {
while (!processes.isEmpty() && processes.get(0).arrivalTime <= currentTime) { updateArrivedProcesses(currentTime, arrivedProcesses);
readyQueue.add(processes.remove(0)); readyQueue.addAll(arrivedProcesses);
} arrivedProcesses.clear();
if (!readyQueue.isEmpty()) { if (!readyQueue.isEmpty()) {
Process currentProcess = readyQueue.poll(); ProcessDetails currentProcess = readyQueue.poll();
ganttChart.add(currentProcess.getProcessId());
currentProcess.setBurstTime(currentProcess.getBurstTime() - 1);
ganttChart.add(currentProcess.name); if (currentProcess.getBurstTime() > 0) {
currentProcess.burstTime--;
if (currentProcess.burstTime > 0) {
readyQueue.add(currentProcess); readyQueue.add(currentProcess);
} }
} else { } else {
@ -53,7 +45,15 @@ public final class PreemptivePriorityScheduling {
currentTime++; currentTime++;
} }
}
return ganttChart; private void updateArrivedProcesses(int currentTime, List<ProcessDetails> arrivedProcesses) {
processes.removeIf(process -> {
if (process.getArrivalTime() <= currentTime) {
arrivedProcesses.add(process);
return true;
}
return false;
});
} }
} }

View File

@ -2,32 +2,29 @@ package com.thealgorithms.scheduling;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList; import com.thealgorithms.devutils.entities.ProcessDetails;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.junit.jupiter.api.Test; import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
/** /**
* Test Cases of Preemptive Priority Scheduling Algorithm * Test Cases of Preemptive Priority Scheduling Algorithm
*
* @author [Bama Charan Chhandogi](https://www.github.com/BamaCharanChhandogi) * @author [Bama Charan Chhandogi](https://www.github.com/BamaCharanChhandogi)
*/ */
class PreemptivePrioritySchedulingTest { class PreemptivePrioritySchedulingTest {
@ParameterizedTest
@MethodSource("provideProcessesAndExpectedSchedules")
void testPreemptivePriorityScheduling(List<ProcessDetails> processes, List<String> expectedSchedule) {
PreemptivePriorityScheduling scheduler = new PreemptivePriorityScheduling(processes);
scheduler.scheduleProcesses();
assertEquals(expectedSchedule, scheduler.ganttChart);
}
@Test static Stream<Arguments> provideProcessesAndExpectedSchedules() {
void testPreemptivePriorityScheduling() { 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")),
// Arrange 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")));
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);
} }
} }