23种设计模式之一(单例模式)

概念

一个类永远只有一个对象存在。

实现方式

方式1(饿汉式 - 线程安全)
package mirale.luna.design.patterns.singleton;

/**
 * Created by Miracle Luna on 2020/12/15
 *
 * 饿汉式:线程安全
 * 类加载到内存后,就实例化一个单例,JVM保证线程安全
 * 简单易用,推荐使用!
 * 唯一缺点:不管用到与否,类装载时就完成实例化
 */
public class Singleton01 {
    private static Singleton01 instance = new Singleton01();

    private Singleton01(){}

    public static Singleton01 getInstance(){
        return instance;
    }

    /**
     * 通过查看 hashCode值 是否一致,判断是否线程安全
     * @param args
     */
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> System.out.println(Singleton01.getInstance().hashCode())).start();
        }
    }
}
方式2(懒汉式 - 线程不安全)
package mirale.luna.design.patterns.singleton;

/**
 * Created by Miracle Luna on 2020/12/15
 *
 * 懒汉式:线程不安全
 */
public class Singleton02 {
    private static Singleton02 instance;

    private Singleton02() {}

    public static Singleton02 getInstance() {
        instance = new Singleton02();
        return instance;
    }

    /**
     * 通过查看 hashCode值 是否一致,判断是否线程安全
     * @param args
     */
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> System.out.println(Singleton02.getInstance().hashCode())).start();
        }
    }
}
方式3(懒汉式 - 线程安全 - 加锁 - 效率下降)
package mirale.luna.design.patterns.singleton;

/**
 * Created by Miracle Luna on 2020/12/15
 *
 * 懒汉式:线程安全(加锁实现,但是效率下降)
 */
public class Singleton03 {
    private static Singleton03 instance;

    private Singleton03() {}

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

    /**
     * 通过查看 hashCode值 是否一致,判断是否线程安全
     * @param args
     */
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> System.out.println(Singleton03.getInstance().hashCode())).start();
        }
    }
}
方式4(懒汉式 - 线程安全 - DCL - 效率提升 - 较完美的实现方式)
package mirale.luna.design.patterns.singleton;

/**
 * Created by Miracle Luna on 2020/12/15
 *
 * 懒汉式:线程安全
 * DCL: Double Check Loading
 * 较为完美的单例写法
 */
public class Singleton04 {
    private static volatile Singleton04 instance; // volatile:防止指令重排

    private Singleton04() {}

    public static Singleton04 getInstance() {
        if (instance == null) {
            // 双重检查
            synchronized (Singleton04.class) {
                if (instance == null) {
                    instance = new Singleton04();
                }
            }
        }
        return instance;
    }

    /**
     * 通过查看 hashCode值 是否一致,判断是否线程安全
     * @param args
     */
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> System.out.println(Singleton04.getInstance().hashCode())).start();
        }
    }
}

方式5(内部类实现 - 线程安全 - 懒加载 - 较完美的实现方式)

package mirale.luna.design.patterns.singleton;

/**
 * Created by Miracle Luna on 2020/12/15
 *
 * 静态内部类
 * JVM 保证单例,且线程安全,不用加锁
 * 加载外部类时不会加载内部类,变相实现了懒加载(Lazy Loading)
 * 较为完美的单例写法
 */
public class Singleton05 {

    private Singleton05() {}

    private static class Singleton05Holder {
        private static final Singleton05 instance = new Singleton05();
    }

    public static Singleton05 getInstance() {
        return Singleton05Holder.instance;
    }

    /**
     * 通过查看 hashCode值 是否一致,判断是否线程安全
     * @param args
     */
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> System.out.println(Singleton05.getInstance().hashCode())).start();
        }
    }
}
方式6(枚举类实现 - 线程安全 - 防止反序列化 - 最完美的实现方式)
package mirale.luna.design.patterns.singleton;

/**
 * Created by Miracle Luna on 2020/12/15
 *
 * 枚举单例
 * 不仅实现了线程同步,而且防止了反序列化
 * 最完美的单例写法
 */
public enum Singleton06 {
    instance;

    /**
     * 通过查看 hashCode值 是否一致,判断是否线程安全
     * @param args
     */
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> System.out.println(Singleton06.instance.hashCode())).start();
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值