在类加载时就初始化创建对象,是线程安全的,不管何时使用都会创建,可能会浪费内存,所以称之为饿汉。
public class HungrySingleton {
private static HungrySingleton instance = new HungrySingleton();
private HungrySingleton(){}
public static HungrySingleton getInstance(){
return instance;
}
}
在被外部调用时才会加载创建,是线程不安全的,加锁保证线程安全但效率变低。通俗的来讲,别人叫它去干活它才去,所以称之为懒汉。
public class LazySingleton{
private static LazySingleton instance;
private LazySingleton(){}
public static LazySingleton getInstance(){
if (instance == null)
instance = new LazySingleton();
return instance;
}
}
使用volatile和多重检查,保证线程安全
public class LazyDoubleCheckSingleton{
private volatile static LazyDoubleCheckSingleton instance;
private LazyDoubleCheckSingleton(){}
public static LazyDoubleCheckSingleton getInstance(){
if (instance == null) {
synchronized (LazyDoubleCheckSingleton.class) {
if (instance == null)
instance = new LazyDoubleCheckSingleton();
}
}
return instance;
}
}
在静态内部类中初始化创建对象,解决内存浪费问题和线程安全问题。
public class StaticClassSingleton{
private StaticClassSingleton(){}
public static StaticClassSingleton getInstance(){
return StaticClass,instance;
}
private static class StaticClass {
private static StaticClassSingleton instance = new StaticClassSingleton();
}
}
优点
缺点
原文:https://www.cnblogs.com/Faink/p/13325017.html