前言:有很多时候,在一个生命周期中我们只要一个对象就可以了,比如:线程池,缓存,对话框,日志,显卡驱动等等。如果造出多个实例,就会导致许多问题产生,例如:程序的行为异常、资源使用过量,或者说不一致的结果。

public class Singleton
    {
        private static Singleton instance;
        private Object _synchronizedObj = new Object();

        //private constructor
        private Singleton() { }
       

        public Singleton GetInstance
        {
            get
            {
                if (instance == null)
                    lock (_synchronizedObj)
                    {
                        if (instance == null)
                            instance = new Singleton();
                    }
                return instance;
            }
        }
    }
View Code

相关文章:

  • 2021-10-17
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-09
  • 2022-01-15
  • 2021-06-15
  • 2021-12-09
  • 2021-12-07
  • 2021-11-23
相关资源
相似解决方案