static void Main(string[] args) { // 仅支持单线程 var instance = Singleton.GetInstance(); // 多线程,有额外的开销 var instance1 = Singleton1.GetInstance(); // Double check var instance2 = Singleton2.GetInstance(); }
public static Singleton2 GetInstance() { if (_unqiueInstance == null) { lock (locker) { if (_unqiueInstance == null) { _unqiueInstance = new Singleton2(); } } } return(_unqiueInstance); }