看到下面的代碼,這讓我感到困惑,在類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));
}
}
}
爲什麼你將'class'鎖定在第一位? – emotionlessbananas
「鎖只能在課堂上或實例本身中使用」這裏不清楚你的意思。 –
還要注意''synchronized''不能「鎖定」任何東西。它*獲取對象監視器上的獨佔鎖(在本例中爲'ConfigurationManager.class'),以防止同時運行同一個鎖的多個同步方法或塊。 – Kayaman