饿汉式单例:
立刻加载
不存在线程安全问题,但是它一般不被使用,因为它会浪费空间
/**
* Description:饿汉式单例:先创建好对象,放在那里
* Date: 2022/7/23 11:23
**/
public class Hungry {
// 构造器私有,防止外部调用
private Hungry(){
}
// 可能会浪费空间,因为这个对象已经存在了,但是可能还未使用
private final static Hungry HUNGRY=new Hungry();
public static Hungry getInstance(){
return HUNGRY;
}
}
懒汉式单例
延时加载
能合理使用内存空间,但是这种方式存在线程安全问题
双重检查锁方式
/**
* Description:懒汉式单例:使用到该对象时再创建
* Date: 2022/7/23 11:27
**/
public class LazyMan {
// 构造器私有
private LazyMan(){
}
private volatile static LazyMan lazyMan;
// 双重检查锁模式 懒汉式单例 DCL懒汉式
public static LazyMan getInstance(){
if(lazyMan==null){
synchronized (LazyMan.class){
if(lazyMan==null){
lazyMan=new LazyMan();//不是一个原子性操作
/**
*创建一个对象时会有三步操作:
* 1.分配内存空间
* 2.执行构造方式,初始化对象
* 3.把对象指向这个空间
*
* 在操作时可能会指令重排 执行132
* 在并发情况下,如果一个线程执行了13 还未执行2 ,而另一个线程在获取这个对象时会获取到一个还未初始化的对象,就会产生问题
* 所以要使用volatile来防止指令重排
*/
}
}
}
return lazyMan;
}
}
反射可以破坏单例模式:
比如:
public static void main(String[] args) throws Exception {
LazyMan instance2=LazyMan.getInstance();
// 通过反射获取它的无参构造器
Constructor<LazyMan> declaredConstructor = LazyMan.class.getDeclaredConstructor(null);
// 调用setAccessible设置为true就可以使用private修饰的构造器了
declaredConstructor.setAccessible(true);
// 通过反射再次创建一个实例
LazyMan instance1 = declaredConstructor.newInstance();
System.out.println(instance2);
System.out.println(instance1);
}
结果发现生成了两个实例:
这种方法,可以通过在构造方法中进行判断来解决:
// 构造器私有
private LazyMan(){
synchronized (LazyMan.class){
if(lazyMan!=null){
throw new RuntimeException("不要试图使用反射去破坏单例模式");
}
}
}
但是,还是会有问题,
如果使用下面这种写法,还是会产生两个实例:
通过反射来创建两个实例
public static void main(String[] args) throws Exception {
// 通过反射获取它的无参构造器
Constructor<LazyMan> declaredConstructor = LazyMan.class.getDeclaredConstructor(null);
// 调用setAccessible设置为true就可以使用private的构造器了
declaredConstructor.setAccessible(true);
LazyMan instance1 = declaredConstructor.newInstance();
LazyMan instance2 = declaredConstructor.newInstance();
System.out.println(instance2);
System.out.println(instance1);
}
结果发现能成功创建
可以加一个标志位来解决这个方法:
// 使用一个关键字
private static boolean flag=false;
// 构造器私有
private LazyMan(){
synchronized (LazyMan.class){
if(flag==false){
flag=true;
}else {
throw new RuntimeException("不要试图使用反射去破坏单例模式");
}
}
}
但是我们是可以通过反射获取到我们定义的这个关键字,然后在创建实例时修改这个关键字的状态,依旧可以创建多个实例
public static void main(String[] args) throws Exception {
// 通过反射获取到这个关键字
Field flag=LazyMan.class.getDeclaredField("flag");
flag.setAccessible(true);
// 通过反射获取它的无参构造器
Constructor<LazyMan> declaredConstructor = LazyMan.class.getDeclaredConstructor(null);
// 调用setAccessible设置为true就可以使用private的构造器了
declaredConstructor.setAccessible(true);
LazyMan instance1 = declaredConstructor.newInstance();
//在创建过一个实例后,使用反射改变这个关键字的状态
flag.set(instance1,false);
LazyMan instance2 = declaredConstructor.newInstance();
System.out.println(instance2);
System.out.println(instance1);
}
结果如下:
所以,我们可以通过枚举来创建单列模式:
静态内部类实现单例
/**
* Description:静态内部类
* Date: 2022/7/23 11:38
**/
public class Holder {
private Holder(){
}
public static Holder getInstance(){
return InnerClass.HOLDER;
}
public static class InnerClass{
private static final Holder HOLDER=new Holder();
}
}
枚举实现单例模式
使用枚举实现单例可以防止单例模式被反射破坏
//enum是什么?枚举本身也是一个class类,反射不能破坏枚举
public enum EnumSingle {
INSTANCE;
public EnumSingle getInstance(){
return INSTANCE;
}
}
原因:
在构造器类中newInstance方法中有一个判断:如果是枚举类型就会抛出异常
测试如下:
class Test{
public static void main(String[] args) throws Exception {
EnumSingle instance1=EnumSingle.INSTANCE;
Constructor<EnumSingle> declaredConstructor = EnumSingle.class.getDeclaredConstructor(String.class,int.class);
declaredConstructor.setAccessible(true);
EnumSingle instance2= declaredConstructor.newInstance();
System.out.println(instance1);
System.out.println(instance2);
}