public class Singleton
    {
        private static Singleton _Singleton = null;
        private static object Singleton_Lock = new object();
        public static Singleton CreateInstance()
        {
            if (_Singleton == null) //双if +lock
            {
                lock (Singleton_Lock)
                {
                    Console.WriteLine("路过。");
                    if (_Singleton == null)
                    {
                        Console.WriteLine("被创建。");
                        _Singleton = new Singleton();
                    }
                }
            }
            return _Singleton;
        }
    }

 

相关文章:

  • 2021-07-31
  • 2021-12-23
  • 2022-12-23
  • 2021-10-31
  • 2021-10-29
  • 2021-09-19
猜你喜欢
  • 2021-12-21
  • 2021-09-14
  • 2022-03-07
  • 2021-08-07
  • 2021-12-05
  • 2021-10-05
相关资源
相似解决方案