zoukankan      html  css  js  c++  java
  • 单件模式

    代码
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// 单件模式(线程安全的单件(双重锁定方法2:线程安全而且性能优越))
    /// </summary>
    public class Singleton
    {
        
    /// <summary>
        
    /// 定义自身静态变量实例默认为null
        
    /// </summary>
        private static Singleton instance=null;
        
        
    private static readonly object olock=new Object();
        
    ///私有构造函数
        
    /// 之所以不为public类型,因为是为了避免发生new Singleton()产生实例
        private Singleton()
        {}
        
    public static Singleton getInstance()
        {
            
    if(instance==null)
            {
                
    lock(olock)
                {
                    
    if(instance==null)
                    {
                        instance
    =new Singleton();
                    }
                    
    return instance;
                }
            }
        }
    }

     1         public static Singleton GetInstance()
     2         {
     3             if (singleton == null)
     4             {
     5                 lock (syncObject)
     6                 {
     7 
     8                     if (singleton == null)
     9                     {
    10                         singleton = new Singleton();
    11                     }
    12                 }
    13             }
    14             return singleton;
    15         } 
     1 namespace Singleton
     2 {
     3     public sealed class Singleton
     4     {
     5         private static readonly Singleton singleton = new Singleton();
     6 
     7         private Singleton()
     8         {
     9         }
    10 
    11         public static Singleton GetInstance()
    12         {
    13             return singleton;
    14         }
    15     }
    16 }



  • 相关阅读:
    Sql Server2000里面获得数据库里面所有的用户表名称 和对应表的列名称
    c#开发windows应用程序几个小技巧
    "Unexpected Error 0x8ffe2740 Occurred" Error Message When You Try to Start a Web Site
    注册asp.net到iis
    O/R Mapping 影射文件生成的一点研究(暂时放在这里,实现以后再进行总结)
    WMI使用集锦
    NHibernate快速指南
    项目打包以前需要删除的文件
    Asp.Net 学习资源列表
    精彩Blog
  • 原文地址:https://www.cnblogs.com/mikechang/p/1705077.html
Copyright © 2011-2022 走看看