Singleton(单例模式)

Singleton(单例模式)

单例一写法(无线程安全问题)

/**
 * 恶汉模式
 * 类加载到内存后,就实例一个单例,JVM保证线程安全
 * 简单实用,推荐使用
 * 唯一缺点:不管用到与否,类装载时就完成实例化(你能)
 */
public class Singleton01 {
    private static final Singleton01 INSTANCE = new Singleton01();
    private Singleton01() {};
    public static Singleton01 getInstance() { return INSTANCE;}
    public void m() {
        System.out.println("m");
    }

    public static void main(String[] args) {
        Singleton01 m1 = Singleton01.getInstance();
        Singleton01 m2 = Singleton01.getInstance();
        System.out.println(m1.toString().equals(m2.toString()) );
        System.out.println(m1.toString());
        System.out.println(m2.toString());
    }
}

单例二写法(存在问题)

/**
 * 懒汉模式
 * 线程不安全
 *
 */
public class Singleton02 {
    private static Singleton02 INSTANCE;
    private Singleton02(){

    };
    public static Singleton02 getInstance() throws InterruptedException {
        if (INSTANCE == null) {
            Thread.sleep(1);
            INSTANCE = new Singleton02();
        }
        return INSTANCE;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                    Singleton02 instance = null;
                    try {
                        instance = Singleton02.getInstance();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(instance.hashCode());
            },i+"").start();
        }
    }
}

单例三写法(存在问题)

package singleton;

/**
 * 直接锁方法解决了问题
 * 但带了结果效率低
 */
public class Singleton03 {
    private Singleton03(){};
    private static Singleton03 INTSANCE;
    public static synchronized Singleton03 getInstance() throws InterruptedException {
        if(INTSANCE == null){
            INTSANCE = new Singleton03();
            Thread.sleep(1);
        }
        return INTSANCE;
    }

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

单例四写法(存在问题)

package singleton;

public class Singleton04 {
    private Singleton04(){};
    private static Singleton04 INSTANCE;
    public static Singleton04 getInstance() throws InterruptedException {

        if(INSTANCE == null){
            //通过减少同步代码块提升效率  然后有问题
            synchronized (Singleton04.class){
                Thread.sleep(1);
                INSTANCE = new Singleton04();
            }
        }

        return INSTANCE;
    }

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

单例五写法(存在问题)

package singleton;

public class Singleton05 {
   private Singleton05(){};
   private static Singleton05 INSTANCE;
   public static Singleton05 getInstance() throws InterruptedException {
       if(INSTANCE == null){
           //双重检查单例
           synchronized (Singleton05.class){
               if(INSTANCE == null){
                   Thread.sleep(1);
                   INSTANCE = new Singleton05();//如果指令重排序还是会有问题
               }
           }
       }
       return INSTANCE;
   }

   public static void main(String[] args) {
       for (int i = 0; i < 10; i++) {
           new Thread(() ->{
               try {
                   Singleton05 instance = Singleton05.getInstance();
                   System.out.println(instance.hashCode());
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }

           }).start();
       }
   }
}

单例六写法(无线程安全问题)

package singleton;

public class Singleton06 {
   private Singleton06(){
   };
   private volatile static Singleton06 INSTANCE;//volatile 防止指令重排序
   public static Singleton06 getInstance() throws InterruptedException {
       if(INSTANCE == null){
           synchronized (Singleton06.class){
               Thread.sleep(1);
               if(INSTANCE == null){
                   INSTANCE = new Singleton06();
               }
           }
       }
       return INSTANCE;
   }

   public static void main(String[] args) {
       for (int i = 0; i < 10; i++) {
           new Thread(() -> {
               try {
                   Singleton06 instance = Singleton06.getInstance();
                   System.out.println(instance.hashCode());
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }).start();
       }
   }
}

单例七写法(无线程安问题 利用jvm类加载写的单例)

package singleton;

/**
* 静态内部类方式
* JVM保证单例  虚拟机加载类值加载一次
* 加载外部类时,不会加载内部类
* 利用静态内部类
*/
public class Singleton07 {
   private Singleton07 (){};
   private static class SingletonNext{
       private final static Singleton07 INSTNACE = new Singleton07();
   }
   public static Singleton07 getInstance(){
       return SingletonNext.INSTNACE;
   }

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

单例八写法(无线程安全问题 完美写法)

package singleton;

/**
* 完美写法
*/
public enum Singleton08 {
   INTSANCE;

   private void m(){
       System.out.println("我是m");
   }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值