美文网首页
递增场景

递增场景

作者: zzj0990 | 来源:发表于2020-12-31 13:09 被阅读0次

synchronized
使用系统重量级的锁

AtomicXXX
使用无锁-自旋锁,CAS-类似于乐观锁, 所以效率优于synchronized

LongAdder
分段锁,每段分别使用CAS原理递增,最后再合计。线程较多时,效率优于以上两种。

代码示范

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;
public class T02_AtomicVsSyncVsLongAdder {
    static long count2 = 0L;
    static AtomicLong count1 = new AtomicLong(0L);
    static LongAdder count3 = new LongAdder();

    public static void main(String[] args) throws Exception {
        Thread[] threads = new Thread[1000];

        for (int i = 0; i < threads.length; i++) {
            threads[i] =
                    new Thread(() -> {
                        for (int k = 0; k < 100000; k++) count1.incrementAndGet();
                    });
        }

        long start = System.currentTimeMillis();

        for (Thread t : threads) t.start();

        for (Thread t : threads) t.join();

        long end = System.currentTimeMillis();

        //TimeUnit.SECONDS.sleep(10);

        System.out.println("Atomic: " + count1.get() + " time " + (end - start));
        //-----------------------------------------------------------
        Object lock = new Object();

        for (int i = 0; i < threads.length; i++) {
            threads[i] =
                    new Thread(new Runnable() {
                        @Override
                        public void run() {

                            for (int k = 0; k < 100000; k++)
                                synchronized (lock) {
                                    count2++;
                                }
                        }
                    });
        }

        start = System.currentTimeMillis();

        for (Thread t : threads) t.start();

        for (Thread t : threads) t.join();

        end = System.currentTimeMillis();


        System.out.println("Sync: " + count2 + " time " + (end - start));


        //----------------------------------
        for (int i = 0; i < threads.length; i++) {
            threads[i] =
                    new Thread(() -> {
                        for (int k = 0; k < 100000; k++) count3.increment();
                    });
        }

        start = System.currentTimeMillis();

        for (Thread t : threads) t.start();

        for (Thread t : threads) t.join();

        end = System.currentTimeMillis();

        //TimeUnit.SECONDS.sleep(10);

        System.out.println("LongAdder: " + count1.longValue() + " time " + (end - start));

    }
    static void microSleep(int m) {
        try {
            TimeUnit.MICROSECONDS.sleep(m);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

相关文章

  • 递增场景

    synchronized使用系统重量级的锁 AtomicXXX使用无锁-自旋锁,CAS-类似于乐观锁, 所以效率优...

  • 枚举类型使用

    废话不多说,直接来干货 字符串枚举 数字枚举 默认从0开始,如果设置Red = 1,则从1开始依次递增 场景 1,...

  • ElementUI动态生成el-checkbox后,取消一项后下

    场景:一个数组List用来循环el-checkbox,List可能递增也可能递减。 问题描述:默认勾选了4项,当点...

  • 搜索,没那么难

    1、顺序查询简单适用简单场景2、二分查找递增或者递减,列表3、 杨氏矩陈4、分块查询5、索引与倒排索引

  • Redis 数据类型与应用场景

    数据类型应用场景说明String各种计数器,比如评论计数器。包含 decr,incr 命令,执行递减与递增操作。L...

  • 收益递增

    传统经济学家认为,穷国要想变富,就要想办法拥有建设基础设施、推广教育、积累技术资源。 作为一个非常贫穷的国家基础设...

  • 架构-分布式ID生成系统

    分布式ID特点: 唯一性 趋势递增 单调递增(严格递增)分布式系统中,如果不引用分布式锁,单调递增意义不大。 安全...

  • Jmeter压测,BeanShell内存溢出问题的排查及解决

    测试场景 需要使用Jmeter对Go语言实现的后端服务执行阶梯递增式压测,每阶梯增加2000线程,每个阶梯维持1小...

  • js中++i和i++的理解

    关于++i和i++的理解 最常听说的一句话就是:前置递增:先递增后赋值后置递增:先赋值后递增 再加上常用的写法是a...

  • 基于Redis实现分布式锁

    背景 在很多互联网产品应用中,有些场景需要加锁处理,比如:秒杀,全局递增ID,楼层生成等等。大部分的解决方案是基于...

网友评论

      本文标题:递增场景

      本文链接:https://www.haomeiwen.com/subject/driloktx.html