选择显示字体大小

visual basic.net实现双检锁(dcl)模式(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类  


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons