本文主要内容

  1. 4 种方式实现计数器功能,对比其性能
  2. 介绍 LongAdder
  3. 介绍 LongAccumulator

    来个需求

    一个 jvm 中实现一个计数器功能,需保证多线程情况下数据正确性。
    我们来模拟 50 个线程,每个线程对计数器递增 100 万次,最终结果应该是 5000 万。
    我们使用 4 种方式实现,看一下其性能,然后引出为什么需要使用LongAdder、LongAccumulator。

    方式一:synchronized 方式实现

    package com.itsoku.chat32;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.LongAccumulator;

/
微信公众号:程序员路人,个人博客:http://www.itsoku.com
/
public class Demo1 {
static int** count = 0;

  1. **public** **static** **synchronized** **void** **incr**() {<br /> count++;<br /> }
  2. **public** **static** **void** **main**(String[] args) **throws** ExecutionException, InterruptedException {<br /> **for** (**int** i = 0; i < 10; i++) {<br /> count = 0;<br /> m1();<br /> }<br /> }
  3. **private** **static** **void** **m1**() **throws** InterruptedException {<br /> **long** t1 = System.currentTimeMillis();<br /> **int** threadCount = 50;<br /> CountDownLatch countDownLatch = **new** CountDownLatch(threadCount);<br /> **for** (**int** i = 0; i < threadCount; i++) {<br /> **new** Thread(() -> {<br /> **try** {<br /> **for** (**int** j = 0; j < 1000000; j++) {<br /> incr();<br /> }<br /> } **finally** {<br /> countDownLatch.countDown();<br /> }<br /> }).start();<br /> }<br /> countDownLatch.await();<br /> **long** t2 = System.currentTimeMillis();<br /> System.out.println(String.format("结果:%s,耗时(ms):%s", count, (t2 - t1)));<br /> }<br />}

输出:
结果:50000000,耗时(ms):1437
结果:50000000,耗时(ms):1913
结果:50000000,耗时(ms):386
结果:50000000,耗时(ms):383
结果:50000000,耗时(ms):381
结果:50000000,耗时(ms):382
结果:50000000,耗时(ms):379
结果:50000000,耗时(ms):379
结果:50000000,耗时(ms):392
结果:50000000,耗时(ms):384

平均耗时:390 毫秒

方式 2:AtomicLong 实现

package com.itsoku.chat32;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicLong;

/
微信公众号:程序员路人,个人博客:http://www.itsoku.com
/
public class Demo2 {
static AtomicLong count = new** AtomicLong(0);

  1. **public** **static** **void** **incr**() {<br /> count.incrementAndGet();<br /> }
  2. **public** **static** **void** **main**(String[] args) **throws** ExecutionException, InterruptedException {<br /> **for** (**int** i = 0; i < 10; i++) {<br /> count.set(0);<br /> m1();<br /> }<br /> }
  3. **private** **static** **void** **m1**() **throws** InterruptedException {<br /> **long** t1 = System.currentTimeMillis();<br /> **int** threadCount = 50;<br /> CountDownLatch countDownLatch = **new** CountDownLatch(threadCount);<br /> **for** (**int** i = 0; i < threadCount; i++) {<br /> **new** Thread(() -> {<br /> **try** {<br /> **for** (**int** j = 0; j < 1000000; j++) {<br /> incr();<br /> }<br /> } **finally** {<br /> countDownLatch.countDown();<br /> }<br /> }).start();<br /> }<br /> countDownLatch.await();<br /> **long** t2 = System.currentTimeMillis();<br /> System.out.println(String.format("结果:%s,耗时(ms):%s", count, (t2 - t1)));<br /> }<br />}

输出:
结果:50000000,耗时(ms):971
结果:50000000,耗时(ms):915
结果:50000000,耗时(ms):920
结果:50000000,耗时(ms):923
结果:50000000,耗时(ms):910
结果:50000000,耗时(ms):916
结果:50000000,耗时(ms):923
结果:50000000,耗时(ms):916
结果:50000000,耗时(ms):912
结果:50000000,耗时(ms):908

平均耗时:920 毫秒
AtomicLong内部采用 CAS 的方式实现,并发量大的情况下,CAS 失败率比较高,导致性能比 synchronized 还低一些。并发量不是太大的情况下,CAS 性能还是可以的。
AtomicLong属于 JUC 中的原子类,还不是很熟悉的可以看一下:JUC 中原子类,一篇就够了

方式 3:LongAdder 实现

先介绍一下LongAdder,说到 LongAdder,不得不提的就是 AtomicLong,AtomicLong 是 JDK1.5 开始出现的,里面主要使用了一个 long 类型的 value 作为成员变量,然后使用循环的 CAS 操作去操作 value 的值,并发量比较大的情况下,CAS 操作失败的概率较高,内部失败了会重试,导致耗时可能会增加。
LongAdder 是 JDK1.8 开始出现的,所提供的 API 基本上可以替换掉原先的 AtomicLong。LongAdder 在并发量比较大的情况下,操作数据的时候,相当于把这个数字分成了很多份数字,然后交给多个人去管控,每个管控者负责保证部分数字在多线程情况下操作的正确性。当多线程访问的时,通过 hash 算法映射到具体管控者去操作数据,最后再汇总所有的管控者的数据,得到最终结果。相当于降低了并发情况下锁的粒度,所以效率比较高,看一下下面的图,方便理解:
image.png
代码:
package com.itsoku.chat32;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;

/
微信公众号:程序员路人,个人博客:http://www.itsoku.com
/
public class Demo3 {
static LongAdder count = new** LongAdder();

  1. **public** **static** **void** **incr**() {<br /> count.increment();<br /> }
  2. **public** **static** **void** **main**(String[] args) **throws** ExecutionException, InterruptedException {<br /> **for** (**int** i = 0; i < 10; i++) {<br /> count.reset();<br /> m1();<br /> }<br /> }
  3. **private** **static** **void** **m1**() **throws** ExecutionException, InterruptedException {<br /> **long** t1 = System.currentTimeMillis();<br /> **int** threadCount = 50;<br /> CountDownLatch countDownLatch = **new** CountDownLatch(threadCount);<br /> **for** (**int** i = 0; i < threadCount; i++) {<br /> **new** Thread(() -> {<br /> **try** {<br /> **for** (**int** j = 0; j < 1000000; j++) {<br /> incr();<br /> }<br /> } **finally** {<br /> countDownLatch.countDown();<br /> }<br /> }).start();<br /> }<br /> countDownLatch.await();<br /> **long** t2 = System.currentTimeMillis();<br /> System.out.println(String.format("结果:%s,耗时(ms):%s", count.sum(), (t2 - t1)));<br /> }<br />}

输出:
结果:50000000,耗时(ms):206
结果:50000000,耗时(ms):105
结果:50000000,耗时(ms):107
结果:50000000,耗时(ms):107
结果:50000000,耗时(ms):105
结果:50000000,耗时(ms):99
结果:50000000,耗时(ms):106
结果:50000000,耗时(ms):102
结果:50000000,耗时(ms):106
结果:50000000,耗时(ms):102

平均耗时:100 毫秒
代码中new LongAdder()创建一个 LongAdder 对象,内部数字初始值是 0,调用increment()方法可以对 LongAdder 内部的值原子递增 1。reset()方法可以重置LongAdder的值,使其归 0。

方式 4:LongAccumulator 实现

LongAccumulator 介绍
LongAccumulator 是 LongAdder 的功能增强版。LongAdder 的 API 只有对数值的加减,而 LongAccumulator 提供了自定义的函数操作,其构造函数如下:
/
accumulatorFunction:需要执行的二元函数(接收2个long作为形参,并返回1个long)
identity:初始值
/
public LongAccumulator(LongBinaryOperator accumulatorFunction, long identity) {
this.function = accumulatorFunction;
base = this.identity = identity;
}

示例代码:
package com.itsoku.chat32;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;

/
微信公众号:程序员路人,个人博客:http://www.itsoku.com
/
public class Demo4 {
static LongAccumulator count = new** LongAccumulator((x, y) -> x + y, 0L);

  1. **public** **static** **void** **incr**() {<br /> count.accumulate(1);<br /> }
  2. **public** **static** **void** **main**(String[] args) **throws** ExecutionException, InterruptedException {<br /> **for** (**int** i = 0; i < 10; i++) {<br /> count.reset();<br /> m1();<br /> }<br /> }
  3. **private** **static** **void** **m1**() **throws** ExecutionException, InterruptedException {<br /> **long** t1 = System.currentTimeMillis();<br /> **int** threadCount = 50;<br /> CountDownLatch countDownLatch = **new** CountDownLatch(threadCount);<br /> **for** (**int** i = 0; i < threadCount; i++) {<br /> **new** Thread(() -> {<br /> **try** {<br /> **for** (**int** j = 0; j < 1000000; j++) {<br /> incr();<br /> }<br /> } **finally** {<br /> countDownLatch.countDown();<br /> }<br /> }).start();<br /> }<br /> countDownLatch.await();<br /> **long** t2 = System.currentTimeMillis();<br /> System.out.println(String.format("结果:%s,耗时(ms):%s", count.longValue(), (t2 - t1)));<br /> }<br />}

输出:
结果:50000000,耗时(ms):138
结果:50000000,耗时(ms):111
结果:50000000,耗时(ms):111
结果:50000000,耗时(ms):103
结果:50000000,耗时(ms):103
结果:50000000,耗时(ms):105
结果:50000000,耗时(ms):101
结果:50000000,耗时(ms):106
结果:50000000,耗时(ms):102
结果:50000000,耗时(ms):103

平均耗时:100 毫秒
LongAccumulator的效率和LongAdder差不多,不过更灵活一些。
调用new LongAdder()等价于new LongAccumulator((x, y) -> x + y, 0L)。
从上面 4 个示例的结果来看,LongAdder、LongAccumulator全面超越同步锁及AtomicLong的方式,建议在使用AtomicLong的地方可以直接替换为LongAdder、LongAccumulator,吞吐量更高一些。