html 手机网站开发,相册管理网站模板下载失败,曹鹏wordpress,住房与城乡建设部网站打不开单例模式#xff1a;保证某个类在程序中只存在唯⼀⼀份实例#xff0c;而不会创建出多个实例#xff0c;单例模式的类一般是构造器私有#xff0c;通过一个方法返回唯一实例#xff1b;
点这里查看线程安全的详细讲解#xff1b;
常见的单例模式分为饿汉式和懒汉式
一…单例模式保证某个类在程序中只存在唯⼀⼀份实例而不会创建出多个实例单例模式的类一般是构造器私有通过一个方法返回唯一实例
点这里查看线程安全的详细讲解
常见的单例模式分为饿汉式和懒汉式
一、饿汉式
饿汉式会在类加载的时候创建对象并初始化
public class Singleton {private static final Singleton instance new Singleton();private Singleton() {}public static Singleton getInstance() {return instance;}
}以上是一个饿汉式实现的单例模式的典型代码由代码可以看出 在类加载的时候对象已经创建好了也就是不管你需不需要使用都已经存在了由 getInstance 方法返回这个对象getInstance 方法直接 return只涉及到读操作不涉及写操作因此饿汉式是线程安全的
二、懒汉式
懒汉式在类加载的时候并不会直接创建出实例而是在第一次使用的时候才会创建
public class Singleton {private static Singleton instance null;private Singleton() { }public static Singleton getInstance() {if (instance null) {instance new Singleton();}return instance;}
}
以上代码是懒汉式实现的单例模式的典型代码其中 刚开始的时候instance 对象并没有实例化在使用 getInstance 方法获取该对象时会判断该对象是否为空为空才会初始化也就是第一次使用的时候为空之后使用就会直接返回该对象但是 getInstance 方法既存在读操作也存在写操作 instance new Singleton(); 那么在多线程的情况下是否会存在线程安全问题呢答案是肯定的试想如果两个线程同时执行到 if 判断此时 instance 为空两个线程都会进入 if 语句内这样两个线程就会各自创建两个对象并返回这就违背了单例模式的初衷
那么如何解决这个问题呢
优化一
可以使用 synchronized 加锁由于两个线程不应该同时判断出 instance null故可以对整个 if 块使用 synchronized 进行加锁于是代码就变为
public class Singleton {private static Singleton instance null;private Singleton() { }public static Singleton getInstance() {synchronized (Singleton.class) {if (instance null) {instance new Singleton();}}return instance;}
}这样一来在多线程的情况下当一个线程进入到 if 块内其他线程就会阻塞等待等待出了synchronized 块之后instance 实例也就 new 完了其他线程再进行判断 instance 就不为 null 了但是这样一来之后的每次调用 getInstance 方法都会进行加锁释放锁等操作这样系统开销就非常大影响效率而我们只需要在第一次创建实例的时候加锁因此即为了保证线程安全又要保证效率就得对上述代码进一步优化
优化二
由于我们只需要在第一次创建实例的时候才加锁因此可以在 synchronized 外面再包装一层 if 判断于是代码进一步变为
public class Singleton {private static Singleton instance null;private Singleton() { }public static Singleton getInstance() {if(instance null) {synchronized (Singleton.class) {if (instance null) {instance new Singleton();}}}return instance;}
}
这样一来既保证了线程安全又不会非常影响效率但是上述代码还存在一个问题指令重排序问题在 new Singleton() 实例的时候new 操作可以被拆分为三步
1申请内存空间
2在内存空间上构造对象
3把内存地址赋值给实例引用
编译器为了执行效率会优化这三步的顺序但是 1 肯定是最先执行的因此 new 操作可能的执行顺序为 1 - 2 - 31 - 3 - 2当执行顺序为后者的时候假设有两个线程 t1t2在 t1 执行完 1, 3 还来不及执行 2 的时候此时 t2 线程执行到 if 判断此时由于 t1 线程执行了步骤 3 所以 t2 判断 if 不为 null就直接返回 instance 对象了但此时 instance 指向的是一个还没有初始化的非法对象因此 t2 线程的后续代码访问 instance 里面的属性和方法时就会出错为了避免这种情况需要对上述代码再进行优化
优化三
使用 volatile 关键字告诉编译器不要优化指令重排序
public class Singleton {private static volatile Singleton instance null;private Singleton() { }public static Singleton getInstance() {if(instance null) {synchronized (Singleton.class) {if (instance null) {instance new Singleton();}}}return instance;}
}
至此线程安全的懒汉式就实现了