/** * 模拟CAS */ public class SimulatedCAS { private int value; public synchronized int get(){ return value; } public synchronized int compareAndSwap(int expectedValue, int newValue){ int oldValue = value; if (oldValue == expectedValue){ value = newValue; } return oldValue; } public synchronized boolean compareAndSet(int expectedValue, int newValue){ return (expectedValue == compareAndSwap(expectedValue, newValue)); } }
/** * 基于CAS实现的非阻塞计数器 */ public class CasCounter { private SimulatedCAS value; public int getValue(){ return value.get(); } public int increment(){ int v; do { v = value.get(); } while (v != value.compareAndSwap(v, v+1)); return v + 1; } }
/** * 通过CAS来维持包含多个变量的不变性条件 */ public class CasNumberRange { private static class IntPair{ // 不变性条件: lower <= upper final int lower; final int upper; public IntPair(int lower, int upper) { this.lower = lower; this.upper = upper; } } private AtomicReference<IntPair> values = new AtomicReference<>(); public int getLower(){ return values.get().lower; } public int getUpper(){ return values.get().upper; } public void setLower(int i){ while (true){ IntPair oldv = values.get(); if (i > oldv.upper){ throw new IllegalArgumentException("lower can't > upper"); } IntPair newv = new IntPair(i, oldv.upper); if (values.compareAndSet(oldv, newv)){ return; } } } }
/** * 基于ReentrantLock实现的随机数生成器 */ public class ReentrantLockPreudoRandom extends PseudoRandom{ private final Lock lock = new ReentrantLock(false); private int seed; public ReentrantLockPreudoRandom(int seed){ this.seed = seed; } public int nextInt(int n){ lock.lock(); try{ int s = seed; seed = calculateNext(s); int remainder = s % n; return remainder > 0 ? remainder : remainder + n; } finally{ lock.unlock(); } } ... }
/** * 基于AtomicInteger实现的随机数生成器 */ public class AtomicPseudoRandom extends PseudoRandom{ private AtomicInteger seed; public AtomicPseudoRandom(int seed){ this.seed = new AtomicInteger(seed); } public int nextInt(int n){ while (true){ int s = seed.get(); int nextSeed = calculateNext(s); if (seed.compareAndSet(s, nextSeed)){ int remainder = s % n; return remainder > 0 ? remainder: remainder + n; } } } ... }
/** * 使用Treiber算法构造的非阻塞栈 */ public class ConcurrentStack<E> { private AtomicReference<Node<E>> top = new AtomicReference<ConcurrentStack.Node<E>>(); public void push(E item){ Node<E> newHead = new Node<E>(item); Node<E> oldHead; do{ oldHead = top.get(); newHead.next = oldHead; } while (!top.compareAndSet(oldHead, newHead)); } public E pop(){ Node<E> oldHead; Node<E> newHead; do { oldHead = top.get(); if (oldHead == null) return null; newHead = oldHead.next; } while (!top.compareAndSet(oldHead, newHead)); return oldHead.item; } private static class Node<E>{ public final E item; public Node<E> next; public Node(E item){ this.item = item; } } }
/** * 链表中非阻塞算法中的插入排序,来自Michael-Scott */ public class LinkedQueue<E> { private static class Node<E>{ final E item; final AtomicReference<Node<E>> next; public Node(E item, Node<E> next){ this.item = item; this.next = new AtomicReference<>(next); } } private final Node<E> dummy = new Node<E>(null, null); private final AtomicReference<Node<E>> head = new AtomicReference<>(dummy); private final AtomicReference<Node<E>> tail = new AtomicReference<>(dummy); public boolean put(E item){ Node<E> newNode = new Node<E>(item, null); while (true){ Node<E> curTail = tail.get(); Node<E> tailNext = curTail.next.get(); if (curTail == tail.get()){ //尾部还未修改 if (tailNext != null){ // 队列处于中间状态(即新节点已经接上,尾节点还未更新),推进尾节点 tail.compareAndSet(curTail, tailNext); } else{ // 处于稳定状态, 尝试插入新节点 if (curTail.next.compareAndSet(null, newNode)){ // 插入成功后,推进尾节点 tail.compareAndSet(curTail, tailNext); return true; } } } } } }
private static class Node<E>{ private final E item; private volatile Node<E> next; // volatile变量 public Node(E item){ this.item = item; } } // 基于CAS的更新器 private static AtomicReferenceFieldUpdater<Node, Node> nextUpdater = AtomicReferenceFieldUpdater.newUpdater(Node.class, Node.class, "next");
不吝指正。