简介
synchronized是Java原生提供的用于在多线程环境中保证同步的关键字,底层是通过修改对象头中的MarkWord来实现的。
ReentrantLock是Java语言层面提供的用于在多线程环境中保证同步的类,底层是通过原子更新状态变量state来实现的。
既然有了synchronized的关键字来保证同步了,为什么还要实现一个ReentrantLock类呢?它们之间有什么异同呢?
ReentrantLock VS synchronized
对比测试
在测试之前,我们先预想一下结果,随着线程数的不断增加,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() +