title: compareAndSet vs incrementAndGet
body: <p>Doing a course on concurrent programming.</p>
  
  <p>As an example we have</p>
  
  <pre><code>final class Counter {
  
          private AtomicInteger value;
  
          public long getValue() {
              return value.get();
          }
  
          public long increment() {
              int v;
              do {
                  v = value.get();
              }
              while(!value.compareAndSet(v, v+1));
              return v+1;
          }
  }
  </code></pre>
  
  <p>Why would you use compareAndSet in this case and not incrementAndGet ?</p>
  
  <p>Thanks</p>