单例模式的两种方法

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;

}

}

posted @ 2014-03-01 10:21  天藐水瓶  阅读(143)  评论(0编辑  收藏  举报