单例模式
定义:保证一个类仅有一个实例,并提供一个访问它的全局访问点
单例模式总共有5种实现
- 饿汉模式
- 懒汉模式
- DCL(双重检查模式)----很重要
- 静态内部类单例模式
- 枚举类单例
(1)饿汉模式
public class Singleton{
private static Singleton instance = new Singleton();
private Singleton(){
}
public static Singleton getInstance(){
return instance;
}
}
- 在类加载时就完成初始化,所以类加载较慢,但获取对象的速度快
- 在类加载时就完成初始化,避免了多线程的问题
- 在类加载时就完成实例化,没有达到懒加载效果
- 如果从始至终都没有使用过这个实例,就会造成内存的浪费
(2)懒汉模式
线程不安全:
public class Singleton{
private static Singleton instance;
private Singleton(){
}
public static Singleton getInstance(){
if(instance == null){
instance = new Singleton();
}
return instance;
}
}
线程安全:
public class Singleton{
private static Singleton instance;
private Singleton(){
}
public static synchronized Singleton getInstance(){
if(instance == null){
instance = new Singleton();
}
return instance;
}
}
- 线程安全
- 但每次调用getInstance方法时都需要进行同步(造成不必要的同步开销,而且很多时候我们是用不到同步的)
- 不建议使用
(3)双重检查模式(DCL)
public class Singleton{
private static volatile Singleton instance;
private Singleton(){
}
public static Singleton getInstance() {
if(instance == null) {
synchronized(Singleton.class) {
if(instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
(4)静态内部类单例模式
public class Singleton{
private Singleton(){
}
public static Singleton getInstance getInstance() {
return SingletonHolder.sInstance;
}
private static class SingletonHolder {
private staic final Singleton sInstance = new Singleton();
}
}
第一次加载Singleton类时并不会初始化sInstance,只有第一次调用getInstance方法时虚拟机加载 SingletonHolder 并初始化 sInstance。这样不仅能确保线程安全,也能保证 Singleton 类的唯一性。
所以,推荐使用静态内部类单例模式。
(5)枚举单例
public enum Singleton {
INSTANCE;
public void doSomeThing() {
}
}
默认枚举实例的创建是线程安全的,并且在任何情况下都是单例。