title: Is it possible to synchronize a half of an if-block?
body: <p>The question may seem pretty obvious, but it's not clear to me anyway.</p>
  
  <p>I have the following class:</p>
  
  <pre><code>public class MyClass{
  
      private Object lock = new Object();
      private boolean flag;
  
  
      public void method(){
          //Start synchronization block
          if(!flag){
              flag = true;
          //end synchronization block
              //do some bulk operation, should be synchronized
          }
          //some other staff
      }
  }
  </code></pre>
  
  <p>The thing is I cannot put the piece of code I need to be synchronized, because it will not be correct. How to do such synchronization? Maybe there's something from <code>java.util.concurrent</code> that I could make use of.</p>