单例模式

#懒汉模式引入

package single.lazysingleton;

public class LazySingletonTest {
    public static void main(String[] args) {
        LazySingleton instance = LazySingleton.getInstance();
        LazySingleton instance1 = LazySingleton.getInstance();
        System.out.println(instance == instance1);
    }
}

class LazySingleton{
    private static LazySingleton instance;
    private  LazySingleton(){

    }
    public static LazySingleton getInstance(){
        if(instance == null){
            instance = new LazySingleton();
        }
        return instance;
    }
}

上述代码在单线程模式下输出结果为true。

下面开两个线程测试多线程情况,sleep

package single.lazysingleton;

public class LazySingletonTest {
    public static void main(String[] args) {
        new Thread(()->{
            LazySingleton instance = LazySingleton.getInstance();
            System.out.println(instance);
        }).start();
        new Thread(()->{
            LazySingleton instance = LazySingleton.getInstance();
            System.out.println(instance);
        }).start();
    }
}

class LazySingleton{
    private static LazySingleton instance;
    private  LazySingleton(){

    }
    public static LazySingleton getInstance(){
        if(instance == null){
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            instance = new LazySingleton();
        }
        return instance;
    }
}

上述代码输出不一样的地址值,通过将getinstance方法加S锁来保证单例,但加S锁损耗性能。

package single.lazysingleton;

public class LazySingletonTest {
    public static void main(String[] args) {
        new Thread(()->{
            LazySingleton instance = LazySingleton.getInstance();
            System.out.println(instance);
        }).start();
        new Thread(()->{
            LazySingleton instance = LazySingleton.getInstance();
            System.out.println(instance);
        }).start();
    }
}

class LazySingleton{
    private static LazySingleton instance;
    private  LazySingleton(){

    }
    public synchronized static LazySingleton getInstance(){
        if(instance == null){
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            instance = new LazySingleton();
        }
        return instance;
    }
}

优化,当instance不为空的时候再加锁,避免了每次都加锁判断,资源损耗

package single.lazysingleton;

public class LazySingletonTest {
    public static void main(String[] args) {
        new Thread(()->{
            LazySingleton instance = LazySingleton.getInstance();
            System.out.println(instance);
        }).start();
        new Thread(()->{
            LazySingleton instance = LazySingleton.getInstance();
            System.out.println(instance);
        }).start();
    }
}

class LazySingleton{
    private static LazySingleton instance;
    private  LazySingleton(){

    }
    public  static LazySingleton getInstance(){
        if(instance == null){
           synchronized (LazySingleton.class){
               if(instance == null){//防止一开始一开始null两个线程都走到了代码块里面,防止多例
                   instance = new LazySingleton();
               }
           }
        }
        return instance;
    }
}

但在字节码层面,防止指令重排javap -v XXX.class,(没有初始化就赋值了)

package single.lazysingleton;

public class LazySingletonTest {
    public static void main(String[] args) {
        new Thread(()->{
            LazySingleton instance = LazySingleton.getInstance();
            System.out.println(instance);
        }).start();
        new Thread(()->{
            LazySingleton instance = LazySingleton.getInstance();
            System.out.println(instance);
        }).start();
    }
}

class LazySingleton{
    private volatile static LazySingleton instance;
    private  LazySingleton(){

    }
    public  static LazySingleton getInstance(){
        if(instance == null){
           synchronized (LazySingleton.class){
               if(instance == null){//防止一开始两个线程都走到了代码块里面,防止多例
                   instance = new LazySingleton();
                   //字节码层面
                   //JIT CPU
                   //1.分配空间
                   //2.初始化
                   //3.引用赋值
               }
           }
        }
        return instance;
    }
}

1.懒汉模式:延迟加载,只有在真正使用的时候, 才开始实例化。

1)线程安全问题

2) double check加锁优化

3)编译器(JIT),CPU 有可能对指令进行重排序,导致使用到尚末初始化的实例,可以通过添加volatile 关键字进行修饰,对于volatile修饰的字段,可以防止指令重排

#饿汉模式引入

package singleton.hungrysingleton;

public class HungrySingleTest {
    public static void main(String[] args) {

        HungrySingleton instance = HungrySingleton.getInstance();
        HungrySingleton instance1 = HungrySingleton.getInstance();
        System.out.println(instance == instance1);
    }

}
//饿汉模式
class HungrySingleton{
    private static HungrySingleton instance =new HungrySingleton();
    private HungrySingleton(){

    }
    public static HungrySingleton getInstance(){

        return instance;
    }
}

2.饿汉模式: 


类加载的初始化阶段就完成了实例的初始化。本质上就是借助于jvm类加载机制,保证实例的唯一性。
类加载过程:
1,加载二进制数据到内存中,生成对应的Class数据结构
2,连接: a.验证,b.准备(给类的静态成员变量赋默认值),c.解析
3,初始化:给类的静态变量赋初值
只有在真正使用对应的类时,才会触发初始化如(当前类是启动类即main函数所在类,直接进行new 操作,访问静态属性、访问静态方法,用反射访问类,初始化-个类的子类等.)

3.静态内部类

1).本质上是利用类的加载机制来保证线程安全

2).只有在实际使用的时候,才会触发类的初始化,所以也是懒加载的-种形式。

package singleton.innerclasssingleton;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class InnerClassSingletonTest {
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {

        /*InnerClassSingleton instance = InnerClassSingleton.getInstance();
        InnerClassSingleton instance1 = InnerClassSingleton.getInstance();
        System.out.println(instance == instance1);*/
        //测试多线程
       /* new Thread( ()->{
            InnerClassSingleton instance = InnerClassSingleton.getInstance();
            System.out.println(instance);
        }).start();
        new Thread( ()->{
            InnerClassSingleton instance = InnerClassSingleton.getInstance();
            System.out.println(instance);
        }).start();*/
       //通过反射实现
        //拿到构造函数
        Constructor<InnerClassSingleton> declaredConstructor = InnerClassSingleton.class.getDeclaredConstructor();
        declaredConstructor.setAccessible(true);
        InnerClassSingleton innerClassSingleton = declaredConstructor.newInstance();

        InnerClassSingleton instance = InnerClassSingleton.getInstance();
        System.out.println(innerClassSingleton == instance);
    }

}
class InnerClassSingleton{//基于jvm,懒加载
    private static class InnerClassHolder{
        private static InnerClassSingleton instance = new InnerClassSingleton();
    }
    private InnerClassSingleton(){

        if(InnerClassHolder.instance != null){
            throw new RuntimeException("单例不允许多个实例");
        }
    }
    public static InnerClassSingleton getInstance(){
        return InnerClassHolder.instance;
    }
}

4.反射创建实例

代码如上(暴力反射)

使用静态内部类可以通过判断对反射攻击进行防护

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值