Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

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);
}
}

Transcribed Image Text:at SchedulingAlgorithms.lambda$main0ドル(SchedulingAlgorithms.java:28) at Exception in thread "Thread-2" java.util.ConcurrentModificationException at java.base/java.ut... java.base/java.lang.Thread.run(Thread.java:829)
Transcribed Image Text:Exception in thread "Thread-0" java.util.ConcurrentModificationException at java.base/java.util.ArrayList$Itr.checkForComodification (ArrayList.java:1043) at java.base/java.util.ArrayList$Itr.next(ArrayList.java:997) at SchedulingAlgorithms.executeFCFS (SchedulingAlgorithms.java:88)
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
    Recommended textbooks for you
    Text book image
    Database System Concepts
    Computer Science
    ISBN:9780078022159
    Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
    Publisher:McGraw-Hill Education
    Text book image
    Starting Out with Python (4th Edition)
    Computer Science
    ISBN:9780134444321
    Author:Tony Gaddis
    Publisher:PEARSON
    Text book image
    Digital Fundamentals (11th Edition)
    Computer Science
    ISBN:9780132737968
    Author:Thomas L. Floyd
    Publisher:PEARSON
    Text book image
    C How to Program (8th Edition)
    Computer Science
    ISBN:9780133976892
    Author:Paul J. Deitel, Harvey Deitel
    Publisher:PEARSON
    Text book image
    Database Systems: Design, Implementation, & Manag...
    Computer Science
    ISBN:9781337627900
    Author:Carlos Coronel, Steven Morris
    Publisher:Cengage Learning
    Text book image
    Programmable Logic Controllers
    Computer Science
    ISBN:9780073373843
    Author:Frank D. Petruzella
    Publisher:McGraw-Hill Education