死磕java_死磕 java同步系列之ReentrantLock VS synchronized

简介

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

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

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

ReentrantLock VS synchronized

261e1a0f779171c3a66d93f80d0008c4.png

对比测试

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

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

到底是不是这样呢?

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

public class ReentrantLockVsSynchronizedTest { public static AtomicInteger a = new AtomicInteger(0); public static LongAdder b = new LongAdder(); public static int c = 0; public static int d = 0; public static int e = 0; 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("-------------------------------------"); } 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); } public static void testAtomicInteger(int threadCount, int loopCount) throws InterruptedException { long start = System.currentTimeMillis(); CountDownLatch countDownLatch = new CountDownLatch(threadCount); for (int i = 0; i < threadCount; i++) { new Thread(() -> { for (int j = 0; j < loopCount; j++) { a.incrementAndGet(); } countDownLatch.countDown(); }).start(); } countDownLatch.await(); System.out.println("testAtomicInteger: result=" + a.get() + 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值