I’ve been stuck on this one for a while, so any help is greatly appreciated.
I have the following code section:
BallThread.java
@Override
public synchronized void run() {
while (numItersCompleted < maxNumIters) {
completedThisIter = false;
synchronized (ballList) {
for (Ball b : ballList) { // line 35
b.updatePosition(ballPanel.getBounds(), ballList);
}
}
} // other stuff
Exception in thread "Thread-2" java.util.ConcurrentModificationException
at java.util.ArrayList$SubList.checkForComodification(ArrayList.java:1091)
at java.util.ArrayList$SubList.listIterator(ArrayList.java:972)
at java.util.AbstractList.listIterator(AbstractList.java:300)
at java.util.ArrayList$SubList.iterator(ArrayList.java:968)
at Part2.BallThread.run(BallThread.java:35)
And sometimes I get this(line 36 in BallPanel)
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:782)
at java.util.ArrayList$Itr.next(ArrayList.java:754)
at Part2.BallPanel.paintComponent(BallPanel.java:36)
at javax.swing.JComponent.paint(JComponent.java:1029)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5138)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:302)
at javax.swing.RepaintManager.paint(RepaintManager.java:1188)
at javax.swing.JComponent._paintImmediately(JComponent.java:5086)
at javax.swing.JComponent.paintImmediately(JComponent.java:4896)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:783)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:735)
Without seeing the rest of your code, one fix would be to declare
ballListas the following:All of the operations of a
synchronizedListattempt to synchronize on itself before every method, which would make your iterations completely safe as long as you keep your currentsynchronized(ballList)s in place.