Jump to content

Solution to Concurrent Modification of a variable. ArrayList & MouseListener

- - - - -

  • Please log in to reply
2 replies to this topic

#1
liamzebedee

liamzebedee

    Programmer

  • Members
  • PipPipPipPip
  • 129 posts
I have an ArrayList in my game to store all the particles ingame. I have an update mehtod which accesses the ArrayList to update the physics, I have a render method that accesses the ArrayList to render the particles onscreen and a MouseClick listener that when it detects a MouseClick it will add a new Particle to the ArrayList.

My problem is I keep getting an java.util.ConcurrentModificationException. This is because when I click at the same time it renders and both methods are trying to access the ArrayList. Is there a solution to accessing the ArrayList at the same time (different data type?).

#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Vectors are threadsafe.
So, Vector is probably a better data structure to use if you know multiple threads are going to be accessing/altering your data.

Edited by lethalwire, 30 April 2011 - 09:07 PM.


#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You can keep your arrayList but do this on it: Collections (Java 2 Platform SE v1.4.2))
Look for synchronizedLis(List )




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users