title: Non-atomic use of check/put error during static analysis
body: <p>I have a code from which I am trying to get the instance of my class as I have written a wrapper around <code>java.util.logging.Logger</code>. </p>
  
  <p>Below is the snippet of code in my <code>ClientLogger</code> class -</p>
  
  <pre><code>private static final Map&lt;Class&lt;?&gt;, ClientLogger&gt; s_classLoggers = new ConcurrentHashMap&lt;Class&lt;?&gt;, ClientLogger&gt;();
  
  final private Logger m_logger;
  
  private ClientLogger(final Class&lt;?&gt; caller) {
      m_logger = Logger.getInstance(caller);
  }   
  
  public static ClientLogger getInstance(final Class&lt;?&gt; klass) {
      final ClientLogger result;
  
      if (s_classLoggers.containsKey(klass)) {
          result = s_classLoggers.get(klass);
      } else {
          result = new ClientLogger(klass);
          s_classLoggers.put(klass, result);
      }
  
      return result;
  }
  </code></pre>
  
  <p>And this is the way I am initializing it in my other classes where I need to use my above logger -</p>
  
  <pre><code>private static final ClientLogger s_logger = ClientLogger.getInstance(TestLogger.class);
  </code></pre>
  
  <p>Now when I am running my static analysis tool, it is complaining as -</p>
  
  <pre><code>Non-atomic use of check/put on this line s_classLoggers.put(klass, result);
  </code></pre>
  
  <p>in my <code>ClientLogger</code> class and I am not sure why? Is there anything wrong I am doing here?</p>
  
  <p><strong>UPDATE:-</strong></p>
  
  <p>Here is my updated code - </p>
  
  <pre><code>private static final ConcurrentHashMap&lt;Class&lt;?&gt;, ClientLogger&gt; s_classLoggers = new ConcurrentHashMap&lt;Class&lt;?&gt;, ClientLogger&gt;();
  
  public static ClientLogger getInstance(final Class&lt;?&gt; klass) {
      final ClientLogger result;
  
      result = new ClientLogger(klass);
      s_classLoggers.putIfAbsent(klass, result);
  
      return result;
  }
  </code></pre>
  
  <p><strong>Another Update:-</strong></p>
  
  <pre><code>private static final ConcurrentHashMap&lt;Class&lt;?&gt;, ClientLogger&gt; s_classLoggers = new ConcurrentHashMap&lt;Class&lt;?&gt;, ClientLogger&gt;();
  
  public static ClientLogger getInstance(final Class&lt;?&gt; klass) {
      ClientLogger result;
  
      result = s_classLoggers.putIfAbsent(klass, new ClientLogger(klass));
      if (result == null) {
          result = new ClientLogger(klass);
      }       
  
      return result;
  }
  </code></pre>