ConcurrentModificationException
Hello all,
I am getting a ConcurrentModificationException when trying to run my code, even though I am using a ListIterator when I am iterating and removing. I wrote some sample code to show you.
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
publicclass Mainextends JPanel
{
private Engine engine;
public Main()
{
setBackground(Color.black);
engine =new Engine();
Thread t =new Thread(new Runnable()
{
publicvoid run()
{
while(true)
{
Random randNumGen =new Random();
if(randNumGen.nextInt(5) == 1)
{
engine.createDots(randNumGen.nextInt(20)+1);
}
repaint();
try
{
Thread.sleep(20);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
});
t.start();
}
publicvoid paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.white);
g.drawString(engine.dots.size() +" dots", 15, 15);
engine.draw(g);
}
publicstaticvoid main(String[] args)
{
JFrame f =new JFrame("Test");
f.setSize(500, 500);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new Main());
f.setVisible(true);
}
}
/*=================================================*/
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Random;
publicclass Engine
{
protected ArrayList<Dot> dots;
private ListIterator<Dot> iterator;
public Engine()
{
dots =new ArrayList<Dot>();
}
publicvoid createDots(int num)
{
for(int i=0; i<num; i++)
{
Random randNumGen =new Random();
int x = randNumGen.nextInt(500);
int y = randNumGen.nextInt(500);
Dot dot =new Dot(this, x, y);
dots.add(dot);
}
}
publicvoid actOnDots()
{
iterator = dots.listIterator();
while(iterator.hasNext())
{
Dot dot = iterator.next();
dot.act();
}
}
publicvoid removeDot()
{
if(iterator ==null)
{
System.err.println("Cannot remoce Dot.");
}
else
{
iterator.remove();
}
}
publicvoid draw(Graphics g)
{
for(Dot dot : dots)
{
dot.draw(g);
}
}
}
/*============================================*/
import java.awt.Graphics;
import java.util.Random;
publicclass Dot
{
private Engine engine;
privateint xPos;
privateint yPos;
public Dot(Engine e,int x,int y)
{
engine = e;
xPos = x;
yPos = y;
}
publicvoid act()
{
Random randNumGen =new Random();
if(randNumGen.nextInt(20) == 1)
{
engine.removeDot();
}
}
publicvoid draw(Graphics g)
{
g.fillOval(xPos, yPos, 4, 4);
}
}
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at concurrencydemo.Engine.draw(Engine.java:54)
at concurrencydemo.Main.paintComponent(Main.java:54)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintWithOffscreenBuffer(Unknown Source)
at javax.swing.JComponent.paintDoubleBuffered(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
I occasionally get another error that completely freezes my program. Does anyone know why this happens? Thank you for your help.>

