LockFree算法
LockFree算法,不需要加锁
通常都是三个部分组成:
1、循环
2、CAS(CompareAndSet) CAS机制:http://blog.csdn.net/lifesider/article/details/6582338
3、回退
public class Counter2 { private AtomicInteger max = new AtomicInteger(); public void set(int value) { for (;;) { int current = max.get(); if (value > current) { if (max.compareAndSet(current, value)) { break; } else { continue; } } else { break; } } } public int getMax() { return max.get(); } }