[Java]重學(xué)Java-LongAccumulator 類

LongAccumulator

LongAdder類是LongAccumulator的一個(gè)特例,它提供給用戶一個(gè)自定義規(guī)則的可能——accumulatorFunction.

public LongAccumulator(LongBinaryOperator accumulatorFunction,
                       long identity) {
    this.function = accumulatorFunction;
    base = this.identity = identity;
}
  • java.util.function.LongBinaryOperator
@FunctionalInterface
public interface LongBinaryOperator {

    /**
     * Applies this operator to the given operands.
     *
     * @param left the first operand
     * @param right the second operand
     * @return the operator result
     */
    long applyAsLong(long left, long right);
}

LongBinaryOperator是一個(gè)lambda表達(dá)式接口,(a,b)->c,輸入left和right返回一個(gè)long值.

LongAccumulator可以實(shí)現(xiàn)初始化自定義,LongAdder默認(rèn)是0,同時(shí),通過LongBinaryOperator,我們可以實(shí)現(xiàn)一些計(jì)算過程.

LongAccumulator示例

package com.tea.modules.java8.thread.atomic;

import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.LongBinaryOperator;

/**
 * com.tea.modules.java8.thread.atomic
 * LongAccumulator學(xué)習(xí)
 *
 * @author jaymin
 * @since 2022/11/7
 */
public class LongAccumulatorDemo {
    public static void main(String[] args) {
        // left代表base的值,也就是1;
        // right代表的是傳進(jìn)來的值,也就下面accumulate函數(shù)傳的值
        LongAccumulator longAccumulator = new LongAccumulator((left, right) -> left * right, 1);
        longAccumulator.accumulate(2);
        System.out.println(longAccumulator.get());

        LongAdder longAdder = new LongAdder();
        longAdder.add(1);
        System.out.println(longAdder.longValue());

        LongAccumulator longAdd = new LongAccumulator(Long::sum, 0);
        longAdd.accumulate(1);
        System.out.println(longAdd.get());
    }
}

這里展示了2個(gè)示例,一個(gè)是聲明一個(gè)累乘計(jì)算器,一個(gè)是用LongAccumulator實(shí)現(xiàn)了一個(gè)LongAdder.

源碼對比

  • java.util.concurrent.atomic.LongAccumulator#accumulate
public void accumulate(long x) {
    Cell[] as; long b, v, r; int m; Cell a;
    if ((as = cells) != null ||
        // casBase采用的是lambda表達(dá)式返回的值
        (r = function.applyAsLong(b = base, x)) != b && !casBase(b, r)) {
        boolean uncontended = true;
        if (as == null || (m = as.length - 1) < 0 ||
            (a = as[getProbe() & m]) == null ||
            !(uncontended =
              (r = function.applyAsLong(v = a.value, x)) == v ||
              a.cas(v, r)))
            longAccumulate(x, function, uncontended);
    }
}
  • java.util.concurrent.atomic.LongAdder#add
public void add(long x) {
    Cell[] as; long b, v; int m; Cell a;
    // casBase采用的是 b+x
    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);
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容