Advertisement
Guest User

Untitled

a guest
Oct 9th, 2012
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement