现在的位置: 首页 > 综合 > 正文

单例模式(线程安全)

2012年06月30日 ⁄ 综合 ⁄ 共 551字 ⁄ 字号 评论关闭
public class Singleton
{
private static Singleton _instance = null;
private Singleton(){}
public static Singleton CreateInstance()
{
if(_instance == null)

{
_instance = new Singleton();
}
return _instance;
}
}


public class Singleton
{
private volatile static Singleton _instance = null;
private static readonly object lockHelper = new object();
private Singleton(){}
public static Singleton CreateInstance()
{
if(_instance == null)
{
lock(lockHelper)
{
if(_instance == null)
_instance = new Singleton();
}
}
return _instance;
}
}


public class Singleton
{

private Singleton(){}
public static readonly Singleton instance = new Singleton();
}

 

抱歉!评论已关闭.