设计模式 懒汉模式

1.新手写法 单线程安全 多线程 不安全 (千万不能用)

public class People {

    private static People singleton;

    private People() {
        System.out.println(Thread.currentThread().getName()+"    OK");
    }

    public static People getInstance() {
        if (singleton == null) {
            singleton = new People();
        }
        return singleton;
    }
}

2. 静态方法加锁

资源浪费 性能不好
原因 锁的东西太多了

/**
 *线程安全,
 *
 */
public class Fish {

    private static Fish fish;

    private Fish() {
        System.out.println(Thread.currentThread().getName()+"    OK");
    }

    public static synchronized Fish getInstance() {
        if (fish == null) {
            fish = new Fish();
        }
        return fish;
    }
}

3 ,双重检查加 禁止指令重排

/**
 *双重检查锁 模式
 *
 *
 */
public class Tiger {

    private static volatile Tiger tiger;   //volatile 禁止指令从重排

    private Tiger() {
        System.out.println(Thread.currentThread().getName()+"    OK");
    }

    public static Tiger getInstance() {
        if (tiger == null) {
            synchronized (Tiger.class) {
                if (tiger == null) {
                    tiger = new Tiger();
                    /**
                     * 1 分配内存空间
                     * 2.执行构造方法 初始化对象
                     *3.把这个对象指向这个空间
                     *  指令重排现象
                     */
                }
            }
        }
        return tiger;
    }
}

以上方法防好人不妨坏人

因为Java 1.8 反射的存在

测试方法


import org.junit.Test;

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


public class TigerTest {


    /**
     * 采用多线程的方式
     *
     */

 @Test
    public void threadTset() throws InterruptedException {

    for(int i=0;i<10;i++)
    {
        new Thread(()->{
            System.out.println(Tiger.getInstance().hashCode());
            /* System.out.println(People.getInstance().hashCode());*/
        }).start();
    }
    }

    @Test //采用 反射  获得对象
    public  void  reflection() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {

        Tiger instance = Tiger.getInstance();
        System.out.println(instance.hashCode());
        Constructor<Tiger> declaredConstructor = Tiger.class.getDeclaredConstructor(null);
        declaredConstructor.setAccessible(true); //无视私有的构造器
        Tiger tiger = declaredConstructor.newInstance();
        System.out.println(tiger.hashCode());
    }
}

4.静态内部类

/**
 *
 * 静态内部类
 */
public class Pig {

    private Pig() {
        System.out.println(Thread.currentThread().getName()+"    OK");
    }

    private static class chicken {
        private static final Pig PIG = new Pig();
    }

    public static Pig getInstance() {
        return chicken.PIG;
    }
}

5.加强版 双重检查锁

/**
 *双重检查锁 模式
 *
 *
 */
public class TigerStrengthen {
private static Boolean NWK=false;
    private static volatile TigerStrengthen tiger;   //volatile 禁止指令从重排

    private TigerStrengthen()
    {
       synchronized (TigerStrengthen.class)
       {
         /*  if(tiger!=null)
           {
               throw new RuntimeException("小伙子很有想法");
           }*/
         if(NWK==false)
           {
             NWK=true;
           }
           else
           {
               throw new RuntimeException("小伙子很有想法");
           }
       }
    }

    public static TigerStrengthen getInstance() {
        if (tiger == null) {
            synchronized (TigerStrengthen.class) {
                if (tiger == null) {
                    tiger = new TigerStrengthen();
                    /**
                     * 1 分配内存空间
                     * 2.执行构造方法 初始化对象
                     *3.把这个对象指向这个空间
                     *  指令重排现象
                     */
                }
            }
        }
        return tiger;
    }
}

测试方法
里面的方法和注释掉的代码有关
测试时请前找到对应的方法

import org.junit.Test;

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


public class TigerStrengthenTest {


    /**
     * 采用多线程的方式
     *
     */

 @Test
    public void threadTset() throws InterruptedException {

    for(int i=0;i<10;i++)
    {
        new Thread(()->{
            System.out.println(TigerStrengthen.getInstance().hashCode());

        }).start();
    }
    }
    @Test
    public  void  reflection() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {

        TigerStrengthen instance = TigerStrengthen.getInstance();
        System.out.println(instance.hashCode());
        Constructor<TigerStrengthen> declaredConstructor = TigerStrengthen.class.getDeclaredConstructor(null);
        declaredConstructor.setAccessible(true); //无视私有的构造器
        TigerStrengthen tigerStrengthen = declaredConstructor.newInstance();
        System.out.println(tigerStrengthen.hashCode());
    }


    @Test
    public  void  reflectiontS() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {


        Constructor<TigerStrengthen> declaredConstructor = TigerStrengthen.class.getDeclaredConstructor(null);
        declaredConstructor.setAccessible(true); //无视私有的构造器
        TigerStrengthen tigerStrengthen1 = declaredConstructor.newInstance();
        TigerStrengthen tigerStrengthen2 = declaredConstructor.newInstance();
        System.out.println(tigerStrengthen1.hashCode());
        System.out.println(tigerStrengthen2.hashCode());
    }
    @Test
    public  void  reflectiontSS() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
        Field nwk = TigerStrengthen.class.getDeclaredField("NWK");
        nwk.setAccessible(true);

        Constructor<TigerStrengthen> declaredConstructor = TigerStrengthen.class.getDeclaredConstructor(null);
        declaredConstructor.setAccessible(true); //无视私有的构造器
        TigerStrengthen tigerStrengthen1 = declaredConstructor.newInstance();
        nwk.set(nwk,false);
        TigerStrengthen tigerStrengthen2 = declaredConstructor.newInstance();
        System.out.println(tigerStrengthen1.hashCode());
        System.out.println(tigerStrengthen2.hashCode());
    }

}

猜你感兴趣的
饿汉模式
https://blog.csdn.net/A980719/article/details/117429760

枚举实现单例
https://blog.csdn.net/A980719/article/details/117429536

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一叶一菩提魁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值