java单例模式-懒汉模式演进总结

 本文是2018年写的,总结了市面上java单例模式之懒汉模式的所有写法。实际多线程场景中,前面的一些实现是存在问题的。

public class SingleTon1 {
   private static SingleTon1 instance;
   public static SingleTon1 getInstance() {
        if (instance == null) {
            instance = new SingleTon1();
        }
        return instance;
    }
  private SingleTon1() {}
}
public class SingleTon2 {
    private static SingleTon2 instance;
    synchronized public static SingleTon2 getInstance() {
        if (instance == null) {
            instance = new SingleTon2();
        }
        return instance;
    }
    private SingleTon2() {    }
}
public class SingleTon3 {
    private static SingleTon3 instance;
    public static SingleTon3 getInstance() {
        synchronized(SingleTon3.class){
            if (instance == null) {
                instance = new SingleTon3();
            }
            return instance;
        }
    }
    private SingleTon3() {    }
}
public class SingleTon4 {
    private static SingleTon4 instance;
    public static SingleTon4 getInstance() {
        if (instance == null) {
            synchronized (SingleTon4.class) {
                instance = new SingleTon4();
            }
        }
        return instance;
    }
   private SingleTon4() {   }
}
public class SingleTon5 {
    private static SingleTon5 instance;
    public static SingleTon5 getInstance() {
        if (instance == null) {
           synchronized (SingleTon5.class) {
                if(instance == null)
                    instance = new SingleTon5();
            }
        }
        return instance;
    }
   private SingleTon5() {   }
}
public class SingleTon6 {
    private volatile static SingleTon6 instance;
    public static SingleTon6 getInstance() {
        if (instance == null) {
            synchronized (SingleTon6.class) {
                if(instance == null)
                    instance = new SingleTon6();
            }
        }
        return instance;
    }
    private SingleTon6() {    }
}
//增加了内部类
public class SingleTon7 {
    public static SingleTon7 getInstance() {
        return SingleTonEnum.SingleTon7.getInstance();
    }
    private SingleTon7() {    }
    enum SingleTonEnum {
        SingleTon7;
        private SingleTon7 instance;
        private SingleTonEnum() {
            instance = new SingleTon7();
        }       
        public SingleTon7 getInstance() {    
            return instance;     
        }    
    }
}
//增加了内部类
public class SingleTon8 {
    public static SingleTon8 getInstance() {
        return SingleTon8Holder.instance;
    }
    private SingleTon8() {    }
    private static class SingleTon8Holder{
        private static final SingleTon8 instance = new SingleTon8();    
    }
}
//不是真正的单例
public class SingleTon9 {
    public static SingleTon9 getInstance() {
        return instanceThreadLocal.get();
    }
    private SingleTon9() {    }
    private static final ThreadLocal instanceThreadLocal = 
         ThreadLocal.withInitial(() -> new SingleTon9());
}

 

你可能感兴趣的:(java)