标签:单例 syn stat public else ref logs pre single
详细讲解请看:http://www.cnblogs.com/cielosun/p/6582333.html
饿汉模式:
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton(){}
public static Singleton getInstance(){
return instance;
}
}
懒汉模式:
public class Singleton {
private static Singleton instance;
private Singleton() {
}
public static Singleton getInstance() {
if (instance == null)
return new Singleton();
else return instance;
}
}
加锁的的单例:
public class Singleton {
private static Singleton instance;
private Singleton() {
}
public static synchronized Singleton getInstance() {
if (instance == null)
return new Singleton();
else return instance;
}
}
标签:单例 syn stat public else ref logs pre single
原文地址:http://www.cnblogs.com/wangnuo/p/7753650.html