Example #1
0
 /// <summary>
 /// 获取单例实例
 /// 经典的单例模式 不是线程安全的 存在并发的问题
 /// </summary>
 /// <returns></returns>
 public static ClassicSingleton GetInstance()
 {
     if (_instance == null)
     {
         _instance = new ClassicSingleton();
     }
     return(_instance);
 }
Example #2
0
 /// <summary>
 /// 获取单例实例
 /// 经典的单例模式 不是线程安全的 存在并发的问题
 /// </summary>
 /// <returns></returns>
 public static ClassicSingleton GetInstance()
 {
     if (_instance == null)
     {
         _instance = new ClassicSingleton();
     }
     return _instance;
 }
Example #3
0
        static void Main(string[] args)
        {
            ClassicSingleton classicSingleton1 = ClassicSingleton.GetInstance();
            ClassicSingleton classicSingleton2 = ClassicSingleton.GetInstance();

            Console.WriteLine(classicSingleton1 == classicSingleton2);

            SingletonV1 singletonV1_1 = SingletonV1.GetInstance();
            SingletonV1 singletonV1_2 = SingletonV1.GetInstance();

            Console.WriteLine(singletonV1_1 == singletonV1_2);

            SingletonV2 singletonV2_1 = SingletonV2.GetInstance();
            SingletonV2 singletonV2_2 = SingletonV2.GetInstance();

            Console.WriteLine(singletonV2_1 == singletonV2_2);


            Console.ReadLine();
        }