1.懒汉式
懒汉式的特点是实例的延迟加载,但是多线程访问时会出现安全问题,加同步时锁是该类所属的字节码文件对象
[javascript] view plaincopy在CODE上查看代码片派生到我的代码片
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;
}
2.饿汉式
[javascript] view plaincopy在CODE上查看代码片派生到我的代码片
class single
{
private static final single s=new single();
private single(){}
public static single getInstance()
{
return s;
}
}
|