title: Building an object for a cache, "asynchronously" (kind of), in Java: howto?
body: <p>Say you have this kind of code:</p>
  
  <pre><code>public final class SomeClass
  {
      private final Map&lt;SomeKey, SomeValue&gt; map = new HashMap&lt;SomeKey, SomeValue&gt;();
  
      // ...
  
      public SomeValue getFromCache(final SomeKey key)
      {
          SomeKey ret;
          synchronized(map) {
              ret = map.get(key);
              if (ret == null) {
                  ret = buildValue(key);
                  map.put(key, ret);
              }
          }
          return ret;
      }
  
   //etc
   }
  </code></pre>
  
  <p>The problem is performance: if <code>buildValue()</code> is an expensive function, then one caller having to build its value will block all other callers, whose value may already exist. I'd like to find a mechanism in which a caller having to build a value will <em>not</em> block other callers.</p>
  
  <p>I cannot believe this problem hasn't been tackled (<em>and</em> solved) already. I tried to google around for a solution but couldn't find one so far. Do you have a link to do that?</p>
  
  <p>I was thinking about using a <code>ReentrantReadWriteLock</code>, but couldn't come with anything yet.</p>