单例模式-实现线程是否安全

1.什么是单例模式?

        单例模式(Singleton)是一种创建型模式,指某个类采用Singleton单例模式后,则在这个类在同一个JVM上,只可能产生一个实例供外部访问,并且提供一个全局的访问方式。

2.实现方式

2.1 饿汉式-线程安全

        特点:线程安全,但无法实现懒加载策略

public class SingletonHungry {
    private static final SingletonHungry SINGLETON_HUNGRY = new SingletonHungry();

    private SingletonHungry(){}

    public static SingletonHungry instance(){
        return SINGLETON_HUNGRY;
    }
}

2.2 懒汉式-线程不安全

        特点:线程不安全,实现了实例懒加载策略

public class SingletonLazy {
    private static SingletonLazy singletonLazy = null;

    private SingletonLazy(){}

    public static SingletonLazy instance(){
        if (singletonLazy == null){
            //如果为空,在加载
            singletonLazy = new SingletonLazy();
        }

        return singletonLazy;
    }
}

2.3 全局锁式-线程安全

        特点:实现了懒加载策略,但是线程同步效率不高

public class SingletonAllSynchronize {
    private static SingletonAllSynchronize singletonAllSynchronize;

    private SingletonAllSynchronize(){}

    public synchronized static SingletonAllSynchronize instance(){
        if (singletonAllSynchronize == null){
            singletonAllSynchronize = new SingletonAllSynchronize();
        }
        return singletonAllSynchronize;
    }
}

2.4 静态代码块式-线程安全

        特点:线程安全,类加载时初始化实例,实现了懒加载策略。

public class SingletonStatic {
    private static final SingletonStatic singletonStatic;

    private SingletonStatic(){}

    static {
        singletonStatic = new SingletonStatic();
    }

    public static SingletonStatic instance(){
        return singletonStatic;
    }
}

2.5 双重校验锁式-线程安全

        特点:线程安全,并且实现了懒加载策略,同时保证了线程同步时的效率

public class SingletonDouble {
    private static SingletonDouble singletonDouble;

    private SingletonDouble(){}

    public static SingletonDouble instance(){
        if (singletonDouble == null){
            synchronized (SingletonDouble.class){
                if (singletonDouble == null){
                    singletonDouble = new SingletonDouble();
                }
            }
        }
        return singletonDouble;
    }
}

2.6 内部类-线程安全

         特点:线程安全,不存在线程同步问题,单例对象在程序第一次调用instance()时主动加载SingletonHolder 和其静态成员INSTANCE,因而实现了懒加载策略

public class SingletonIn {
    //构造方法的私有化
    private SingletonIn(){}

    //
    private static class SingletonHolder{
        //单例对象在内部中使用饿汉式创建
        private static final SingletonIn INSTANCE = new SingletonIn();
    }

    public static SingletonIn instance(){
        return SingletonHolder.INSTANCE;
    }
}

2.7枚举类-线程安全

         特点:线程安全,不存在线程同步问题,且单例对象在枚举类型INSTANCE第一次引用时通过枚举的 构造函数初始化,从而实现了懒加载策略

public class SingletonEnum {
    private SingletonEnum(){}

    enum SingletonEnum1{
        INSTANCE;

        private final SingletonEnum singletonEnum;

        private SingletonEnum1(){
            singletonEnum  = new SingletonEnum();
        }
    }

    public static SingletonEnum instance(){
        return SingletonEnum1.INSTANCE.singletonEnum;
    }
}

3.测试

public class Test {
    public static void main(String[] args) {
        IntStream.rangeClosed(1,100).forEach((x)->{
            SingletonHungry singletonHungry = SingletonHungry.instance();
            System.out.printf("第%d个对象:%s\n",x,singletonHungry);
        });
    }
}

你可能感兴趣的:(单例模式,java,开发语言)