title: Lock Free Array Element Swapping
body: <p>In multi-thread environment, in order to have thread safe array element swapping, we will perform synchronized locking.</p>
  
  <pre><code>// a is char array.
  synchronized(a) {
      char tmp = a[1];
      a[1] = a[0];
      a[0] = tmp;
  }
  </code></pre>
  
  <p>Is it possible that we can make use of the following API in the above situation, so that we can have a lock free array element swapping? If yes, how?</p>
  
  <p><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.html#compareAndSet%28T,%20V,%20V%29" rel="nofollow noreferrer">http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.html#compareAndSet%28T,%20V,%20V%29</a></p>