单例模式(懒汉)(一)

/**
 * 单例模式(懒汉)线程不安全
 */

public class Single {

    private static Single single=null;

    private Single(){

    }

    public static Single getInstance(){
        if (single == null){
            single=new Single();
        }
        return single;
    }

测试是否为单例

public static void main(String[] args) {
        Single single1 = Single.getInstance();
        Single single2 = Single.getInstance();
        System.out.println(single1 == single2);
 }

 

posted @ 2019-06-20 18:34  司徒二条  阅读(135)  评论(0编辑  收藏  举报