Пример #1
0
 public static Single_A CreateInstance()
 {
     //双重if加lock模式
     if (single == null)
     {
         lock (_lock)
         {
             if (single == null)
             {
                 single = new Single_A();
             }
         }
     }
     return(single);
 }
Пример #2
0
 static void Main(string[] args)
 {
     //SingleDesign :单例模式 ,内存中只实例化一次
     for (int i = 0; i < 10; i++)
     {
         Task.Run(() =>
         {
             Single_A single_A = Single_A.CreateInstance();
             Single_B single_B = Single_B.CreateInstance();
             Single_C single_C = Single_C.CreateInstance();
             single_A.Show();
             single_B.Show();
             single_C.Show();
         });
     }
     Console.ReadKey();
 }