| | Visual Basic.NET实现双检锁(DCL)模式 |
| | | | | | [文章信息] | | | 作者: | 阎宏博士 | | 时间: | 2005-02-26 | | 出处: | 天极网 | | 责任编辑: | 方舟 | |
| [文章导读] | | | 本文介绍了称为双检锁模式的代码模式的工作原理及其在单例模式及多例模式中的应用 | |
| |
|
| | | |
|
|
|
|
|
DCL模式的推广
上面所介绍的DCL模式的实现都是基于一个最为简单的逻辑,也就是单实例逻辑。这一逻辑还可以进一步推广成为更为一般的循环逻辑。
比如工厂对象可以控制产品类实例的数目有一个上限,这个上限为1时,就成为单实例逻辑;大于1时,就成为多实例逻辑。
如果产品对象是有状态的,工厂对象虽然不控制产品类实例的数目,但是却根据产品对象的状态循环使用产品类实例,比如对应每一种状态的产品类实例最多只允许一个(或N个),等等。
问答题
第1题、使用Mutex改写代码清单5。 第2题、使用Monitor改写代码清单5。 第3题、使用SyncLock改写代码清单5。 第4题、使用Monitor改写代码清单6。 第5题、使用SyncLock改写代码清单6。 第6题、使用Monitor改写代码清单9。 第7题、使用SyncLock改写代码清单9。
问答题答案
第1题答案、Mutex改写同步化代码清单5,结果如下:
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类 | 第2题答案、Monitor对象提供针对一个资源对象的同步锁。使用Monitor对象改写代码清单5,结果为:
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类 | 第3题答案、使用了SyncLock的版本如下:
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类 | 第4题答案、使用Monitor对象改写后的双检锁工厂类为:
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类 | 第5题答案、使用SyncLock改写后的双检锁工厂类为;
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类 | 第6题答案、使用Monitor对象改写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类 | 第7题答案、使用SyncLock改写后的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 | 代码清单16、二重检查的线程安全的Singleton类
|
|
|
|
|
|
|
|
|