zoukankan      html  css  js  c++  java
  • 2020-10-22:谈谈java中的LongAdder和LongAccumulator的相同点和不同点。

    福哥答案2020-10-22:

    简单回答:
    相同点:都是多个单元操作。
    不同点:LongAdder相加,LongAccumulator自定义计算规则。

    中级回答:
    相同点:
    LongAddr与LongAccumulator类都是使用非阻塞算法CAS实现的,这相比于使用锁实现原子性操作在性能上有很大的提高。
    LongAddr类是LongAccumulator类的一个特例,只是LongAccumulator提供了更强大的功能,可以让用户自定义累加规则。
    不同点:
    1.LongAccumulator相比于LongAddr不同之处在于调用casBase时,LongAccumulator使用 r = function.applyAsLong(b = base, x)来计算,LongAddr使用casBase(b = base, b + x)来计算。
    2.通过LongAccumulator和LongAddr的longAccumulate()方法可知:当fn为null时就使用v+x加法运算,这时候就等价于LongAddr,当fn不为null时,则使用传递的fn函数计算。
    3.LongAccumulator类相比于LongAddr功能更加强大,如上代码accumulatorFunction是一个双目运算器接口,其根据输入的两个参数返回一个计算值,identity则是LongAccumulator累加器的初始值。
    4.LongAccumulator相比于LongAdder,可以为累加器提供非0的初始值,而LongAdder只能提供默认的0值。
    5.另外,LongAccumulator还可以指定累加规则,比如累加或者相乘,只需要在构造LongAccumulator时,传入自定义的双目运算器即可,后者则内置累加规则。
    ***
    [评论](https://user.qzone.qq.com/3182319461/blog/1603321218)

  • 相关阅读:
    oss blob上传
    app中画一条细线
    antd和原生交互,原生掉前端的方法
    -webkit-touch-callout 图片下载
    ifram嵌入网址 有跨域问题
    ...state
    数组对象的复制
    在vue中使用tinymce组件
    autofs自动挂载服务
    podmen
  • 原文地址:https://www.cnblogs.com/waitmoon/p/13860943.html
Copyright © 2011-2022 走看看