Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Oct 9th, 2012  |  syntax: None  |  size: 0.82 KB  |  views: 53  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.      * Atomically increments by one the current value.
  3.      *
  4.      * @return the updated value
  5.      */
  6.     public final long incrementAndGet() {
  7.         for (;;) {
  8.             long current = get();
  9.             long next = current + 1;
  10.             if (compareAndSet(current, next))
  11.                 return next;
  12.         }
  13.     }
  14.  
  15. /**
  16.      * Atomically sets the value to the given updated value
  17.      * if the current value {@code ==} the expected value.
  18.      *
  19.      * @param expect the expected value
  20.      * @param update the new value
  21.      * @return true if successful. False return indicates that
  22.      * the actual value was not equal to the expected value.
  23.      */
  24.     public final boolean compareAndSet(long expect, long update) {
  25.         return unsafe.compareAndSwapLong(this, valueOffset, expect, update);
  26.     }