title: Mix volatile and synchronized as a read-write lock
body: <p>Consider a primitive type variable with lots of threads reading and a few threads writing, will the following code work correctly?</p>
  
  <p>If it will, does it provide better performance than 1). declaring synchronized on all the methods; 2). using an explicit ReadWriteLock?</p>
  
  <p>Is this a common pattern? If not, what pattern is usually used in this situation?</p>
  
  <p>This currently works fine for me, but I feel it's a bit redundant to use both volatile and synchronized.</p>
  
  <pre><code>private volatile int value = 1;
  
  public void func1()
  {
      if (value == 1) {
          // do something
      }
  }
  
  public void func2()
  {
      if (value == 2) {
          // do something
      }
  }
  
  public void func3()
  {
      if (value == 3) {
          // do something
      }
  }
  
  public synchronized void increase()
  {
      if (value &lt; 10) value++;
  }
  
  public synchronized void decrease()
  {
      if (value &gt; 0) value--;
  }
  </code></pre>