单例设计模式
懒汉式 特点实例的延迟加载,多线程访问时会出现安全问题,加同步时锁是该类所属的字节码文件对象
class single
{
private static single s=null;
private single(){}
public static single getInstance()
{
if(s==null)
{
synchoronized(single.class)
{
if(s==null)
s=new single();
}
}
return s;
}
饿汉式
class single
{
private static final single s=new single();
private single(){}
public static single getInstance()
{
return s;
}
} |