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.

  1. what wrong first code ?
  2. 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

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -