title: How can I make sure a method is only called once by multiple threads?
body: <p>I have the following structure:  </p>
  
  <pre><code>public void someMethod(){  
     //DO SOME STUFF
     try{  
      doSomeProcessing();  
     }  
     catch (Exception e){  
          loadSomeHeavyData();  
          doSomeProcessing();      
     }    
  }  
  </code></pre>
  
  <p>The method <code>someMethod</code> <em>may</em> be called concurrently by many threads. The <code>doSomeProcessing</code> <em>may</em> throw an exception (it is using some data in the backend that could become obsolete).<br>
  If an exception is thrown then <code>loadSomeHeavyData();</code> does some timeconsuming task that let's say "updates" all the current data and I am able to call <code>doSomeProcessing();</code>.<br>
  <strong>Problem:</strong> How can I make sure that <code>loadSomeHeavyData();</code> is called only <strong>once</strong>? If I put some atomic flag in the entry of <code>loadSomeHeavyData();</code> then I can not be sure when this should be cleared.<br>
  How can I solve this? Just a note: I can not modify <code>doSomeProcessing();</code> as it is an external API and I am using decorator pattern to use it.</p>