class Single
{
private static Single s=null;
private Single(){}
public static Single getInstance()
{
if(s==null)
{
synchronized(Single.class)
{
if(s==null)
s=new Single();
}
}
return s;
}
}
class SingleDemo
{
public static void main(String[] args)
{
Single s1=Single.getInstance();
}
}
双重判断为什么能增加代码的运行效率?
虽然减少了锁的判断,但是也增加了if的判断啊
|
|