I’ve searched StackOverflow and there are many ConcurrentModificationException questions. After reading them, I’m still confused. I’m getting a lot of these exceptions. I’m using a “Registry” setup to keep track of Objects:
public class Registry {
public static ArrayList<Messages> messages = new ArrayList<Messages>();
public static ArrayList<Effect> effects = new ArrayList<Effect>();
public static ArrayList<Projectile> proj = new ArrayList<Projectile>();
/** Clears all arrays */
public static void recycle(){
messages.clear();
effects.clear();
proj.clear();
}
}
I’m adding and removing objects to these lists by accessing the ArrayLists like this: Registry.effects.add(obj) and Registry.effects.remove(obj)
I managed to get around some errors by using a retry loop:
//somewhere in my game..
boolean retry = true;
while (retry){
try {
removeEffectsWithSource("CHARGE");
retry = false;
}
catch (ConcurrentModificationException c){}
}
private void removeEffectsWithSource(String src) throws ConcurrentModificationException {
ListIterator<Effect> it = Registry.effects.listIterator();
while ( it.hasNext() ){
Effect f = it.next();
if ( f.Source.equals(src) ) {
f.unapplyEffects();
Registry.effects.remove(f);
}
}
}
But in other cases this is not practical. I keep getting ConcurrentModificationExceptions in my drawProjectiles() method, even though it doesn’t modify anything. I suppose the culprit is if I touched the screen, which creates a new Projectile object and adds it to Registry.proj while the draw method is still iterating.
I can’t very well do a retry loop with the draw method, or it will re-draw some of the objects. So now I’m forced to find a new solution.. Is there a more stable way of accomplishing what I’m doing?
Oh and part 2 of my question: Many people suggest using ListIterators (as I have been using), but I don’t understand.. if I call ListIterator.remove() does it remove that object from the ArrayList it’s iterating through, or just remove it from the Iterator itself?
Top line, three recommendations:
static?)synchronizedor aReadWriteLockunder the covers)Part 1 of your question
You should use a concurrent data structure for the multi-threaded scenario, or use a synchronizer and make a defensive copy. Probably directly exposing the collections as
publicfields is wrong: your registry should expose thread-safe behavioral accessors to those collections. For instance, maybe you want aRegistry.safeRemoveEffectBySource(String src)method. Keep the threading specifics internal to the registry, which seems to be the “owner” of this aggregate information in your design.Since you probably don’t really need
Listsemantics, I suggest replacing these withConcurrentHashMapswrapped intoSetusingCollections.newSetFromMap().Your
draw()method could either a) use aRegistry.getEffectsSnapshot()method that returns a snapshot of the set; or b) use anIterable<Effect> Registry.getEffects()method that returns a safe iterable version (maybe just backed by theConcurrentHashMap, which won’t throwCMEunder any circumstances). I think (b) is preferable here, as long as the draw loop doesn’t need to modify the collection. This provides a very weak synchronization guarantee between the mutator thread(s) and thedraw()thread, but assuming thedraw()thread runs often enough, missing an update or something probably isn’t a big deal.Part 2 of your question
As another answer notes, in the single-thread case, you should just make sure you use the
Iterator.remove()to remove the item, but again, you should wrap this logic inside theRegistryclass if at all possible. In some cases, you’ll need to lock a collection, iterate over it collecting some aggregate information, and make structural modifications after the iteration completes. You ask if theremove()method just removes it from theIteratoror from the backing collection… see the API contract forIterator.remove()which tells you it removes the object from the underlying collection. Also see this SO question.