前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自增效率比较

自增效率比较

作者头像
九转成圣
发布2024-04-10 16:46:32
730
发布2024-04-10 16:46:32
举报
文章被收录于专栏:csdn

自增效率比较

标签:多线程

【参考】volatile 解决多线程内存不可见问题。对于一写多读,是可以解决变量同步问题,但是如果多写,同样无法解决线程安全问题。 说明:如果是 count++操作,使用如下类实现:AtomicInteger count = new AtomicInteger(); count.addAndGet(1); 如果是 JDK8,推荐使用 LongAdder 对象,比 AtomicLong 性能更好(减少乐观锁的重试次数)。

代码演示

代码语言:javascript
复制
class Resource {
    int count;

    public synchronized void incrementBySynchronized() {
        count++;
    }

    AtomicLong atomicLong = new AtomicLong();

    public void incrementByAtomicLong() {
        atomicLong.incrementAndGet();
    }

    LongAdder longAdder = new LongAdder();

    public void incrementByLongAdder() {
        longAdder.increment();
    }

    LongAccumulator longAccumulator = new LongAccumulator((x, y) -> x + y, 0);

    public void incrementByLongAccumulator() {
        longAccumulator.accumulate(1);
    }
}
代码语言:javascript
复制
public static void main(String[] args) throws InterruptedException {
    int threadCount = 50;
    int _1W = 10000;
    Resource resource = new Resource();
    CountDownLatch countDownLatch1 = new CountDownLatch(threadCount);
    CountDownLatch countDownLatch2 = new CountDownLatch(threadCount);
    CountDownLatch countDownLatch3 = new CountDownLatch(threadCount);
    CountDownLatch countDownLatch4 = new CountDownLatch(threadCount);
    long startTime, endTime;

    startTime = System.currentTimeMillis();
    for (int i = 0; i < threadCount; i++) {
        new Thread(() -> {
            for (int j = 0; j < 100 * _1W; j++) {
                resource.incrementBySynchronized();
            }
            countDownLatch1.countDown();
        }).start();
    }
    countDownLatch1.await();
    endTime = System.currentTimeMillis();
    System.out.println("incrementBySynchronized " + resource.count + " 耗时 " + (endTime - startTime));

    startTime = System.currentTimeMillis();
    for (int i = 0; i < threadCount; i++) {
        new Thread(() -> {
            for (int j = 0; j < 100 * _1W; j++) {
                resource.incrementByAtomicLong();
            }
            countDownLatch2.countDown();
        }).start();
    }
    countDownLatch2.await();
    endTime = System.currentTimeMillis();
    System.out.println("incrementByAtomicLong " + resource.atomicLong.get() + " 耗时 " + (endTime - startTime));

    startTime = System.currentTimeMillis();
    for (int i = 0; i < threadCount; i++) {
        new Thread(() -> {
            for (int j = 0; j < 100 * _1W; j++) {
                resource.incrementByLongAdder();
            }
            countDownLatch3.countDown();
        }).start();
    }
    countDownLatch3.await();
    endTime = System.currentTimeMillis();
    System.out.println("incrementByLongAdder " + resource.longAdder.sum() + " 耗时 " + (endTime - startTime));

    startTime = System.currentTimeMillis();
    for (int i = 0; i < threadCount; i++) {
        new Thread(() -> {
            for (int j = 0; j < 100 * _1W; j++) {
                resource.incrementByLongAccumulator();
            }
            countDownLatch4.countDown();
        }).start();
    }
    countDownLatch4.await();
    endTime = System.currentTimeMillis();
    System.out.println("incrementByLongAccumulator " + resource.longAccumulator.get() + " 耗时 " + (endTime - startTime));
}
代码语言:javascript
复制
incrementBySynchronized 50000000 耗时 1301
incrementByAtomicLong 50000000 耗时 1011
incrementByLongAdder 50000000 耗时 74
incrementByLongAccumulator 50000000 耗时 82
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-04-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 自增效率比较
    • 标签:多线程
    • 代码演示
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档