//懒汉式(延迟加载的单例设计模式示例)
class DanLi
{
private DanLi danLi=null;
private DanLi()
{
}
public static DanLi getDanLi()
{
if(danLi=null)
{
synchronized(DanLi.class)
{
/*if(danLi=null)
{
danLi=new DanLi();
}
*/
danLi=new DanLi();//这边好像不需要if语句判断也可以啊!只不过多线程加载时,可能会出现danLi多次被付值的情况,但是好像影响不大啊??
}
}
return danLi;
}
} |