工具软件   办公软件   操作系统   网络安全   设计在线   程序开发   教程宝典   软件下载   软件论坛
您的位置:软件 > 开发者网络 > 微软开发专栏 > Visual Studio.net专栏 > VB.net > 正文
Visual Basic.NET实现双检锁(DCL)模式
[文章信息]
作者:阎宏博士
时间:2005-02-26
出处:天极网
责任编辑:方舟
[文章导读]
本文介绍了称为双检锁模式的代码模式的工作原理及其在单例模式及多例模式中的应用
advertisement
热点推荐
· Visual Basic.NET实现双检锁(DCL)模式
· 动画图解:Word表格自动套用格式
· 游戏开发新手入门之Win32程序资源
· VB中使用DDE技术为应用程序增辉
· Java新手留意:Java编程三十条规则
[正文]

上一页  1 2 3 4  

  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类


上一页  1 2 3 4  

天极社区邀请您:写博客日记  上传相片   论坛聊天  订阅电子杂志  推荐网摘   免费图铃工具
笔名:   请您注意:

 遵守国家有关法律、法规,尊重网上道德,承担一切因您的行为而直接或间接引起的法律责任。

 天极网拥有管理笔名和留言的一切权利。
评论:
 
发表评论推荐给朋友我想参加相关培训打印我对此感兴趣订阅电子杂志
相关内容焦点新闻
  • 浅议Visual Basic.NET中HOOK的应用
  • 用VB.NET结合Excel设计统计生产报表
  • VB.NET上传图片并在DataGrid中显示
  • Visual Stuido.NET扩展存储过程
  • 用VB.net实现Smartphone中进程启动
  • 无需再购新浪股票 传盛大已总体控股新浪36%
  • 微软否认XP优惠遭冷遇 开始发放电子优惠券
  • 电盈网通公布中国业务发展计划 合作四大块
  • 中关村在线前员工将老东家告上法庭
  • EVD获2500台销售“大单” 市场普及做好准备
  • 新浪首次正式表态 反对盛大控制可以和谈
  • 财富"最受推崇公司"出炉 戴尔微软挤进前四
  • 盛大急切收购新浪 因为看中新浪3D游戏?
  • Advertisement