java 单例模式

发布时间:2022-04-10 23:33:50

1 饿汉式

package com.designmode.single;

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

	public void outMsg() {
		System.out.println("HungrySingle");
	}
}

package com.designmode.single;

/**
 * 双重检查
 * @author huhu
 *
 */
public class DbCheckSingle {
	private volatile static DbCheckSingle singleton;  
    private DbCheckSingle (){}  
    public static DbCheckSingle getSingleton() {  
    if (singleton == null) {  
        synchronized (DbCheckSingle.class) {  
            if (singleton == null) {  
                singleton = new DbCheckSingle();  
            }  
        }  
    }  
    return singleton;  
    }  
}


Card image cap
应用宝
Card image cap
APP STORE
Card image cap
应用宝
Card image cap
小米