java - HashMap and ArrayList adding while iterating/looping -


i have game every x seconds write changed values in memory db. these values stored in containers(hashmaps , arraylists) when data hold edited.

for simplicity lets pretend have 1 container write db:

public static hashmap<string, string> dbentitiesdeletesbacklog = new hashmap<string, string>(); 

my db writing loop:

timer dbupdatejob = new timer(); dbupdatejob.schedule(new timertask() {     public void run() {         long starttime = system.nanotime();         boolean updateentitiestablesuccess = updateentitiestable();         if (!updateentitiestablesuccess){             try {                 conn.rollback();             } catch (sqlexception e) {                 e.printstacktrace();                 logger.fatal(e.getmessage());                 system.exit(1);             }         } else { //everything saved db - commit time             try {                 conn.commit();             } catch (sqlexception e) {                 e.printstacktrace();                 logger.fatal(e.getmessage());                 system.exit(1);             }         }         logger.debug("time save db: " + (system.nanotime() - starttime) / 1000000 + " milliseconds");     } }, 0, 10000); //todo:: figure out perfect saving delay 

my update method:

private boolean updateentitiestable() {     iterator<entry<string, string>> = dbentitiesdeletesbacklog.entryset().iterator();     while (it.hasnext()) {         entry<string, string> pairs = it.next();         string tmpentityid = pairs.getkey();          int deletedsuccess = update("delete" +                  " " + db_name + ".entities" +                  " entity_id=(?)", new string[]{tmpentityid});         if (deletedsuccess != 1) {             logger.error("entity " + tmpentityid + " unable deleted.");             return false;         }         it.remove();         dbentitiesdeletesbacklog.remove(tmpentityid);     } 

do need create sort of locking mechanism while 'saving db' dbentitiesdeletesbacklog hashmap , other containers not included in excerpt? think need to, because creates iterator, loops. if added after iterator created, , before done looping through entries. i'm sorry more of process question , less of code question(since included sample code), wanted make sure easy understand trying , asking.

same question other containers use so:

public static arraylist<string> dbcharacterdeletesbacklog =  new arraylist<string>();  private boolean deletecharactersfromdb() {     (string deletewho : dbcharacterdeletesbacklog){         int deletesuccess = mydbsyncher.update("delete " + db_name + ".characters" +                 " name=(?)",                  new string[]{deletewho});          if (deletesuccess != 1) {             logger.error("character(deletesuccess): " + deletesuccess);             return false;         }     }     dbcharacterdeletesbacklog.clear();     return true; } 

thanks much, always, on this. appreciated!!

at least, need synchronized map (via collections.synchronizedmap) if accessing map concurrently, otherwise may experience non deterministic behaviour.

further that, suggest, need lock map during iteration. javadoc collections.synchronizedmap() suggestion is:

it imperative user manually synchronize on returned map when iterating on of collection views:

map m = collections.synchronizedmap(new hashmap());       ... set s = m.keyset();  // needn't in synchronized block       ... synchronized(m) {  // synchronizing on m, not s!     iterator = s.iterator(); // must in synchronized block     while (i.hasnext())         foo(i.next()); } 

failure follow advice may result in non-deterministic behavior.

alternatively, use concurrenthashmap instead of regular hashmap avoid requiring synchronization during iteration. game, better option since avoid locking collection long period of time.

possibly better, consider rotating through new collections such every time update database grab collection , replace new empty 1 new updates written to, avoiding locking collection while database writes occurring. collections in case managed container allow grab , replace thread safe. <<< note: cannot expose underlying collection in case modifying code since need keep reference strictly private swap effective (and not introduce race conditions).


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -