title: Is a critical section necessary to set a boolean value?
body: <p>Suppose we have n threads accessing this function, I heard that even though booleans are just a bit flip, the process is not atomic. In this function, does <code>opening = true</code> need to wrapped inside a synchronize? <code>opening</code> is a member of the class. </p>
  
  <pre><code>boolean opening = false;
  
  public void open() {
  
      synchronized (this) {
          while (numCarsOnBridge != 0 || opening || closing) {
              // Wait if cars on bridge, opening or closing
              try {
                  // Wait until all cars have cleared the bridge, until bridge opening
                  wait();
              } catch (InterruptedException e) {}
          }
          // By now, no cars will be under the bridge.
      }
  
      if (!opening) {
          opening = true; // Do we need to wrap this in a synchronize?
          // notifyAll(); // Maybe need to notify everyone that it is opening
          try {
              sleep(60); // pauses the current thread calling this
          } catch (InterruptedException e) {}
  
          synchronized (this) {
              drawBridgeClosed = false; // drawBridge is opened
              opening = false;
              notifyAll(); // Only notify all when state has fully changed
          }
      }
  
  }
  </code></pre>