静态内部类和双重校验锁 其实没必要记那么多的
- //双重校验锁,在当前的内存模型中无效
- class LockSingleton{
- private volatile static LockSingleton singleton;
- private LockSingleton(){}
- public static LockSingleton getInstance(){
- if(singleton==null){
- synchronized(LockSingleton.class){
- if(singleton==null){
- singleton=new LockSingleton();
- }
- }
- }
- return singleton;
- }
-
- }
复制代码 |