单例模式,双重校验的懒汉式

/**
 * 单例模式,双重校验的懒汉式
 */
public class bTestSingleton {
    public static void main(String[] args) {
        Singleton s1 = Singleton.getInstance();
        Singleton s2 = Singleton.getInstance();
        System.out.println(s1==s2);
    }
}


class Singleton{
    private  Singleton(){

    }

    private static Singleton instance = null;

    public static Singleton getInstance(){
        if(instance==null) {   //为了不让其他的做无谓的等待,不是空的话直接返回
            synchronized (Singleton.class) {  //线程安全
                if (instance == null) {       //是空的时候 创建
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

饿汉式的 (上来就创建好,很饿)

/**
 * 设计模式:设计模式是在大量的实践中总结和理论化之后优选的代码结构、编程风格、以及解决问题的思考方式。
 * 23种设计模式
 *
 * 单例的设计模式:
 * 1.解决的问题:使得一个类只能够创建一个对象。
 * 2.如何实现?见如下的4步
 */

//饿汉式的   (上来就创建好,很饿)
public class TestSingleton1 {
    public static void main(String[] args) {
        Singleton1 s1 = Singleton1.getInstance();
        Singleton1 s2 = Singleton1.getInstance();
        System.out.println(s1==s2);
    }
}

//只能创建mySingleton的单个实例
class Singleton1 {

    //1.私有化构造器,使得在类的外部不能够调用此构造器
    private Singleton1(){

    }
    //2.在类的内部创建一个类的实例
    private static Singleton1 instance = new Singleton1();
    //3.私有化此对象,通过公共的方法来调用
    //4.此公共的方法,只能通过类来调用,因此设置为static,同时类的实例也必须为static声明的
    public static Singleton1 getInstance(){
        return  instance;
    }
}

懒汉式 :可能存在线程安全

/**
 * 懒汉式 :可能存在线程安全
 */
public class TestSingleton2 {
    public static void main(String[] args) {
        Singleton2 s1 = Singleton2.getInstance();
        Singleton2 s2 = Singleton2.getInstance();
        System.out.println(s1==s2);
    }
}

class Singleton2{
    //1.
    private Singleton2(){

    }
    //2.
    private static  Singleton2 instance = null;
    //3.
    //4.
    public  static  Singleton2 getInstance(){
        if(instance==null){
            instance = new Singleton2();
        }
        return instance;
    }
}

posted @ 2019-10-04 17:38  云计算-李耀  阅读(619)  评论(0编辑  收藏  举报