• JAVA设计模式之单例(singleton)


    一、饿汉式

    /**
    * 饿汉式
    */
    public class Singleton01 {

    private static final Singleton01 instance = new Singleton01();

    private Singleton01(){}

    public static Singleton01 getInstance(){
    return instance;
    }

    public static void main(String[] args) {
    System.out.println(Singleton01.getInstance().hashCode());
    System.out.println(Singleton01.getInstance().hashCode());
    }
    }

    二、懒汉式

    /**
     * 懒汉式
     */
    public class Singleton02 {
    
        private static Singleton02 instance;
    
        private Singleton02(){}
    
        public static Singleton02 getInstance(){
            if(instance == null){
                instance = new Singleton02();
            }
            return instance;
        }
    
        public static void main(String[] args) {
            System.out.println(Singleton02.getInstance().hashCode());
            System.out.println(Singleton02.getInstance().hashCode());
        }
    }

    三、懒汉式线程不安全测试

    /**
    * 懒汉式 线程不安全测试
    */
    public class Singleton03 {

    private static Singleton03 instance;
    private static Object obj = new Object();

    private Singleton03(){}

    public static Singleton03 getInstance(){

    if(instance == null){
    try {
    Thread.sleep(1);
    } catch (InterruptedException 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();
    }
    }
    }

    四、懒汉式线程安全写法(坑),并不能保证线程安全

    /**
     * 懒汉式 线程不安全
     */
    public class Singleton03 {
    
        private static Singleton03 instance;
    
        private Singleton03(){}
    
        public static Singleton03 getInstance(){
    
                if(instance == null){
                    synchronized (Singleton03.class) {
                        try {
                            Thread.sleep(1);
                        } catch (InterruptedException 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();
            }
        }
    }

    5、懒汉式线程安全写法DCL,必须添加volatile关键字防止指令重排序

    /**
    * 懒汉式 线程安全
    */
    public class Singleton04 {

    private static volatile Singleton04 instance;

    private Singleton04(){}

    public static Singleton04 getInstance(){

    if(instance == null){
    synchronized (Singleton04.class) {
    if(instance == null){
    try {
    Thread.sleep(1);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    instance = new Singleton04();
    }
    }
    }
    return instance;
    }

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

    6、单例模式静态内部类实现

    /**
     * 静态内部类实现 线程安全
     */
    public class Singleton05 {
    
        private Singleton05(){}
    
        private static class SingletonHolder {
            private static final Singleton05 instance = new  Singleton05();
        }
    
        public static Singleton05 getInstance(){
            return SingletonHolder.instance;
        }
    
        public static void main(String[] args) {
            for(int i = 0; i< 100;i++) {
                new Thread (() ->{
                    System.out.println(Singleton05.getInstance().hashCode());
                }).start();
            }
        }
    }

    7、终极实现之枚举类,Joshua Bloch 的《effective java》中推荐写法,防止反射和反序列化并且线程安全

    /**
     * 枚举类实现 线程安全
     */
    public enum Singleton06 {
    
        instance;
        public static void main(String[] args) {
            for(int i = 0; i< 100;i++) {
                new Thread (() ->{
                    System.out.println(Singleton06.instance.hashCode());
                }).start();
            }
        }
    }
  • 相关阅读:
    洛谷 P1593 因子和
    洛谷 P1167 刷题
    洛谷 P1613 跑路
    洛谷 P1629 邮递员送信
    洛谷 P1654 OSU!
    洛谷 P1967 货车运输
    FPGA开平方的实现
    FPGA设计思想之串并转换
    verilog乘法器的设计
    FPGA浮点数定点数的处理
  • 原文地址:https://www.cnblogs.com/sx-bj-srr/p/12772661.html
Copyright © 2020-2023  润新知