单例模式创建的两种方法

package com.wetuo.javabase;
public class SingletonDemo {
    private static SingletonDemo instance = new SingletonDemo();
    private SingletonDemo() {}
      
    public static SingletonDemo getInstance() {
        return instance;
    }//充血模式
      
}
方法二
public class SingletonDemo {
    private static SingletonDemo instance = null;
    private SingletonDemo() {}
      
    public static synchronized SingletonDemo getInstance() {//保证只new一次
        if(null==instance)
            return new SingletonDemo();
        else
            return instance;
    }//饥饿模式
      
}


你可能感兴趣的:(单例模式,饥饿模式‘充血模式)