Java8新特性系列(原子性操作)

史培培發表於2018-02-08

題圖:by pixel2013 From pixabay

上期我們介紹了Java8中新的時間日期API,本期我們介紹Java8中原子性操作LongAdder

原子操作

根據百度百科的定義:

"原子操作(atomic operation)是不需要synchronized",這是Java多執行緒程式設計的老生常談了。所謂原子操作是指不會被執行緒排程機制打斷的操作;這種操作一旦開始,就一直執行到結束,中間不會有任何 context switch (切換到另一個執行緒)。

AtomicLong

在單執行緒的環境中,使用Long,如果對於多執行緒的環境,如果使用Long的話,需要加上synchronized關鍵字,從Java5開始,JDK提供了AtomicLong類,AtomicLong是一個提供原子操作的Long類,通過執行緒安全的方式操作加減,AtomicLong提供原子操作來進行Long的使用,因此十分適合高併發情況下的使用。

public class AtomicLongFeature {
	private static final int NUM_INC = 1_000_000;

	private static AtomicLong atomicLong = new AtomicLong(0);

	private static void update() {
		atomicLong.set(0);
		ExecutorService executorService = Executors.newFixedThreadPool(5);
		IntStream.range(0, NUM_INC).forEach(i -> {
			Runnable task = () -> atomicLong.updateAndGet(n -> n + 2);
			executorService.submit(task);
		});
		stop(executorService);
		System.out.println(atomicLong.get());
	}

	private static void stop(ExecutorService executorService) {
		try {
			executorService.shutdown();
			executorService.awaitTermination(60, TimeUnit.SECONDS);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			if (!executorService.isTerminated()) {
				System.out.println("kill tasks");
			}
			executorService.shutdownNow();
		}
	}

	public static void main(String[] args) {
		update();
	}
}
複製程式碼

輸出: 2000000

為什麼AtomicInteger能支援高併發呢?看下AtomicLongupdateAndGet方法:

public final int updateAndGet(IntUnaryOperator updateFunction) {
    int prev, next;
    do {
        prev = get();
        next = updateFunction.applyAsInt(prev);
    } while (!compareAndSet(prev, next));
    return next;
}

public final boolean compareAndSet(int expect, int update) {
    return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
}
複製程式碼

原因是每次updateAndGet時都會呼叫compareAndSet方法。

AtomicLong是在使用非阻塞演算法實現併發控制,在一些高併發程式中非常適合,但並不能每一種場景都適合,不同場景要使用使用不同的數值類。

LongAdder

AtomicLong的原理是依靠底層的cas來保障原子性的更新資料,在要新增或者減少的時候,會使用死迴圈不斷地cas到特定的值,從而達到更新資料的目的。那麼LongAdder又是使用到了什麼原理?難道有比cas更加快速的方式?

public class LongAdderFeature {
	private static final int NUM_INC = 1_000_000;

	private static LongAdder longAdder = new LongAdder();

	private static void update() {
		ExecutorService executorService = Executors.newFixedThreadPool(5);
		IntStream.range(0, NUM_INC).forEach(i -> {
			Runnable task = () -> longAdder.add(2);
			executorService.submit(task);
		});
		stop(executorService);
		System.out.println(longAdder.sum());
	}

	private static void stop(ExecutorService executorService) {
		try {
			executorService.shutdown();
			executorService.awaitTermination(60, TimeUnit.SECONDS);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			if (!executorService.isTerminated()) {
				System.out.println("kill tasks");
			}
			executorService.shutdownNow();
		}
	}

	public static void main(String[] args) {
		update();
	}
}
複製程式碼

輸出: 2000000

我們來看下LongAdder的add方法:

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);
    }
}
複製程式碼

我們可以看到一個Cell的類,那這個類是用來幹什麼的呢?

@sun.misc.Contended static final class Cell {
    volatile long value;
    Cell(long x) { value = x; }
    final boolean cas(long cmp, long val) {
        return UNSAFE.compareAndSwapLong(this, valueOffset, cmp, val);
    }

    // Unsafe mechanics
    private static final sun.misc.Unsafe UNSAFE;
    private static final long valueOffset;
    static {
        try {
            UNSAFE = sun.misc.Unsafe.getUnsafe();
            Class<?> ak = Cell.class;
            valueOffset = UNSAFE.objectFieldOffset
                (ak.getDeclaredField("value"));
        } catch (Exception e) {
            throw new Error(e);
        }
    }
}
複製程式碼

我們可以看到Cell類的內部是一個volatile的變數,然後更改這個變數唯一的方式通過cas。我們可以猜測到LongAdder的高明之處可能在於將之前單個節點的併發分散到各個節點的,這樣從而提高在高併發時候的效率。

LongAdder在AtomicLong的基礎上將單點的更新壓力分散到各個節點,在低併發的時候通過對base的直接更新可以很好的保障和AtomicLong的效能基本保持一致,而在高併發的時候通過分散提高了效能。

public long sum() {
    Cell[] as = cells; Cell a;
    long sum = base;
    if (as != null) {
        for (int i = 0; i < as.length; ++i) {
            if ((a = as[i]) != null)
                sum += a.value;
        }
    }
    return sum;
}
複製程式碼

當計數的時候,將base和各個cell元素裡面的值進行疊加,從而得到計算總數的目的。這裡的問題是在計數的同時如果修改cell元素,有可能導致計數的結果不準確,所以缺點是LongAdder在統計的時候如果有併發更新,可能導致統計的資料有誤差。

Java8新特性系列(原子性操作)

微信公眾號:碼上論劍
請關注我的個人技術微信公眾號,訂閱更多內容

相關文章