//懒汉式单例设计模式并发访问
class Single{
private static Single single = null;
private Single(){}
public static Single getInstamce(){
if(single == null){
synchronized(Single.class){
if(single == null){
single = new Single();
}
}
}
return single;
}
}
class Demo implements Runnable{
@Override
public void run() {
Single.getInstamce();
}
}
这是延迟加载模式的并发访问,通过同步锁解决了安全问题;通过双if判断解决了效率问题 |
|