最近在工作的时候发生了一些多执行续配合CONSUL的问题
解了以后发现singleton的重要


点击(此处)折叠或打开

  1. static void Main(string[] args)
  2.         {

  3.             Task.Run(() => Console.WriteLine(ConfigStore.Amigo.CategoryIDs));
  4.             Task.Run(() => Console.WriteLine(ConfigStore.Common.ApiPath2));
  5.             Task.Run(() => Console.WriteLine(ConfigStore.Common.ApiPathByB2B2));
  6.             Task.Run(() => Console.WriteLine(ConfigStore.Common.AuthAPIName));
  7.             Task.Run(() => Console.WriteLine(ConfigStore.Common.CreateOrderToken));
  8.             Task.Run(() => Console.WriteLine(ConfigStore.Common.JWTSecret));
  9.             Task.Run(() => Console.WriteLine(ConfigStore.Common.LdapPath));
  10.             Task.Run(() => Console.WriteLine(ConfigStore.Common.IsTestCase));
  11.             Console.Read();
  12.     }

以这段CODE来模拟同时间呼叫 原本的写法在建立的时候是这样做
CONSUL问题修正(SINGLETON)-LMLPHP

点击(此处)折叠或打开

  1. Console.WriteLine("中間");
  2.                     if (signalled = mutex.WaitOne(2000))
  3.                     {
  4.                         Console.WriteLine("裡面");
  5.                         if (instance == null)
  6.                         {
  7.                             Console.WriteLine("很裡面");
  8.                             instance = new ConsulConfigStore();
  9.                         }
  10.                     }

发生了两秒过去了会让NULL的物件继续往前走
所以改回了这样的写法

点击(此处)折叠或打开

  1. Console.WriteLine("中間");
  2.                     if (instance == null)
  3.                     {
  4.                         lock (o)
  5.                         {
  6.                             Console.WriteLine("裡面");
  7.                             if (instance == null)
  8.                             {
  9.                                 Console.WriteLine("很裡面");
  10.                                 instance = new ConsulConfigStore();
  11.                             }
  12.                         }

  13.                     }
回归用Lock搞定
纪录纪录

12-16 18:10