siva
unread,Sep 28, 2010, 3:53:13 AM9/28/10Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to guava-discuss
Just like ConcurrentHashMap is safe during iterator of keys for
HashMap. Is there any implementation for MultiMap?
We are using Multimap for keeping track of listeners on item. While
sending some event, if new subscriber is added for same item, for
which we are iterating and sending events,
ConcurrentModificationException is thrown. Is there any way to avoid
this.
static Multimap<String,String> getMap() {
Multimap<String, String> map = HashMultimap.create();
map.put("1", "1");
map.put("1", "2");
map.put("1", "3");
return map;
}
static void throwsException2() {
Multimap<String, String> map = getMap();
for (String string : map.get("1")) {
System.out.println("Get: "+string);
if (string.equals("2")) {
map.put("1", "4"); --> adding new element to same key
System.out.println("Added 3");
}
}
System.out.println(map.get("1"));
}
Exception:
Get: 3
Get: 2
Added 3
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)
at java.util.HashMap$KeyIterator.next(HashMap.java:828)
at com.google.common.collect.AbstractMultimap$WrappedCollection
$WrappedIterator.next(AbstractMultimap.java:516)
at
guava.MapConcurrentAccessTest.throwsException2(MapConcurrentAccessTest.java:
37)
at guava.MapConcurrentAccessTest.main(MapConcurrentAccessTest.java:
51)