Esempio n. 1
0
        static void Main(string[] args)
        {
            //Stopwatch stopwatch = new Stopwatch();
            //stopwatch.Start();

            //stopwatch.Stop();
            //Console.WriteLine(stopwatch.ElapsedMilliseconds);

            单例类 单例 = 单例类.创建单例模式();

            单例.日志();
            单例.日志();
            单例.日志();
            单例.日志();

            Console.ReadKey();
        }
Esempio n. 2
0
 /// <summary>
 /// 静态方法获取对象实例
 /// </summary>
 /// <returns></returns>
 public static 单例类 创建单例模式()
 {
     //检测singletonOne是否为空,保证第一次创建后,不再等待锁
     if (singleton == null)
     {
         //加锁
         lock (Lock)
         {
             //Thread.Sleep(1000);
             //二次检测singletonOne是否为空,保证单例
             if (singleton == null)
             {
                 singleton = new 单例类();
             }
         }
     }
     单例类.CreateCount++;
     Console.WriteLine("单例模式第{0}次执行创建单例模式", 单例类.CreateCount);
     return(singleton);
 }