zoukankan      html  css  js  c++  java
  • Java高并发17-LongAccumulator类详解

    一、LongAccumulator类

    1.和LongAdder之间的关系

    • LongAdder类是LongAccumulator的一个特例,我们看一下LongAccumulator的一个构造方法
     public LongAccumlator(LongBinaryOperator accumulatorFunction,long identity) {
      this.function = accumulatorFunction;
      base = this.identity = identity;
     }
    • 其中参数identity是累加器的初始值;参数类型LongBinaryOperator是一种相当于二目运算符的类,它的输入是两个Long型数,返回也是一个Long型数字,我们看一下这个接口定义
    public interface LongBinaryOperator {
     long applyAsLong(long left,long right);
    }
    • 下面来看一下两个类的等价形式
    LongAdder adder = new LongAdder();

    LongAccumulator accumulator = new LongAccumulator(new LongBinaryOperator() {
     @Override
     public long applyAsLong(long left,long right) {
      return left + right;
     }
    }
    • LongAccumulator相比于LongAdder,可以为累加器提供非0的初始值,后者只能从0开始累加,并且前者可以自定义累加规则,我们只需要实现这个接口,然后在接口内部的方法内,自定累加规则即可。
    • 从下面的代码看一下LongAccumulator的accumulate方法和LongAdder类的add方法
    //LongAdder的add方法
    public void add(long x){
     Cell[] as;
     long b;
     long 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))){
       longAccumulator(x,null,uncontended);
      }
     }
    }

    //LongAccumulator的accumulate方法
    public void accumulate(long x){
      Cell[] as;
     long b;
     long v;
     int m;
     Cell a;
     if(as = cells) != null || 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))){
       longAccumulator(x,null,uncontended);
      }
     }
    • 调用casBase的时候后者传递的是b+x,前者使用了r=function.applyAsLong(b = base,x)来计算
    • 前者在调用longAccumulator时传递的是function,而后者是null,从下面的代码看出
     else if(casBase(v = base,((fn==null)?v+x:fn.applyAsLong(v,x)))){
      break;
    }
    • 当fu为null时,就是用了x+v的加法运算,这时候等价于LongAdder,当fn不为null则使用传递的fu函数计算

    2.总结:可以看到该类提供的功能更加一般化

    二、源码:

  • 相关阅读:
    Leetcode 109
    Leetcode 118
    js时间操作
    DWR搭建以及使用教程
    Ant 概念
    Eclipse 快捷键
    [eclipse] 三个操作技巧
    js call方法
    js验证密码强弱
    request getParameter getAttribute
  • 原文地址:https://www.cnblogs.com/ruigege0000/p/14249271.html
Copyright © 2011-2022 走看看