不支持非公共的无参构造函数的多线程
public abstract class BaseInstance<T> where T : class,new() { private readonly static object lockObj = new object(); private static T instance = null; public static T Instance { get { if (instance == null) { lock (lockObj) { if (instance == null) { instance = new T(); } } } return instance; } } }
支持非公共的无参构造函数的函数
public class BaseInstance<T> where T : class//new(),new不支持非公共的无参构造函数 { /* * 单线程测试经过! * 多线程测试经过! * 根据须要在调用的时候才实例化单例类! */ private static T _instance; private static readonly object SyncObject = new object(); public static T Instance { get { if (_instance == null)//没有第一重 singleton == null 的话,每一次有线程进入 GetInstance()时,均会执行锁定操做来实现线程同步, //很是耗费性能 增长第一重singleton ==null 成立时的状况下执行一次锁定以实现线程同步 { lock (SyncObject) { if (_instance == null)//Double-Check Locking 双重检查锁定 { //_instance = new T(); //须要非公共的无参构造函数,不能使用new T() ,new不支持非公共的无参构造函数 _instance = (T)Activator.CreateInstance(typeof(T), true); //第二个参数防止异常:“没有为该对象定义无参数的构造函数。” } } } return _instance; } } public static void SetInstance(T value) { _instance = value; } }