public class Singleton { private static Singleton _instance; private static readonly object syn = new object(); private Singleton() //构造函数设置private,不能被new,单例模式 { } public static Singleton CreateInstance() { if (_instance == null) { lock (syn) //加锁防止多线程 { if (_instance == null) { _instance = new Singleton(); } } } return _instance; } }
时间: 2024-10-23 11:10:34