[android-developers] Re: ConcurrentModificationException from Handler

2011-01-17 Thread AC
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
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] Re: ConcurrentModificationException from Handler

2011-01-17 Thread Kumar Bibek
This exception generally occurs, if you are iterating through it and
changing it simultaneously.

*Note that this implementation is not synchronized.* If multiple threads
access this map concurrently, and at least one of the threads modifies the
map structurally, it *must* be synchronized externally. (A structural
modification is any operation that adds or deletes one or more mappings;
merely changing the value associated with a key that an instance already
contains is not a structural modification.) This is typically accomplished
by synchronizing on some object that naturally encapsulates the map. If no
such object exists, the map should be wrapped using the
Collections.synchronizedMap method. This is best done at creation time, to
prevent accidental unsynchronized access to the map:

 Map m = Collections.synchronizedMap(new HashMap(...));


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


Kumar Bibek
http://techdroid.kbeanie.com
http://www.kbeanie.com



On Mon, Jan 17, 2011 at 2:09 PM, AC alistair.cunning...@gmail.com wrote:

 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.comandroid-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

Re: [android-developers] Re: ConcurrentModificationException from Handler

2011-01-17 Thread Kostya Vasilyev
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.comandroid-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

[android-developers] Re: ConcurrentModificationException from Handler

2011-01-17 Thread AC
I'm afraid that didn't help. I now have:

public class MapViewActivity extends MapActivity {
private static MapString, Object markers =
Collections.synchronizedMap( new HashMapString, Object() );

and the problem still occurs.

-- 
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


Re: [android-developers] Re: ConcurrentModificationException from Handler

2011-01-17 Thread Kumar Bibek
There might be other reasons to the problem. Kostya has mentioned all the
details regarding the exception. :)

Kumar Bibek
http://techdroid.kbeanie.com
http://www.kbeanie.com



On Mon, Jan 17, 2011 at 2:32 PM, AC alistair.cunning...@gmail.com wrote:

 I'm afraid that didn't help. I now have:

 public class MapViewActivity extends MapActivity {
private static MapString, Object markers =
 Collections.synchronizedMap( new HashMapString, Object() );

 and the problem still occurs.

 --
 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.comandroid-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

Re: [android-developers] Re: ConcurrentModificationException from Handler

2011-01-17 Thread Kostya Vasilyev
Well, I don't know if that covered *all* the possibilities, but that's a
place to start :)

2011/1/17 Kumar Bibek coomar@gmail.com

 There might be other reasons to the problem. Kostya has mentioned all the
 details regarding the exception. :)


 Kumar Bibek
 http://techdroid.kbeanie.com
 http://www.kbeanie.com



 On Mon, Jan 17, 2011 at 2:32 PM, AC alistair.cunning...@gmail.com wrote:

 I'm afraid that didn't help. I now have:

 public class MapViewActivity extends MapActivity {
private static MapString, Object markers =
 Collections.synchronizedMap( new HashMapString, Object() );

 and the problem still occurs.

 --
 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.comandroid-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.comandroid-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

[android-developers] Re: ConcurrentModificationException from Handler

2011-01-17 Thread AC


On Jan 17, 5:00 pm, Kostya Vasilyev kmans...@gmail.com wrote:
 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.

That was the problem! Thank you very much!

-- 
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


[android-developers] Re: ConcurrentModificationException from Handler

2011-01-17 Thread AC
After changing to iterator.remove() rather than removing from the Map,
the problem went away. Thank you very much!

-- 
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


Re: [android-developers] Re: ConcurrentModificationException from Handler

2011-01-17 Thread Kostya Vasilyev
Cool.

I would also recommend that you change your code to send new data in the
message, like I described above.

If your netwoking code runs on a separate thread - which it should be - then
that global is like a dog waiting to bite you when you least expect.

-- Kostya

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



 On Jan 17, 5:00 pm, Kostya Vasilyev kmans...@gmail.com wrote:
  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.

 That was the problem! Thank you very much!

 --
 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.comandroid-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