public class MySingleton
{
private static object myLock = new object();
private static volatile MySingleton mySingleton = null;
private MySingleton()
{
}
public static MySingleton GetInstance()
{
if (mySingleton == null)
{ //第一次检查
lock (myLock)
{
if (mySingleton == null)
{ // 第二次检查
mySingleton = new MySingleton();
}
}
}
return mySingleton;
}
}