ERROR IN THIS CODE!! import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; class Task { String name; int priority; int burstTime; int arrivalTime; Task(String name, int priority, int burstTime) { this.name = name; this.priority = priority; this.burstTime = burstTime; } } public class SchedulingAlgorithms { private static List taskSet; public static void main(String[] args) { initializeTaskSet(); // FCFS Thread fcfsThread = new Thread(() -> { System.out.println("FCFS Schedule:"); executeFCFS(); }); // SJF Thread sjfThread = new Thread(() -> { System.out.println("\nSJF Schedule:"); executeSJF(); }); // Priority Scheduling Thread priorityThread = new Thread(() -> { System.out.println("\nPriority Schedule:"); executePriority(); }); // Round-Robin Thread rrThread = new Thread(() -> { System.out.println("\nRound-Robin Schedule:"); executeRoundRobin(); }); fcfsThread.start(); sjfThread.start(); priorityThread.start(); rrThread.start(); try { fcfsThread.join(); sjfThread.join(); priorityThread.join(); rrThread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } private static void initializeTaskSet() { taskSet = new ArrayList<>(); taskSet.add(new Task("T1", 2, 20)); taskSet.add(new Task("T2", 4, 25)); taskSet.add(new Task("T3", 3, 25)); taskSet.add(new Task("T4", 3, 15)); taskSet.add(new Task("T5", 1, 10)); // Assign random arrival times in the range [0, 100] Random random = new Random(); for (Task task : taskSet) { task.arrivalTime = random.nextInt(101); } // Sort tasks based on arrival time Collections.sort(taskSet, (t1, t2) -> Integer.compare(t1.arrivalTime, t2.arrivalTime)); } private static void executeFCFS() { int currentTime = 0; int totalWaitTime = 0; int totalTurnaroundTime = 0; for (Task task : taskSet) { int waitTime = currentTime - task.arrivalTime; totalWaitTime += waitTime; int turnaroundTime = waitTime + task.burstTime; totalTurnaroundTime += turnaroundTime; System.out.println(task.name + ": " + currentTime + " - " + (currentTime + task.burstTime)); currentTime += task.burstTime; } printAverages(totalWaitTime, totalTurnaroundTime); } private static void executeSJF() { // Sort tasks based on burst time Collections.sort(taskSet, (t1, t2) -> Integer.compare(t1.burstTime, t2.burstTime)); executeFCFS(); // SJF is essentially FCFS after sorting by burst time } private static void executePriority() { int currentTime = 0; int totalWaitTime = 0; int totalTurnaroundTime = 0; for (Task task : taskSet) { int waitTime = currentTime - task.arrivalTime; totalWaitTime += waitTime; int turnaroundTime = waitTime + task.burstTime; totalTurnaroundTime += turnaroundTime; System.out.println(task.name + ": " + currentTime + " - " + (currentTime + task.burstTime)); currentTime += task.burstTime; } printAverages(totalWaitTime, totalTurnaroundTime); } private static void executeRoundRobin() { int currentTime = 0; int totalWaitTime = 0; int totalTurnaroundTime = 0; int timeQuantum = 10; List remainingTasks = new ArrayList<>(taskSet); while (!remainingTasks.isEmpty()) { for (Task task : new ArrayList<>(remainingTasks)) { int remainingBurstTime = Math.min(task.burstTime, timeQuantum); int waitTime = currentTime - task.arrivalTime; totalWaitTime += waitTime; int turnaroundTime = waitTime + remainingBurstTime; totalTurnaroundTime += turnaroundTime; System.out.println(task.name + ": " + currentTime + " - " + (currentTime + remainingBurstTime)); currentTime += remainingBurstTime; task.burstTime -= remainingBurstTime; if (task.burstTime == 0) { remainingTasks.remove(task); } } } printAverages(totalWaitTime, totalTurnaroundTime); } private static void printAverages(int totalWaitTime, int totalTurnaroundTime) { double averageWaitTime = (double) totalWaitTime / taskSet.size(); double averageTurnaroundTime = (double) totalTurnaroundTime / taskSet.size(); System.out.println("\nAverage Waiting Time: " + averageWaitTime); System.out.println("Average Turnaround Time: " + averageTurnaroundTime); } }
ERROR IN THIS CODE!!
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
class Task {
String name;
int priority;
int burstTime;
int arrivalTime;
Task(String name, int priority, int burstTime) {
this.name = name;
this.priority = priority;
this.burstTime = burstTime;
}
}
public class SchedulingAlgorithms {
private static List<Task> taskSet;
public static void main(String[] args) {
initializeTaskSet();
// FCFS
Thread fcfsThread = new Thread(() -> {
System.out.println("FCFS Schedule:");
executeFCFS();
});
// SJF
Thread sjfThread = new Thread(() -> {
System.out.println("\nSJF Schedule:");
executeSJF();
});
// Priority Scheduling
Thread priorityThread = new Thread(() -> {
System.out.println("\nPriority Schedule:");
executePriority();
});
// Round-Robin
Thread rrThread = new Thread(() -> {
System.out.println("\nRound-Robin Schedule:");
executeRoundRobin();
});
fcfsThread.start();
sjfThread.start();
priorityThread.start();
rrThread.start();
try {
fcfsThread.join();
sjfThread.join();
priorityThread.join();
rrThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static void initializeTaskSet() {
taskSet = new ArrayList<>();
taskSet.add(new Task("T1", 2, 20));
taskSet.add(new Task("T2", 4, 25));
taskSet.add(new Task("T3", 3, 25));
taskSet.add(new Task("T4", 3, 15));
taskSet.add(new Task("T5", 1, 10));
// Assign random arrival times in the range [0, 100]
Random random = new Random();
for (Task task : taskSet) {
task.arrivalTime = random.nextInt(101);
}
// Sort tasks based on arrival time
Collections.sort(taskSet, (t1, t2) -> Integer.compare(t1.arrivalTime, t2.arrivalTime));
}
private static void executeFCFS() {
int currentTime = 0;
int totalWaitTime = 0;
int totalTurnaroundTime = 0;
for (Task task : taskSet) {
int waitTime = currentTime - task.arrivalTime;
totalWaitTime += waitTime;
int turnaroundTime = waitTime + task.burstTime;
totalTurnaroundTime += turnaroundTime;
System.out.println(task.name + ": " + currentTime + " - " + (currentTime + task.burstTime));
currentTime += task.burstTime;
}
printAverages(totalWaitTime, totalTurnaroundTime);
}
private static void executeSJF() {
// Sort tasks based on burst time
Collections.sort(taskSet, (t1, t2) -> Integer.compare(t1.burstTime, t2.burstTime));
executeFCFS(); // SJF is essentially FCFS after sorting by burst time
}
private static void executePriority() {
int currentTime = 0;
int totalWaitTime = 0;
int totalTurnaroundTime = 0;
for (Task task : taskSet) {
int waitTime = currentTime - task.arrivalTime;
totalWaitTime += waitTime;
int turnaroundTime = waitTime + task.burstTime;
totalTurnaroundTime += turnaroundTime;
System.out.println(task.name + ": " + currentTime + " - " + (currentTime + task.burstTime));
currentTime += task.burstTime;
}
printAverages(totalWaitTime, totalTurnaroundTime);
}
private static void executeRoundRobin() {
int currentTime = 0;
int totalWaitTime = 0;
int totalTurnaroundTime = 0;
int timeQuantum = 10;
List<Task> remainingTasks = new ArrayList<>(taskSet);
while (!remainingTasks.isEmpty()) {
for (Task task : new ArrayList<>(remainingTasks)) {
int remainingBurstTime = Math.min(task.burstTime, timeQuantum);
int waitTime = currentTime - task.arrivalTime;
totalWaitTime += waitTime;
int turnaroundTime = waitTime + remainingBurstTime;
totalTurnaroundTime += turnaroundTime;
System.out.println(task.name + ": " + currentTime + " - " + (currentTime + remainingBurstTime));
currentTime += remainingBurstTime;
task.burstTime -= remainingBurstTime;
if (task.burstTime == 0) {
remainingTasks.remove(task);
}
}
}
printAverages(totalWaitTime, totalTurnaroundTime);
}
private static void printAverages(int totalWaitTime, int totalTurnaroundTime) {
double averageWaitTime = (double) totalWaitTime / taskSet.size();
double averageTurnaroundTime = (double) totalTurnaroundTime / taskSet.size();
System.out.println("\nAverage Waiting Time: " + averageWaitTime);
System.out.println("Average Turnaround Time: " + averageTurnaroundTime);
}
}



Step by step
Solved in 4 steps with 1 images









