为什么ReentrantLock在演示中同步工作时无效?

问题描述 投票:1回答:1

我试图遵循ReentrantLock Example in Java, Difference between synchronized vs ReentrantLock类教程。我有一个以-ea开头的演示

public class ReentrantLockZero {
    private static ReentrantLock CountLock = new ReentrantLock();
    private static int count = 0;
    private static final int RESULT_COUNT = 10_000;

    public static void main(String... args) throws Exception {
        ThreadPoolExecutor threadPoolExecutor = getMyCachedThreadPool();
        for (int i = 0; i < RESULT_COUNT; ++i) {
            threadPoolExecutor.submit(ReentrantLockZero::getCount);
            threadPoolExecutor.submit(ReentrantLockZero::getCountUsingLock);
        }
        threadPoolExecutor.shutdown();
        threadPoolExecutor.awaitTermination(10, TimeUnit.SECONDS);
        assert count == RESULT_COUNT * 2;
    }

    private static synchronized int getCount() {
        count++;
        System.out.println(Thread.currentThread().getName() + " counting in synchronized: " + count);
        return count;
    }

    private static int getCountUsingLock() {
        CountLock.lock();
        try {
            count++;
            System.out.println(Thread.currentThread().getName() + " counting in lock: " + count);
            return count;
        } finally {
            CountLock.unlock();
        }
    }
}

当使用ReentrantLock作为第二种方法getCountUsingLock时,我会得到java.lang.AssertionError但是当我评论它们使用synchronized时,它会没问题。

考虑到它的ReentrantLock,我删除了类中定义的CountLock并使用本地锁定如下,但它仍然无效。

private static int getCountUsingLock() {
    ReentrantLock countLock = new ReentrantLock();
    countLock.lock();
    try {
        count++;
        System.out.println(Thread.currentThread().getName() + " counting in lock: " + count);
        return count;
    } finally {
        countLock.unlock();
    }
}

这里遗漏的是什么?

任何帮助将不胜感激 ;)

java java-8 synchronized reentrantlock
1个回答
2
投票

我自己就是那种傻瓜。

它的工作原理是因为我实际上是在锁定不同的对象。

private static synchronized int getCount()

等于

private static synchronized (ReentrantLockZero.class) int getCount()

new ReentrantLock();总是一个新的对象,没有办法消除使用不同锁的race condition

这么愚蠢,我很容易通过以下演示来解决

public class ReentrantLockZero {
    private static ReentrantLock CountLock = new ReentrantLock();
    private static int synchronisedCount = 0;
    private static int lockedCount = 0;
    private static final int RESULT_COUNT = 10_000;

    public static void main(String... args) throws Exception {
        ThreadPoolExecutor threadPoolExecutor = getMyCachedThreadPool();
        for (int i = 0; i < RESULT_COUNT; ++i) {
            threadPoolExecutor.submit(ReentrantLockZero::getSynchronisedCount);
            threadPoolExecutor.submit(ReentrantLockZero::getCountUsingLock);
        }
        threadPoolExecutor.shutdown();
        threadPoolExecutor.awaitTermination(10, TimeUnit.SECONDS);
        assert synchronisedCount == RESULT_COUNT;
        assert lockedCount == RESULT_COUNT;
    }

    private static synchronized int getSynchronisedCount() {
        synchronisedCount++;
        System.out.println(Thread.currentThread().getName() + " counting in synchronized: " + synchronisedCount);
        return synchronisedCount;
    }

    private static int getCountUsingLock() {
        CountLock.lock();
        try {
            lockedCount++;
            System.out.println(Thread.currentThread().getName() + " counting in lock: " + lockedCount);
            return lockedCount;
        } finally {
            CountLock.unlock();
        }
    }
}

为什么synchronized工作?因为只有一个锁,两个方法都锁定,因此竞争条件直接解决。

很容易被教程愚弄;对我感到羞耻;(

© www.soinside.com 2019 - 2024. All rights reserved.