Java CAS算法优化实现乐观锁及原子性自增demo(LongAdder)

JDK1.8 对CAS机制进行了优化LongAdder。

使用分段CAS以及自动分段迁移的方式来大幅度提升多线程高并发执行CAS操作的性能。

  1. 首先有一个基值base,多线程累加数值都是对base进行累加的。
  2. 如果竞争激烈到一定程度无法对base进行累加操作时,会实施分段CAS机制,搞一个Cell数据,每个数组是一个数值分段,对cells数组中某个元素进行更新。

核心结构

// 单元格表。非空时,大小是2的幂
transient volatile Cell[] cells;
// 基值,主要在没有争用时使用,但在表初始化争用期间也用作回退。通过CAS更新。
transient volatile long base;
// 调整和/或创建单元格时使用的spinlock(通过cas锁定)
transient volatile int cellsBusy;

核心代码

   public void increment() {
        add(1L);
    }
    
    public void add(long x) {
        Cell[] as; long b, v; int m; Cell a;
        if ((as = cells) != null || !casBase(b = base, b + x)) {
            boolean uncontended = true;
            if (as == null || (m = as.length - 1) < 0 ||
                (a = as[getProbe() & m]) == null ||
                !(uncontended = a.cas(v = a.value, v + x)))
                longAccumulate(x, null, uncontended);
        }
    }
    // 并发较低时仅对base变量进行CAS操作
    final boolean casBase(long cmp, long val) {
        return UNSAFE.compareAndSwapLong(this, BASE, cmp, val);
    }
    // 高并发时对单一变量的CAS操作分散为对数组cells中多个元素的CAS操作,取值时进行求和
    final void longAccumulate(long x, LongBinaryOperator fn,
                              boolean wasUncontended) {
        int h;
        if ((h = getProbe()) == 0) {
            ThreadLocalRandom.current(); // force initialization
            h = getProbe();  // 返回当前线程的threadLocalRandomProbe值
            wasUncontended = true;
        }
        boolean collide = false;                // True if last slot nonempty
        for (;;) {
            Cell[] as; Cell a; int n; long v;
            if ((as = cells) != null && (n = as.length) > 0) {
                if ((a = as[(n - 1) & h]) == null) {
                    // 如果cells对应位置没有数据则插入
                    if (cellsBusy == 0) {       // Try to attach new Cell
                        Cell r = new Cell(x);   // Optimistically create
                        if (cellsBusy == 0 && casCellsBusy()) {
                            boolean created = false;
                            try {               // Recheck under lock
                                Cell[] rs; int m, j;
                                if ((rs = cells) != null &&
                                    (m = rs.length) > 0 &&
                                    rs[j = (m - 1) & h] == null) {
                                    rs[j] = r;
                                    created = true;
                                }
                            } finally {
                                cellsBusy = 0;
                            }
                            if (created)
                                break;
                            continue;           // Slot is now non-empty
                        }
                    }
                    collide = false;
                }
                else if (!wasUncontended)       // CAS already known to fail
                    wasUncontended = true;      // Continue after rehash
                else if (a.cas(v = a.value, ((fn == null) ? v + x :
                                             fn.applyAsLong(v, x))))
                    break;
                else if (n >= NCPU || cells != as)
                    collide = false;            // At max size or stale
                else if (!collide)
                    collide = true;
                else if (cellsBusy == 0 && casCellsBusy()) { // 扩容,2倍
                    try {
                        if (cells == as) {      // Expand table unless stale
                            Cell[] rs = new Cell[n << 1];
                            for (int i = 0; i < n; ++i)
                                rs[i] = as[i];
                            cells = rs;
                        }
                    } finally {
                        cellsBusy = 0;
                    }
                    collide = false;
                    continue;                   // Retry with expanded table
                }
                h = advanceProbe(h);
            }
            else if (cellsBusy == 0 && cells == as && casCellsBusy()) {
                boolean init = false;
                try {                           // Initialize table
                    if (cells == as) {
                        Cell[] rs = new Cell[2];
                        rs[h & 1] = new Cell(x);
                        cells = rs;
                        init = true;
                    }
                } finally {
                    cellsBusy = 0;
                }
                if (init)
                    break;
            }
            else if (casBase(v = base, ((fn == null) ? v + x :
                                        fn.applyAsLong(v, x))))
                break;                          // Fall back on using base
        }
    }

原子性自增demo

github地址

public class AtomicityLongAdder {

	private LongAdder count = new LongAdder();
	private void increase() {
		count.increment();
	}
	
	public static void main(String[] args) {
		Long time = System.currentTimeMillis();
		final AtomicityLongAdder atomicityLongAdder = new AtomicityLongAdder();
		for (int i = 0; i < 100; i++) {
			new Thread(new Runnable() {
				public void run() {
					for (int j = 0; j < 100000; j++) {
						atomicityLongAdder.increase();
					}
				}
			}).start();
		}
		while(Thread.activeCount() > 1) {
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("运行时间:" + (System.currentTimeMillis() - time));
		System.out.println("LongAdder(乐观锁):" + atomicityLongAdder.count);
	}
	
}

猜你喜欢

转载自blog.csdn.net/qq_27243963/article/details/94714491