Note that there are some explanatory texts on larger screens.

plurals
  1. POSynchronizing a Map of Sets/Lists
    primarykey
    data
    text
    <p>I would like to implement a variation on the "Map of Sets" collection that will be constantly accessed by multiple threads. I am wondering whether the synchronization I am doing is sufficient to guarantee that no issues will manifest.</p> <p>So given the following code, where Map, HashMap, and Set are the Java implementations, and Key and Value are some arbitrary Objects:</p> <pre><code>public class MapOfSets { private Map&lt;Key, Set&lt;Value&gt;&gt; map; public MapOfLists() { map = Collections.synchronizedMap(new HashMap&lt;Key, Set&lt;Value&gt;()); } //adds value to the set mapped to key public void add(Key key, Value value) { Set&lt;Value&gt; old = map.get(key); //if no previous set exists on this key, create it and add value to it if(old == null) { old = new Set&lt;Value&gt;(); old.add(value); map.put(old); } //otherwise simply insert the value to the existing set else { old.add(value); } } //similar to add public void remove(Key key, Value value) {...} //perform some operation on all elements in the set mapped to key public void foo(Key key) { Set&lt;Value&gt; set = map.get(key); for(Value v : set) v.bar(); } } </code></pre> <p>The idea here is that because I've synchronized the Map itself, the get() and put() method should be atomic right? So there should be no need to do additional synchronization on the Map or the Sets contained in it. So will this work?</p> <p>Alternatively, would the above code be advantageous over another possible synchronization solution:</p> <pre><code>public class MapOfSets { private Map&lt;Key, Set&lt;Value&gt;&gt; map; public MapOfLists() { map = new HashMap&lt;Key, Set&lt;Value&gt;(); } public synchronized void add(Key key, Value value) { Set&lt;Value&gt; old = map.get(key); //if no previous set exists on this key, create it and add value to it if(old == null) { old = new Set&lt;Value&gt;(); old.add(value); map.put(old); } //otherwise simply insert the value to the existing set else { old.add(value); } } //similar to add public synchronized void remove(Key key, Value value) {...} //perform some operation on all elements in the set mapped to key public synchronized void foo(Key key) { Set&lt;Value&gt; set = map.get(key); for(Value v : set) v.bar(); } } </code></pre> <p>Where I leave the data structures unsynchronized but synchronize all the possible public methods instead. So which ones will work, and which one is better?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload