First, a link to docs:

http://download.oracle.com/javase/1.4.2/docs/api/java/util/ConcurrentModificationException.html

I can see several possible causes for this, and you should get a more
detailed stack trace (more than "inside syncMarkers"):

1 - You have two threads concurrently accessing the same hash map, and while
the UI thread is iterating, the worker thread comes around and starts
modifying the hash map.

To fix this, you can use synchronized collections:

http://download.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html#synchronizedCollection(java.util.Collection)

Although I would use a different fix - make the data collection be part of
the message. The worker thread would build a new hash map each time, put it
into a message object, and send the message to the UI thread. From that
point on, the collection would be owned by the UI thread.

No concurrent access, no issues with synchronization.

2 - As described in the link above, code that is trying to remove elements
from a collection while iterating, for example:

for (Item i : myHashMap.values()) {
   if (some condition) {
      myHashMap.remove(i.key);
   }
}

You can't do this - use iterator.remove() to remove elements while
iterating.

3 - Code within a single thread that uses iterator.remove on a collection in
a loop, breaking out of the loop early after it finds (and removes) the
object it was going to remove.

I don't know if this is documented anywhere, but I've found that with hash
maps, you have to let that iteration loop run all the way through, until the
iterator's hasNext returns false. The changes (element removals) are applied
at that point, and the collection is then marked as not being in the middle
of a modification.

If you break out of the loop early, after calling iterator.remove, the
change is never applied, and the collection stays marked as being modified.
The next attempt to access the collection will throw the exception as you're
getting.

-- Kostya

2011/1/17 AC <alistair.cunning...@gmail.com>

> Perhaps I'm sending the message from the service to the activity
> incorrectly, and syncMarkers() ends up getting called from the
> service's thread? If so, would anyone have any advice on the correct
> way to send the message?
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com<android-developers%2bunsubscr...@googlegroups.com>
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to