比较几种自增操作时间复杂度

比较几种Counter的自增操作时间

编写一个没有做任何处理的自增

static class StupidCounter implements Counter{

    private long counter = 0;

    @Override
    public void increment() {
        counter++;
    }

    @Override
    public long getCounter() {
        return counter;
    }
}

编写一个加内置锁的自增

static class SyncCounter implements Counter{
    private long counter = 0;

    @Override
    public synchronized   void increment() {
        counter++;
    }

    @Override
    public long getCounter() {
        return counter;
    }
}

编写一个使用原子类型的自增

static class AtomicCounter implements Counter{
    private AtomicLong counter = new AtomicLong(0);


    @Override
    public synchronized   void increment() {
        counter.incrementAndGet();
    }

    @Override
    public long getCounter() {
        return counter.get();
    }
}

编写一个加显示锁的自增

static class LockCounter implements Counter{
    private final Lock lock = new ReentrantLock();

    private long counter = 0;

    @Override
    public    void increment() {
        try {
            lock.lock();
            counter++;
        }finally {
            lock.unlock();
        }
    }

    @Override
    public long getCounter() {
        return counter;
    }
}

编写一个利用Unsafe的CAS操作的自增

static class CASCounter implements Counter{

    private  Unsafe unsafe ;
    private  long offset;//偏移量
    private volatile long counter = 0;

    CASCounter() throws Exception {
        unsafe = getUnsafe();//利用反射获取
        assert unsafe != null;
        offset = unsafe.objectFieldOffset(getClass().getDeclaredField("counter"));//获取偏移量
    }


    @Override
    public   void increment() {
        long current = counter;
        while (!unsafe.compareAndSwapLong(this,offset,current,current+1)){
            current = counter;
        }
    }

    @Override
    public long getCounter() {
        return counter;
    }
}

测试

    public static void main(String[] args) throws Exception {
        ExecutorService executorService = Executors.newFixedThreadPool(1000);

        Counter counter = new CASCounter();

        long start = System.currentTimeMillis();
        for (int i = 0;i<1000;i++){
            executorService.submit(new CounterRunnable(counter,10000));
        }

        executorService.shutdown();
        executorService.awaitTermination(1, TimeUnit.HOURS);
        long end = System.currentTimeMillis();
        System.out.println("Counter result : " + counter.getCounter());
        System.out.println("Time passed in ms : " + (end-start));
    }

结果

/**
 * StupidCounter :
 * Counter result : 9849270
 * Time passed in ms : 696
 *
 *
 * SyncCounter :
 * Counter result : 10000000
 * Time passed in ms : 966
 *
 * LockCounter :
 * Counter result : 10000000
 * Time passed in ms : 452
 *
 * AtomicCounter :
 * Counter result : 10000000
 * Time passed in ms : 859
 *
 * CASCounter :
 * Counter result : 10000000
 * Time passed in ms : 1144
 */

从结果上看

  • 如果不进行任何操作,结果会出问题
  • 显示锁速度的最快,其次是原子类型
  • 可以发现内置锁的速度没有特别差
  • 使用Unsafe操作最慢
全部评论

相关推荐

不愿透露姓名的神秘牛友
11-21 19:05
面试官_我太想进步了:混学生会的,难怪简历这么水
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
11-27 10:52
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务