写一个SingleTon,(饿最终、懒同步)

1.饿汉式:

public class SingleTon {
    
    private SingleTon(){
        
    }
    
    private final static SingleTon instance = new SingleTon();
    
    public static SingleTon getInstance(){
        return instance;
    }
}

2.懒汉式:

public class SingleTon {
    
    private SingleTon(){
        
    }
    
    private static SingleTon instance = null;
    
    public static synchronized SingleTon getInstance(){
        if(instance == null)
            instance = new SingleTon();
        return instance;
    }
}

3.枚举:

public enum SingleTon {
    instance;
}

 

posted @ 2017-03-13 11:46  如果屈原会编程  阅读(223)  评论(0编辑  收藏  举报