java - HashMap throwing ConcurrentModificationException -
if i'm getting following error
map<string, object> fooddata = new hashmap<string, object>(); fooddata.put("penguin", 1); fooddata.put("flamingo", 2); map<string,object> synchronizedfooddata = collections.synchronizedmap(fooddata); for(string key: synchronizedfooddata.keyset()) synchronizedfooddata.remove(key);
question :- why code throws concurrentmodificationexception
@ runtime, whereas same code concurrenthashmap
did not :-
map<string, object> fooddata = new concurrenthashmap<string, object>(); fooddata.put("penguin", 1); fooddata.put("flamingo", 2); map<string,object> synchronizedfooddata = collections.synchronizedmap(fooddata); for(string key: synchronizedfooddata.keyset()) synchronizedfooddata.remove(key);
why not showing error. im expecting internal working clear example :-
means i'm still using collections.synchronizedmap(fooddata); still getting error should not case.
- what wrong first code ?
- then both hashmap , collections.synchronizedmap throwing error when use synchronizedmap ?
well showing internal workings not going easy - in case of chm
- it's far not trivial implementation. if want - can @ sources , ask specific questions.
so, expecting synchronized map
act chm
without throwing exceptions on removal while traversing? not case, synchronized has nothing , documentation specially not it. guaranteed via collections.synchronizedmap
each method protected shared mutex, single thread can access single method: nothing more, nothing less.
the internal details of chm
different - , documentation says - similarly, iterators, spliterators , enumerations return elements reflecting state of hash table @ point @ or since creation of iterator/enumeration. **not** throw java.util.concurrentmodificationexception concurrentmodificationexception
Comments
Post a Comment