设计模式-单例模式

设计模式-单例模式

说明

单例模式一共有 8 种, 但是只有 5 种是线程安全的

  • 饿汉式
  • 懒汉式-直接加锁
  • 懒汉式-双重检锁(性能好)
  • 静态内部类
  • 枚举

饿汉式

package com.shaoming.patterns.singleton;

/**
 * 饿汉式
 * 简单实用 , 线程安全
 * 推荐使用!!! 工作中可以使用
 * 唯一缺点 : 不使用该类示例也会进行实例化
 */
public class Singleton01 {
    private static final Singleton01 INSTANCE = new Singleton01();
    // 私有化构造器
    private Singleton01(){}
    
    public static Singleton01 getInstance(){
        return INSTANCE;
    }
    public void sayHello(){
        System.out.println("hello singleton01");
    }

    public static void main(String[] args) {
        // 第一种方式
        Singleton01 instance011 = Singleton01.getInstance();
        Singleton01 instance012 = Singleton01.getInstance();
        System.out.println(instance011==instance012);

    }
}

懒汉式-直接加锁

package com.shaoming.patterns.singleton;

/**
 懒汉式
 存在线程安全问题 , 需要加 synchronized 
 缺点: 性能有问题
 这种方式用的少
 */
public class Singleton02 {
   private static Singleton02 INSTANCE;
   
   private Singleton02(){}
    
   public static synchronized Singleton02 getInstance(){
       if(INSTANCE==null){

           try {
               Thread.sleep(1);
           } catch (Exception e) {
               e.printStackTrace();
           }
               INSTANCE = new Singleton02();
       }
       return INSTANCE;
   }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                System.out.println(Singleton02.getInstance().hashCode());
            }).start();
        }
    }
}

懒汉式-双重检锁

package com.shaoming.patterns.singleton;

/**
 * 饿汉式
 * 双重检锁
 * 缺点: 代码量复杂
 */
public class Singleton03 {
    // 需要加上 volatile
    private static volatile Singleton03 INSTANCE;
    
    private Singleton03(){}
    
    public static Singleton03 getInstance(){
        if(INSTANCE==null){
            synchronized (Singleton03.class){
                if(INSTANCE==null){
                    try {
                        Thread.sleep(1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    INSTANCE = new Singleton03();
                }
            }
        }
        return INSTANCE;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                System.out.println(Singleton03.getInstance().hashCode());
            }).start();
        }
    }
}

静态内部类

package com.shaoming.patterns.singleton;

/**
 * 静态内部类
 * 优点: 代码量少 , 不需要时候不进行实例初始化 
 * 只加载 singleton04 , 静态内部类不会被下载 , 调用 getInstance() 方法 ,才会加载内部类 , jvm 帮我们保证
 */
public class Singleton04 {
    
    private Singleton04(){}
    
    private static class Singleton04Holder{
        private final static Singleton04 INSTANCE = new Singleton04();
    }
    
    public static Singleton04 getInstance(){
        return Singleton04Holder.INSTANCE;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                System.out.println(Singleton04.getInstance().hashCode());
            }).start();
        }
    }
}

枚举

package com.shaoming.patterns.singleton;

/**
 * 枚举
 */
public enum Singleton05 {
    INSTANCE;
    
    private void sayHello(){
        System.out.println("say hello ==> enum");
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                System.out.println(Singleton05.INSTANCE.hashCode());
            }).start();
        }
    }
}

总结

int i = 0; i < 100; i++) {
new Thread(()->{
System.out.println(Singleton05.INSTANCE.hashCode());
}).start();
}
}
}


# 总结

使用 饿汉式 最简单 , 最好理解,线程安全 ,工作中可以使用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值