深读源码-java同步系列之ReentrantLock与synchronized对比分析

60 篇文章 4 订阅
28 篇文章 0 订阅

问题

(1)ReentrantLock有哪些优点?

(2)ReentrantLock有哪些缺点?

(3)ReentrantLock是否可以完全替代synchronized?

简介

synchronized是Java原生提供的用于在多线程环境中保证同步的关键字,底层是通过修改对象头中的MarkWord来实现的。

ReentrantLock是Java语言层面提供的用于在多线程环境中保证同步的类,底层是通过原子更新状态变量state来实现的。

既然有了synchronized的关键字来保证同步了,为什么还要实现一个ReentrantLock类呢?它们之间有什么异同呢?

ReentrantLock VS synchronized

直接上表格:(手机横屏查看更方便)

功能ReentrantLocksynchronized
可重入支持支持
非公平支持(默认)支持
加锁/解锁方式需要手动加锁、解锁,一般使用try..finally..保证锁能够释放手动加锁,无需刻意解锁
按key锁不支持,比如按用户id加锁支持,synchronized加锁时需要传入一个对象
公平锁支持,new ReentrantLock(true)不支持
中断支持,lockInterruptibly()不支持
尝试加锁支持,tryLock()不支持
超时锁支持,tryLock(timeout, unit)不支持
获取当前线程获取锁的次数支持,getHoldCount()不支持
获取等待的线程支持,getWaitingThreads()不支持
检测是否被当前线程占有支持,isHeldByCurrentThread()不支持
检测是否被任意线程占有支持,isLocked()不支持
条件锁可支持多个条件,condition.await(),condition.signal(),condition.signalAll()只支持一个,obj.wait(),obj.notify(),obj.notifyAll()

对比测试

在测试之前,我们先预想一下结果,随着线程数的不断增加,ReentrantLock(fair)、ReentrantLock(unfair)、synchronized三者的效率怎样呢?

我猜测应该是ReentrantLock(unfair)> synchronized > ReentrantLock(fair)。

到底是不是这样呢?

直接上测试代码:(为了全面对比,这里把AtomicInteger和LongAdder也拿来一起对比了)

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.LongAdder;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.IntStream;

/**
 * @ClassName ReentrantLockVsSynchronizedTest
 * @Author suidd
 * @Description ReentrantLock与synchronized对比分析
 * @Date 10:35 2020/5/24
 * @Version 1.0
 **/
public class ReentrantLockVsSynchronizedTest {
    public static AtomicInteger a = new AtomicInteger(0);
    public static LongAdder b = new LongAdder();
    public static int c;
    public static int d;
    public static int e;

    public static final ReentrantLock fairLock = new ReentrantLock(true);
    public static final ReentrantLock unfairLock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {
        System.out.println("-------------------------------------");
        testAll(1, 100000);
        System.out.println("-------------------------------------");
        testAll(2, 100000);
        System.out.println("-------------------------------------");
        testAll(4, 100000);
        System.out.println("-------------------------------------");
        testAll(6, 100000);
        System.out.println("-------------------------------------");
        testAll(8, 100000);
        System.out.println("-------------------------------------");
        testAll(10, 100000);
        System.out.println("-------------------------------------");
        testAll(50, 100000);
        System.out.println("-------------------------------------");
        testAll(100, 100000);
        System.out.println("-------------------------------------");
        testAll(200, 100000);
        System.out.println("-------------------------------------");
        testAll(500, 100000);
        System.out.println("-------------------------------------");
        testAll(1000, 1000000);
        System.out.println("-------------------------------------");
        testAll(500, 10000);
        System.out.println("-------------------------------------");
        testAll(500, 1000);
        System.out.println("-------------------------------------");
        testAll(500, 100);
        System.out.println("-------------------------------------");
        testAll(500, 10);
        System.out.println("-------------------------------------");
        testAll(500, 1);
        System.out.println("-------------------------------------");
    }

    /**
     * @return
     * @Author suidd
     * @Description 测试
     * @Date 10:58 2020/5/24
     * @Param
     * @Version 1.0
     **/
    public static void testAll(int threadCount, int loopCount) throws InterruptedException {
        testAtomicInteger(threadCount, loopCount);
        testLongAdder(threadCount, loopCount);
        testSynchronized(threadCount, loopCount);
        testReentrantLockUnfair(threadCount, loopCount);
        //testReentrantLockFair(threadCount, loopCount);
    }

    /**
     * @return
     * @Author suidd
     * @Description 测试AtomicInteger
     * @Date 10:50 2020/5/24
     * @Param
     * @Version 1.0
     **/
    public static void testAtomicInteger(int threadCount, int loopCount) throws InterruptedException {
        long start = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(threadCount);

        IntStream.range(0, threadCount)
                .forEach(i -> new Thread(() -> {
                    IntStream.range(0, loopCount)
                            .forEach(j -> a.incrementAndGet());
                    countDownLatch.countDown();
                }).start());

        countDownLatch.await();

        System.out.println("testAtomicInteger: result=" + a.get() + ", threadCount=" + threadCount + ", loopCount=" + loopCount + ", elapse=" + (System.currentTimeMillis() - start));
    }

    /**
     * @return
     * @Author suidd
     * @Description 测试LongAdder
     * @Date 10:51 2020/5/24
     * @Param
     * @Version 1.0
     **/
    public static void testLongAdder(int threadCount, int loopCount) throws InterruptedException {
        long start = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(threadCount);

        IntStream.range(0, threadCount)
                .forEach(i -> new Thread(() -> {
                    IntStream.range(0, loopCount)
                            .forEach(j -> b.increment());
                    countDownLatch.countDown();
                }).start());

        countDownLatch.await();

        System.out.println("testLongAdder: result=" + b.sum() + ", threadCount=" + threadCount + ", loopCount=" + loopCount + ", elapse=" + (System.currentTimeMillis() - start));
    }

    /**
     * @return
     * @Author suidd
     * @Description 测试ReentrantLock公平锁
     * @Date 10:52 2020/5/24
     * @Param
     * @Version 1.0
     **/
    public static void testReentrantLockFair(int threadCount, int loopCount) throws InterruptedException {
        long start = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(threadCount);

        IntStream.range(0, threadCount)
                .forEach(i -> new Thread(() -> {
                    IntStream.range(0, loopCount)
                            .forEach(j -> {
                                fairLock.lock();
                                c++;
                                fairLock.unlock();
                            });
                    countDownLatch.countDown();
                }).start());

        countDownLatch.await();

        System.out.println("testReentrantLockFair: result=" + c + ", threadCount=" + threadCount + ", loopCount=" + loopCount + ", elapse=" + (System.currentTimeMillis() - start));
    }

    /**
     * @return
     * @Author suidd
     * @Description 测试ReentrantLock非公平锁
     * @Date 10:55 2020/5/24
     * @Param
     * @Version 1.0
     **/
    public static void testReentrantLockUnfair(int threadCount, int loopCount) throws InterruptedException {
        long start = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(threadCount);

        IntStream.range(0, threadCount)
                .forEach(i -> new Thread(() -> {
                    IntStream.range(0, loopCount)
                            .forEach(j -> {
                                unfairLock.lock();
                                d++;
                                unfairLock.unlock();
                            });
                    countDownLatch.countDown();
                }).start());

        countDownLatch.await();

        System.out.println("testReentrantLockUnfair: result=" + d + ", threadCount=" + threadCount + ", loopCount=" + loopCount + ", elapse=" + (System.currentTimeMillis() - start));
    }

    /**
     * @return
     * @Author suidd
     * @Description 测试Synchronized
     * @Date 10:57 2020/5/24
     * @Param
     * @Version 1.0
     **/
    public static void testSynchronized(int threadCount, int loopCount) throws InterruptedException {
        long start = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(threadCount);

        IntStream.range(0, threadCount)
                .forEach(i -> new Thread(() -> {
                    IntStream.range(0, loopCount)
                            .forEach(j -> {
                                synchronized (ReentrantLockVsSynchronizedTest.class) {
                                    e++;
                                }
                            });

                    countDownLatch.countDown();
                }).start());

        countDownLatch.await();

        System.out.println("testSynchronized: result=" + e + ", threadCount=" + threadCount + ", loopCount=" + loopCount + ", elapse=" + (System.currentTimeMillis() - start));
    }
}

运行这段代码,你会发现结果大大出乎意料,真的是不测不知道,一测吓一跳,运行后发现以下规律:

随着线程数的不断增加,synchronized的效率竟然比ReentrantLock非公平模式要高!

作者的电脑上大概是高3倍左右,我的运行环境是4核8G,java版本是8,请大家一定要在自己电脑上运行一下,并且最好能给我反馈一下。

作者又使用Java7及以下的版本运行了,发现在Java7及以下版本中synchronized的效率确实比ReentrantLock的效率低一些。


本人8核8G,测试出来的数据是 synchronized的效率比ReentrantLock非公平模式要低!

总结

(1)synchronized是Java原生关键字锁;

(2)ReentrantLock是Java语言层面提供的锁;

(3)ReentrantLock的功能非常丰富,解决了很多synchronized的局限性;

(4)至于在非公平模式下,ReentrantLock与synchronized的效率孰高孰低,作者给出的结论是随着Java版本的不断升级,synchronized的效率只会越来越高

彩蛋

既然ReentrantLock的功能更丰富,而且效率也不低,我们是不是可以放弃使用synchronized了呢?

答:我认为不是。因为synchronized是Java原生支持的,随着Java版本的不断升级,Java团队也是在不断优化synchronized,所以我认为在功能相同的前提下,最好还是使用原生的synchronized关键字来加锁,这样我们就能获得Java版本升级带来的免费的性能提升的空间。

另外,在Java8的ConcurrentHashMap中已经把ReentrantLock换成了synchronized来分段加锁了,这也是Java版本不断升级带来的免费的synchronized的性能提升。

推荐阅读

  1. 《深读源码-java魔法类之Unsafe解析》

  2. 《深读源码-java同步系列之JMM(Java Memory Model)》

  3. 《深读源码-java同步系列之volatile解析》

  4. 《深读源码-java同步系列之synchronized解析》

  5. 《深读源码-java同步系列之自己手写一个锁Lock》

  6. 《深读源码-java同步系列之AQS简介》

  7. 《深读源码-java同步系列之ReentrantLock源码解析(一)——公平锁、非公平锁》

  8. 《深读源码-java同步系列之ReentrantLock源码解析(二)——条件锁》


参考链接:https://www.cnblogs.com/tong-yuan/p/11001615.html 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值