Java concurrency support using java.util.concurrent semantics -
Java concurrency support using java.util.concurrent semantics -
i'm trying back upwards modification (deactivate() function call) of next info construction in thread safe manner -
private static map<string, set<integer>> dbpartitionstatus = new hashmap<string, set<dbpartitionid>>(); public void deactivate(dbpartitionid partition) throws exception { synchronized (dbpartitionstatus) { set<dbpartitionid> partitions = dbpartitionstatus.get(servicename); if (partitions == null) { partitions = new hashset<dbpartitionid>(); } partitions.add(partition); dbpartitionstatus.put(servicename, partitions); } }
if replace synchronization concurrenthashmap & concurrentskiplistset duo, there race condition.
i wondering if there cleaner way of achieving synchronization here (using java.util.concurrent)
should no race conditions next implementation:
private final static concurrentmap <string, set <dbpartitionid>> dbpartitionstatus = new concurrenthashmap <string, set <dbpartitionid>> (); public void deactivate (dbpartitionid partition) { set <dbpartitionid> partitions = dbpartitionstatus.get (servicename); if (partitions == null) { partitions = new concurrentskiplistset <dbpartitionid> (); set <dbpartitionid> p = dbpartitionstatus.putifabsent (servicename, partitions); if (p != null) partitions = p; } partitions.add (partition); }
java concurrency
Comments
Post a Comment