Java 为什么在演示中同步工作时ReentrantLock不工作?

Java 为什么在演示中同步工作时ReentrantLock不工作?,java,java-8,synchronized,reentrantlock,Java,Java 8,Synchronized,Reentrantlock,我正在努力学习这类教程。我在as上有一个以-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

我正在努力学习这类教程。我在as上有一个以
-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();
        }
    }
}
这里遗漏了什么


任何帮助都将不胜感激;)

我有点傻

它是这样工作的,因为我实际上是在锁定不同的对象

private static synchronized int getCount()
等于

private static synchronized (ReentrantLockZero.class) int getCount()
newreentrantlock()
始终是一个新对象,使用不同的锁无法消除此问题

我真傻,下面的演示很容易修复它

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();
        }
    }
}
公共类ReentrantLockZero{
private static ReentrantLock CountLock=new ReentrantLock();
专用静态int synchronizedCount=0;
私有静态int lockedCount=0;
私有静态最终整数结果计数=10\u 000;
公共静态void main(字符串…参数)引发异常{
ThreadPoolExecutor ThreadPoolExecutor=getMyCachedThreadPool();
对于(int i=0;i

为什么
同步的
工作?因为只有一个锁,两个方法都锁定,所以竞争条件直接解决


有点容易被教程愚弄;我感到羞耻;(

我有点傻

它是这样工作的,因为我实际上是在锁定不同的对象

private static synchronized int getCount()
等于

private static synchronized (ReentrantLockZero.class) int getCount()
new ReentrantLock();
始终是一个新对象,使用不同的锁无法消除这种情况

我真傻,下面的演示很容易修复它

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();
        }
    }
}
公共类ReentrantLockZero{
private static ReentrantLock CountLock=new ReentrantLock();
专用静态int synchronizedCount=0;
私有静态int lockedCount=0;
私有静态最终整数结果计数=10\u 000;
公共静态void main(字符串…参数)引发异常{
ThreadPoolExecutor ThreadPoolExecutor=getMyCachedThreadPool();
对于(int i=0;i

为什么
synchronized
可以工作?因为只有一个锁,两个方法都在锁定,所以竞争条件直接得到解决


有点容易被教程愚弄;真为我感到羞耻;(

“我自己有点傻”就像我们大家一样。它被称为编程(0:谢谢你,c0der。祝你周末愉快;)“我有点傻”就像我们大家一样。它被称为编程(0:谢谢你,c0der。祝你周末愉快;)