First look at the definition of Node in ConcurrentQueue
private static class Node<E> { private volatile E item; private volatile Node<E> next; Node(E item) { // Piggyback on imminent casNext() lazySetItem(item); } E getItem() { return item; } boolean casItem(E cmp, E val) { return UNSAFE.compareAndSwapObject(this, itemOffset, cmp, val); } void setItem(E val) { item = val; } void lazySetItem(E val) { UNSAFE.putOrderedObject(this, itemOffset, val); } void lazySetNext(Node<E> val) { UNSAFE.putOrderedObject(this, nextOffset, val); } Node<E> getNext() { return next; } boolean casNext(Node<E> cmp, Node<E> val) { return UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val); } // Unsafe mechanics private static final sun.misc.Unsafe UNSAFE = sun.misc.Unsafe.getUnsafe(); private static final long nextOffset = objectFieldOffset(UNSAFE, "next", Node.class); private static final long itemOffset = objectFieldOffset(UNSAFE, "item", Node.class); }
We see that one of the casItem methods calls UNSAFE.compareAndSwapObject(this, itemOffset, cmp, val);
1. What is CAS operation?
CAS: Compare and Swap, translated into Compare and Swap.
The java.util.concurrent package implements an optimistic lock that is different from the synchronous lock with the help of CAS.