java - ConcurrentModificationException in HashMap<Object,DateTime> -


i have following code:

 iterator<ggitem> iter = ggitemtimestampmap.keyset().iterator();  ggitem gg;  while (iter.hasnext()) {         gg = iter.next();         if (datetime.now().isafter(ggitemtimestampmap.get(gg).plusseconds(10))) {              log.v("ggitem 10 second limit:", gg.tostring());              //if hasn't been seen 10 seconds after last timestamp. remove arraylist , remove ggitemtimestampmap hashmap.              ggitemtimestampmap.remove(gg); //todo probable problem causer.              removeggitemfromlist(gg);          }  } 

i concurrentmodificationexception error on iter.next(); call , uncertain why?

i realize cannot both access hashmap of object keys , timestamp values , modify @ same time, doesn't iterator counteract that?

first of can change hashmap concurrenthashmap

secondary may have try synchronize iter on class or of object.

synchronized(this) // if not in static context  {     // synchronized body }  synchronized(iter) {     // synchronized body } 

or use in method declaration

public synchronized void add(int value){      this.count += value; } 


, problem explained :

// @see http://stackoverflow.com/questions/602636/concurrentmodificationexception-and-a-hashmap iterator = map.entryset().iterator(); while (it.hasnext()) {    entry item = it.next();    map.remove(item.getkey()); } 

correct way :

// @see http://stackoverflow.com/questions/602636/concurrentmodificationexception-and-a-hashmap    iterator = map.entryset().iterator();    while (it.hasnext())    {       entry item = it.next();       it.remove(item);    } 

for future troubles need know concurrentmodificationexception means :

this exception may thrown methods have detected concurrent modification of object when such modification not permissible.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -