아래 코드를 참조하십시오. 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