title: Using ConcurentHashMap and AtomicInteger as instance variables within a spring service class
body: <p>I am attempting to implement thread-safe usage of ConcurentHashMap within singleton spring service that is injected into controllers:</p>
  
  <pre><code>@Service
  public MyService{
  
    final ConcurrentMap&lt;String, AtomicInteger&gt; myMap = new ConcurrentHashMap&lt;String,   AtomicInteger&gt;(10) {
          {/* initialize the ten key/values */
          }
      };
  
   public int add(String key) {
     return myMap.get(key).incrementAndGet();
   }
  
      // accessed via ajax loop (and controller), if value changes update display
    public int getCount(String key) {
      return myMap.get(key).get();
    }
  }
  </code></pre>
  
  <p>Is there a better way to make access to a hashmap thread-safe?  How could I adapt this to work in a clustered environment? It is a follw up to <a href="https://stackoverflow.com/q/11125497/106261">my other question.</a></p>