Can't get synchronized to work

Hey..I'm having trouble getting synchronized to work with my ArrayList, and consequently, a ConcurrentModificationException is thrown.

The list is passed from a parent object and modified in this method.

Here's the method:

publicsynchronizedvoid removeGame(){

System.out.println(username +" removeGame(): in");

for(String get: gameList){// this where the exception is thrown

if(get.contains(username) && get.contains(opponent)){

if(get.equals(username +"," + opponent) || get.equals(opponent +"," + username)){

gameList.remove(get);

game.remove(username);

game.remove(opponent);

}// end if

}// end if

}// end for loop

System.out.println(username +" removeGame(): finished");

}// end remove game method

Thanks for any help

[1509 byte] By [gra4497a] at [2007-10-2 20:08:31]
# 1

That's because you are modifying the collection gameList at the same time as iterating over it. Whilst a ConcurrentModificationException is normally symptomatic of a threading issue, a single thread can still cause one to be thrown - as you have demonstrated.

You are iterating over the gameList (in the for-loop that you have commented) and you are concurrently modifying the gameList when you do gameList.remove().

The only way you can structurally modify a collection whilst iterating over it is to modify it via the iterator. Iterator has a remove() method which will remove the currently observed entry from the underlying collection. This is exactly what you need to use. Unfortunately, the iterator is being hidden from you because you are using the new Java 1.5 for-loop syntax.

So... convert the for-loop that you have commented back to an old style iteration, and the do the removal through the iterator.

dannyyatesa at 2007-7-13 22:48:56 > top of Java-index,Core,Core APIs...
# 2

That 1.5 style for loop simply creates an Iterator on that list. Iterators

don't want any elements removed or added to the list behind their back.

You iterate over that list (by using that 1.5 style for loop) *and* you remove

an element from the list in the body of that for loop. That makes the

Iterator whine and throw an exception at you.

kind regards,

Jos

JosAHa at 2007-7-13 22:48:56 > top of Java-index,Core,Core APIs...
# 3

The problem isn't that there is more than one thread within the method. The problem is the hidden Iterator within your for construct. You can't delete out from under an Iterator the way you're doing it. You must use the Iterator.remove() method to do so.

See the [url=http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html]for each documentation[/url] for more information.

Edit - must remember to not go grab another Dr. Pepper in the middle of posting...

stdunbara at 2007-7-13 22:48:56 > top of Java-index,Core,Core APIs...
# 4

Yes, I understand all this. But what if I need to modify the ArrayList later? I apologize for not giving you guys enough ground info on this. This arraylist keeps is used to track who is in and out of a game on a server application. My understanding of iterators is that once you make them, you can't add anything else to the arraylist. Yes/no?

gra4497a at 2007-7-13 22:48:56 > top of Java-index,Core,Core APIs...
# 5
> Edit - must remember to not go grab another> Dr. Pepper in the middle of posting...That should teach you! ;-)kind regards,Jos (< not the slowest old sod for once)
JosAHa at 2007-7-13 22:48:56 > top of Java-index,Core,Core APIs...
# 6

> Yes, I understand all this. But what if I need to modify the ArrayList

> later? I apologize for not giving you guys enough ground info on this.

> This arraylist keeps is used to track who is in and out of a game on a

> server application. My understanding of iterators is that once you

> make them, you can't add anything else to the arraylist. Yes/no?

Simply don't use that pooped up 1.5 style for loop then; simply do this:for (Iterator i= list.iterator(); i.hasNext(); ) {

YourObject obj= (YourObject)i.next();

if (someConditionHoldsOn(obj))

i.remove();

}

kind regards,

Jos

JosAHa at 2007-7-13 22:48:56 > top of Java-index,Core,Core APIs...
# 7
awesome, thanks alot.
gra4497a at 2007-7-13 22:48:56 > top of Java-index,Core,Core APIs...