麻烦请教大家一个问题: 看到张老三的关于一个用double lock来产生线程安全的单例对象的方法突然有一个疑问,
第一种方法:如果把这句话(事实上是一个随便的初始化过程)
Dim source As IConfigurationSource = System.Configuration.ConfigurationSettings.GetConfig("activerecord")
ActiveRecordStarter.Initialize([Assembly].Load("ASPNET.StarterKit.Portal.Model"), source)
ActiveRecordStarter.Initialize([Assembly].Load("ASPNET.StarterKit.Portal.Model"), source)
放在Double Lock的话而这个Double Lock放在一个普通的类文件(可能是基类)的话
private static readonly object lockObj = new object();
if ( _factory == null ) {
lock ( lockObj ) {
if ( _factory == null ) {
// 放在这里;
}
} // end lock
}
if ( _factory == null ) {
lock ( lockObj ) {
if ( _factory == null ) {
// 放在这里;
}
} // end lock
}
第二种方法:放在全局文件里面
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' 在应用程序启动时激发
Dim source As IConfigurationSource = System.Configuration.ConfigurationSettings.GetConfig("activerecord")
ActiveRecordStarter.Initialize([Assembly].Load("ASPNET.StarterKit.Portal.Model"), source)
End Sub
' 在应用程序启动时激发
Dim source As IConfigurationSource = System.Configuration.ConfigurationSettings.GetConfig("activerecord")
ActiveRecordStarter.Initialize([Assembly].Load("ASPNET.StarterKit.Portal.Model"), source)
End Sub
这两种方法能产生一样的效果吗(也就是说只初始化一次)?还是产生不同的效果?