title: Synchronizing nested Maps and Sets (Java)
body: <p>I have a nested set within a map within a map that I am trying to synchronize over two threads.</p>
  
  <p>The map is instantiated as such:    </p>
  
  <pre><code>private final Map&lt;Manageable, Map&lt;String, Set&lt;Manageable&gt;&gt;&gt; manageableMap = 
          Collections.synchronizedMap(new HashMap&lt;Manageable, Map&lt;String, Set&lt;Manageable&gt;&gt;&gt;());
  </code></pre>
  
  <p>This is the function I use to add values to the map:</p>
  
  <pre><code>private void put(Manageable key, Manageable value, String valueType) {
      synchronized (manageableMap) {
          Map&lt;String, Set&lt;Manageable&gt;&gt; setMap = manageableMap.get(key);
          if (setMap == null) {
              setMap = new HashMap&lt;String, Set&lt;Manageable&gt;&gt;();
              manageableMap.put(key, Collections.synchronizedMap(setMap));
          }
  
          synchronized (setMap) {
              Set&lt;Manageable&gt; set = setMap.get(valueType);
              if (set == null) {
                  set = new HashSet&lt;Manageable&gt;();
                  setMap.put(valueType, Collections.synchronizedSet(set));
              }
  
              synchronized (set) {
                  set.add(value);
              }
          }
      }
  }
  </code></pre>
  
  <p>Intellij IDEA warns me that I am synchronizing on local variables setMap and set.</p>
  
  <p>I am fairly new to synchronization and I was wondering if this is the proper way to 
  synchronize a nested data structure like this.</p>
  
  <p>Thank you for you help.</p>