单例模式设计,是采取一定的方法,使得在整个软件系统中,对某个类只能存在一个对象实例,并且这个类只提供一个取得其对象实例的方法。
单例模式注意事项和细节说明
- 单例模式保证了系统内存中该类只存在一个对象,节省了系统资源,对于一些需要频繁创建销毁的对象,使用单例模式可以提高系统的性能。
- 当我们想实例化一个单例类的时候,必须要记住使用相应的获取对象的方法,而不是使用new
单例模式使用的场景:需要频繁的进行创建和销毁的对象,创建对象时消耗时间过多或者耗费资源过多,但又经常用到的对象、工具类对象,频繁访问数据库或文件的对象(比如数据源,session工厂等)。
单例模式有八种方式:
- 饿汉式(静态常量)
- 饿汉式(静态代码块)
- 懒汉式(线程不安全)
- 懒汉式(线程安全,同步方法)
- 懒汉式(线程安全,同步代码块)
- 双重检查
- 静态内部类
- 枚举
饿汉式(不推荐使用)
饿汉式(静态常量)实例:
- 构造器私有化
- 类内部创建对象
- 向外暴露一个静态的公有方法,返回实例
饿汉式(静态代码块):class Singleton{
//构造器私有化
private Singleton(){}
//在类内部创建对象实例
private final static Singleton instance = new Singleton();
//提供一个公有的静态方法,返回实例对象
public static Singleton getInstance(){
return instance;
}
}
public class SingletonTest01 {
public static void main(String[] args) {
Singleton instance = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance == instance2); //true
System.out.println(instance.hashCode() == instance2.hashCode()); //true
}
}
class Singleton{
//构造器私有化
private Singleton(){}
private static Singleton instance;
//在类内部创建对象实例
static {
instance = new Singleton();
}
//提供一个公有的静态方法,返回实例对象
public static Singleton getInstance(){
return instance;
}
}
分析:这种两种写法简单,在类加载的时候就完成了实例化,避免了线程同步问题。但是没有实现lazy loading的效果,可能造成内存浪费。
懒汉式(不推荐使用)
懒汉式(线程不安全):
class Singleton{
//构造器私有化
private Singleton(){}
private static Singleton instance;
// 提供一个公有的静态方法
// 只有在使用到该方法的时候,才去创建instace
public static Singleton getInstance(){
if(instance == null){
instance = new Singleton();
}
return instance;
}
}
分析:起到了lazy loading的效果,但是只能在单线程下使用。如果在多线程的时候,一个线程进入了**if(singleton == null)**
判断语句块,还没有来的及执行,其他的线程也通过了这个判断语句,就会产生多个实例。所以,开发中不要用这种方式。
懒汉式(线程安全,同步方法或同步代码块):
使用同步方法或者同步代码块,解决了线程安全问题,但是每个线程在想要获得类的实例的时候,执行getInstance()
方法都要进行同步,效率太低。所以也不推荐使用。
//使用同步方法
public static synchronized Singleton getInstance(){
if(instance == null){
instance = new Singleton();
}
return instance;
}
public static Singleton getInstance(){
if(instance == null){
//使用同步代码块
synchronized(Singleton.class){
instance = new Singleton();
}
instance = new Singleton();
}
return instance;
}
双重检查
//双重检查
class Singleton{
private static volatile Singleton instance;
private Singleton(){}
//提供了一个静态的公有方法,加入双重检查代码,解决线程安全问题,同时解决懒加载问题。
//保证了效率,推荐使用
public static synchronized Singleton getInstance(){
if(instance == null){
synchronized(Singleton.class){
if(instance == null){
instance = new Singleton();
}
}
}
return instance;
}
}
分析:进行了两次getInstance()
检查,可以保证线程安全。实例化代码只用执行一次,后面再进行访问的时候,直接return实例化对象,也避免了反复进行方法同步。线程安全,延迟加载,效率较高,推荐使用。
静态内部类
class Singleton{
private static volatile Singleton instance;
private Singleton(){}
//写一个静态内部类,该类中有一个静态属性Singleton
private static class SingletonInstance{
private static final Singleton INSTANCE = new Singleton();
}
//提供一个静态的公有方法,直接返回SingletonInstance.INSTANCE
public static synchronized Singleton getInstance(){
return SingletonInstance.INSTANCE;
}
}
分析:
- 静态内部类方式在Singleton类被装载时并不会立即实例化,而是在需要实例化的时候,调用getInstance方法,才会装载SingletonInstance类,从而完成Singleton的实例化。
- 这种方式采用了类装载机制来保证初始化实例时只有一个线程,避免了线程不安全。推荐使用。
枚举
enum Singleton{
INSTANCE; //属性
public void sayOK(){
System.out.println("OK");
}
}
public class SingletonTest01 {
public static void main(String[] args) {
Singleton instance = Singleton.INSTANCE;
Singleton instance2 = Singleton.INSTANCE;
System.out.println(instance == instance2); //true
System.out.println(instance.hashCode() == instance2.hashCode()); //true
instance.sayOK();
}
}
分析:借助枚举来实现单例模式,不仅能避免多线程同步问题,而且还能防止反序列化重新创建新的对象。推荐使用。