0

I'm currently working on a game that is multi-threaded, there is one thread running the updates for the game and one thread repainting the panel that the game is being played on. After editing some code that was responsible for an enemy firing projectiles at the player i noticed that i was getting a ConcurrentModificationException from the Iterator that was iterating through all the entities and updating them. I isolated the problem to this statement here:

getMyBounds2D().getCenterY();

Because when I change the the statement to:

getMyBounds2D().getY();

the Exception is no longer thrown from the updating Iterator. the method getMyBounds2D() just returns a Rectangle2D that is representative of the bounding box of the entity, is there anyone that could explain why changing the statement above causes a CME and how to fix this issue?

Neil Locketz
  • 4,228
  • 1
  • 23
  • 34
  • 1
    are you messing with the awt/swing objects from outside the EDT? – jtahlborn Feb 04 '13 at 15:47
  • EventDispatchThread the thread that calls all listeners and does the painting of the gui – ratchet freak Feb 04 '13 at 15:55
  • The iterator remembers the modification count of its collection from the start of the iteration. On each next, it will check if there was another modification and if so, fail with CME. Find the source of that change. – Ralf H Feb 04 '13 at 15:58

1 Answers1

1

You cannot mess with awt/swing objects outside of the EDT (event dispatch thread). the various gui objects are meant to be handled in a single threaded manner, by this thread alone. if you have multiple threads handling these objects, then that will cause all kinds of problems, CME among others.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118