扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:阎宏博士 来源:天极网 2007年11月9日
关键字:
Public Class Factory2A Private Shared instance As Product Private Shared m As Mutex = New Mutex() Private Sub New() System.Console.WriteLine("Factory object is created.") End Sub Public Shared Function GetInstance() As Product Thread.Sleep(10) m.WaitOne() If (instance Is Nothing) Then instance = New Product() End If m.ReleaseMutex() Return instance End Function End Class |
代码清单10、二重检查的线程安全的Singleton类 |
Public Class Factory2B Private Shared instance As Product Private Sub New() System.Console.WriteLine("Factory object is created.") End Sub Public Shared Function GetInstance() As Product Thread.Sleep(10) Monitor.Enter(GetType(Factory2B)) If (instance Is Nothing) Then instance = New Product() End If Monitor.Exit(GetType(Factory2B)) Return instance End Function End Class |
代码清单11、二重检查的线程安全的Singleton类 |
Public Class Factory2C Private Shared instance As Product Private Sub New() System.Console.WriteLine("Factory object is created.") End Sub Public Shared Function GetInstance() As Product Thread.Sleep(10) SyncLock (GetType(Factory2C)) If (instance Is Nothing) Then instance = New Product() End If End SyncLock Return instance End Function End Class |
代码清单12、二重检查的线程安全的Singleton类 |
Public Class Factory3A Private Shared instance As Product Public Shared Function GetInstance() As Product Thread.Sleep(10) If (instance Is Nothing) Then Monitor.Enter(GetType(Factory3A)) If (instance Is Nothing) Then instance = New Product() End If Monitor.Exit(GetType(Factory3A)) End If Return instance End Function End Class |
代码清单13、二重检查的线程安全的Singleton类 |
Public Class Factory3B Private Shared instance As Product Public Shared Function GetInstance() As Product Thread.Sleep(10) If (instance Is Nothing) Then SyncLock (GetType(Factory3B)) If (instance Is Nothing) Then instance = New Product() End If End SyncLock End If Return instance End Function End Class |
代码清单14、二重检查的线程安全的Singleton类 |
Public Class SingletonA Private Shared instance As SingletonA Public Sub New() System.Console.WriteLine("Singleton object is created.") End Sub Public Shared Function GetInstance() As SingletonA Thread.Sleep(10) If instance Is Nothing Then Monitor.Enter(GetType(SingletonA)) If instance Is Nothing Then instance = New SingletonA() End If Monitor.Exit(GetType(SingletonA)) End If Return instance End Function End Class |
代码清单15、二重检查的线程安全的Singleton类 |
Public Class SingletonB Private Shared instance As SingletonB Public Sub New() System.Console.WriteLine("Singleton object is created.") End Sub Public Shared Function GetInstance() As SingletonB Thread.Sleep(10) If instance Is Nothing Then SyncLock (GetType(SingletonB)) If instance Is Nothing Then instance = New SingletonB() End If End SyncLock End If Return instance End Function End Class |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者