以下のコードを参照してください。これは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