This discussion is archived
2 Replies Latest reply: Nov 25, 2009 8:01 AM by 796262 RSS

Concurrent Modification Exception

843853 Newbie
Currently Being Moderated
Hey, I get "Exception in thread "Thread-5" java.util.ConcurrentModificationException" While trying to run my Applet. I assume this means that I have two threads trying to modify one object. However, I don't see why there should be multiple threads (especially 5) running at once.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Avalanche extends JApplet implements KeyListener, Runnable
{
    Player player;
    boolean playing;
    Thread th;
    Image dbImage;
    Graphics dbg;
    final int WIDTH = 400;
    final int HEIGHT = 400;
    ArrayList<Icicle> waitingIcicles = new ArrayList<Icicle>();
    ArrayList<Icicle> movingIcicles = new ArrayList<Icicle>();
    Random r = new Random();
    boolean firstCall = true;

    public void init()
    {
        //set size and bg color
        setBackground(Color.white);
        this.setSize(WIDTH, HEIGHT);

        //Initialize buffer
        dbImage = createImage (WIDTH, HEIGHT);
        dbg = dbImage.getGraphics ();

        //Init icicle list with a random number of icicles
        int numStartIcicles = r.nextInt(10)+1;
        for(int i=0; i < numStartIcicles; i++)
        {
            waitingIcicles.add(new Icicle());
        }

        //Init player graphic
        player = new Player(200, 350, 20);

        //Init key listener
        addKeyListener(this);
        playing = false;
    }

   public void start()
    {
        th = new Thread(this);
        th.start();
    }
    public void stop()
    {
        th.stop();
    }
   

    public void run ()
     {
          Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
          while (playing)
          {
                        for (Icicle icicle: waitingIcicles)
                        {
                            icicle.countdown();
                            if(icicle.willFall)
                            {
                                    waitingIcicles.remove(icicle);
                                    movingIcicles.add(icicle);
                            }
                        }
                        for (Icicle icicle: movingIcicles)
                        {
                            icicle.moveDown();
                            if(icicle.hitBottom)
                                movingIcicles.remove(icicle);
                        }

               repaint();

               Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
                        try
               {
                    //15 ms Delay
                    Thread.sleep(20);
               }
               catch(InterruptedException ex)
               {
                    //Or not.
               }
          }
     }
    public void keyPressed(KeyEvent e) {
        int k = e.getKeyCode();
        switch (k)
        {
            case 10: playing = true; start(); break;
            case 37: player.moveLeft(); break;
            case 38: player.moveDown(); break;
            case 39: player.moveRight(); break;
            case 40: player.moveUp(); break;
            default: break;
        }
    }
    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {}

    public void paint(Graphics g)
    {
        update(g);

     //   if(firstCall)
      //  {
            
        //    firstCall = false;
       // }
    }

    public void update(Graphics g)
    {
      //Clearness
      g.drawImage(dbImage, 0, 0, this);

      player.paintPlayer(g);
      for(Icicle icicle: movingIcicles)
         icicle.paintIcicle(g);
      for(Icicle icicle: waitingIcicles)
                icicle.paintIcicle(g);
    }

}
  • 1. Re: Concurrent Modification Exception
    800419 Newbie
    Currently Being Moderated
    Is there a line # included in your error message?
    Tanis.7x wrote:
    Hey, I get "Exception in thread "Thread-5" java.util.ConcurrentModificationException" While trying to run my Applet. I assume this means that I have > two threads trying to modify one object.
    Well that could throw a CME or you could be trying to modify a Collection using both an Iterator and it's built in methods...
  • 2. Re: Concurrent Modification Exception
    796262 Newbie
    Currently Being Moderated
    Tanis.7x wrote:
    Hey, I get "Exception in thread "Thread-5" java.util.ConcurrentModificationException" While trying to run my Applet. I assume this means that I have two threads trying to modify one object.
    That's not necessarily what that Exception means. It's happening because you can't add or remove items from a list while you are iterating over it, which is what you do here:
    for (Icicle icicle: waitingIcicles)
                            {
                                icicle.countdown();
                                if(icicle.willFall)
                                {
                                        waitingIcicles.remove(icicle);
                                        movingIcicles.add(icicle);
                                }
                            }
                            for (Icicle icicle: movingIcicles)
                            {
                                icicle.moveDown();
                                if(icicle.hitBottom)
                                    movingIcicles.remove(icicle);
                            }
    The for-each syntax is just syntactic sugar for using an iterator. The easiest solution here is to use an iterator directly instead of a for-each loop, and then use the iterator's add( ) and remove( ) methods.

    Even after you fix that, you might have some issues with iterating over your lists in paint while adding and removing items from them in your thread. You'll have to check that out then.
    However, I don't see why there should be multiple threads (especially 5) running at once.
    I can promise that you have at least 3 threads running, but probably many more than that. You definitely want to look into swing threading, but basically: the program enters on one thread (the main thread), does Swing work (including your key listener paint method) on the Event Dispatching Thread, and then you create another thread.

    By the way, you're starting a new thread every time the user presses the key with keycode 10. So that might be why your thread was named Thread-5 (or it could be just because java felt like naming it that)