java - 使用同步锁时的困惑

标签 java concurrency java.util.concurrent concurrent-programming

<分区>

请看下面的代码,这让我很困惑,在DynamicPropertyFactory类中,它锁定了ConfigurationManager.class,据我了解,锁定仅在类或实例本身起作用。这个怎么理解?

public class *DynamicPropertyFactory*{
      public static *DynamicPropertyFactory* initWithConfigurationSource(AbstractConfiguration config) {
            synchronized (**ConfigurationManager.class**) {
                if (config == null) {
                    throw new NullPointerException("config is null");
                }
                if (ConfigurationManager.isConfigurationInstalled() && config != ConfigurationManager.instance) {
                    throw new IllegalStateException("ConfigurationManager is already initialized with configuration "
                            + ConfigurationManager.getConfigInstance());
                }
                if (config instanceof DynamicPropertySupport) {
                    return initWithConfigurationSource((DynamicPropertySupport) config);
                }
                return initWithConfigurationSource(new ConfigurationBackedDynamicPropertySupportImpl(config));
            }
        }
    }

最佳答案

as my understanding, the lock works only in the class or the instance itself

这是错误的,不清楚你想说什么。

可以对任何不指向 null 的引用进行锁定。

synchronized (ConfigurationManager.class)

这一行意味着在 class 对象上正在获取锁。

关于java - 使用同步锁时的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38714126/

相关文章:

java - 如果weakCompareAndSet 的实现与compareAndSet 完全一样,它怎么会虚假失败?

Java:notifyObservers不并发?

java - 同步三个线程

java - 如何检测线程正在等待原子引用

java - scala ScheduledThreadPoolExecutor

java - 运行非 GUI Jar 文件

java - Google App Engine - 内存缓存 - 热键警告

java - Android FireBase 存储下载 - 如何检测网络中断?

python - multiprocessing.Semaphore 和 multiprocessing.BoundedSemaphore 有什么区别?

multithreading - 临时多线程和超线程有什么区别?