mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 02:04:31 +08:00
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:
@ -6,6 +6,14 @@ public class ProcessDetails {
|
||||
private int burstTime;
|
||||
private int waitingTime;
|
||||
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) {
|
||||
this.processId = processId;
|
||||
@ -33,6 +41,10 @@ public class ProcessDetails {
|
||||
return turnAroundTime;
|
||||
}
|
||||
|
||||
public int getPriority() {
|
||||
return priority;
|
||||
}
|
||||
|
||||
public void setProcessId(final String processId) {
|
||||
this.processId = processId;
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.thealgorithms.scheduling;
|
||||
|
||||
import com.thealgorithms.devutils.entities.ProcessDetails;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
@ -7,44 +8,35 @@ import java.util.PriorityQueue;
|
||||
|
||||
/**
|
||||
* Preemptive Priority Scheduling Algorithm
|
||||
*
|
||||
* @author [Bama Charan Chhandogi](https://www.github.com/BamaCharanChhandogi)
|
||||
*/
|
||||
public class PreemptivePriorityScheduling {
|
||||
protected final List<ProcessDetails> processes;
|
||||
protected final List<String> ganttChart;
|
||||
|
||||
class Process {
|
||||
String name;
|
||||
int arrivalTime;
|
||||
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 PreemptivePriorityScheduling(List<ProcessDetails> processes) {
|
||||
this.processes = new ArrayList<>(processes);
|
||||
this.ganttChart = new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
public final class PreemptivePriorityScheduling {
|
||||
private PreemptivePriorityScheduling() {
|
||||
}
|
||||
public static List<String> preemptivePriorityScheduling(List<Process> processes) {
|
||||
List<String> ganttChart = new ArrayList<>();
|
||||
PriorityQueue<Process> readyQueue = new PriorityQueue<>(Comparator.comparingInt(p -> - p.priority));
|
||||
public void scheduleProcesses() {
|
||||
PriorityQueue<ProcessDetails> readyQueue = new PriorityQueue<>(Comparator.comparingInt(ProcessDetails::getPriority).reversed().thenComparingInt(ProcessDetails::getArrivalTime));
|
||||
|
||||
int currentTime = 0;
|
||||
List<ProcessDetails> arrivedProcesses = new ArrayList<>();
|
||||
|
||||
while (!processes.isEmpty() || !readyQueue.isEmpty()) {
|
||||
while (!processes.isEmpty() && processes.get(0).arrivalTime <= currentTime) {
|
||||
readyQueue.add(processes.remove(0));
|
||||
}
|
||||
updateArrivedProcesses(currentTime, arrivedProcesses);
|
||||
readyQueue.addAll(arrivedProcesses);
|
||||
arrivedProcesses.clear();
|
||||
|
||||
if (!readyQueue.isEmpty()) {
|
||||
Process currentProcess = readyQueue.poll();
|
||||
ProcessDetails currentProcess = readyQueue.poll();
|
||||
ganttChart.add(currentProcess.getProcessId());
|
||||
currentProcess.setBurstTime(currentProcess.getBurstTime() - 1);
|
||||
|
||||
ganttChart.add(currentProcess.name);
|
||||
currentProcess.burstTime--;
|
||||
|
||||
if (currentProcess.burstTime > 0) {
|
||||
if (currentProcess.getBurstTime() > 0) {
|
||||
readyQueue.add(currentProcess);
|
||||
}
|
||||
} else {
|
||||
@ -53,7 +45,15 @@ public final class PreemptivePriorityScheduling {
|
||||
|
||||
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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user