以下代码摘自 MS 用于创建新安全令牌服务网站的 (Windows Identity Foundation SDK) 模板。
public static CustomSecurityTokenServiceConfiguration Current
{
get
{
var key = CustomSecurityTokenServiceConfigurationKey;
var httpAppState = HttpContext.Current.Application;
var customConfiguration = httpAppState.Get(key)
as CustomSecurityTokenServiceConfiguration;
if (customConfiguration == null)
{
lock (syncRoot)
{
customConfiguration = httpAppState.Get(key)
as CustomSecurityTokenServiceConfiguration;
if (customConfiguration == null)
{
customConfiguration =
new CustomSecurityTokenServiceConfiguration();
httpAppState.Add(key, customConfiguration);
}
}
}
return customConfiguration;
}
}
我对多线程编程比较陌生。我假设该声明的原因lock
是在两个 Web 请求同时到达网站的情况下使此代码线程安全。
但是,我会认为 usinglock (syncRoot)
没有意义,因为syncRoot
指的是该方法正在运行的当前实例......但这是一个静态方法!
这有什么意义?