I have the following code:
while (rrProcesses.size() > 0) {
for (Process process : rrProcesses) {
if (process.getBurstTime() <= quantum) {
System.out.println("@t=" + clock + ", selected for " + process.getBurstTime() + " units");
clock += process.getBurstTime();
rrProcesses.remove(process);
} else {
System.out.println("@t=" + clock + ", selected for " + quantum + " units");
clock += quantum;
process.setBurstTime(process.burstTime - quantum);
}
if (processCounter != rrProcesses.size()) {
System.out.println("@t=" + clock + ", context switch " + contextSwitch + " occurs");
contextSwitch++;
clock += latency;
}
}
}
I am attempting to iterate over each object within my list, remove an item based on a conditional, and keep looping until all items are removed.
When I run this in intellij, I get the following err:
Exception in thread "main" java.util.ConcurrentModificationException
at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1013)
at java.base/java.util.ArrayList$Itr.next(ArrayList.java:967)
at com.company.Main.main(Main.java:224)
I searched the issue and found this solution. enter link description here
When I changed my code it became this:
Iterator<Process> iter = rrProcesses.iterator();
while (iter.hasNext()) {
for (Process process : rrProcesses) {
if (process.getBurstTime() <= quantum) {
System.out.println("@t=" + clock + ", selected for " + process.getBurstTime() + " units");
clock += process.getBurstTime();
iter.remove();
} else {
System.out.println("@t=" + clock + ", selected for " + quantum + " units");
clock += quantum;
process.setBurstTime(process.burstTime - quantum);
}
if (processCounter != rrProcesses.size()) {
System.out.println("@t=" + clock + ", context switch " + contextSwitch + " occurs");
contextSwitch++;
clock += latency;
}
}
}
However, now I am receiving the following error:
Exception in thread "main" java.lang.IllegalStateException
at java.base/java.util.ArrayList$Itr.remove(ArrayList.java:980)
at com.company.Main.main(Main.java:228)
I found this answer to the error but its honestly not clarifying anything for me. How is it possible for me to then remove the object from the List?