设计模式:Singleton--单例模式

 饿汉式:线程安全
  1 直接实例化(简洁直观)
  2 枚举(最简洁)
 3 静态代码块(适合复杂实例化)

 饱汉式
  1 线程不安全(适用于单线程)
 2 线程安全 ,双端检索dcl+volatile
  3 静态内部类形式(适用于多线程)

一、饿汉式

1

public class Singleton1 {

    public static final Singleton1 INSTANCE = new Singleton1();

    private Singleton1() {};
}

2


public enum Singleton2 {
    INSTANCE
}

3

public class Singleton3 {

    public static final Singleton3 INSTANCE;
    private String info;
    static {
        Properties pro = new Properties();
        try {
            pro.load(Singleton3.class.getResourceAsStream("single.properties"));
        } catch (IOException e) {
           throw new RuntimeException(e);
        }

        INSTANCE = new Singleton3(pro.getProperty("info"));
    }
    private Singleton3(String info) {
        this.info = info;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Singleton3{" +
                "info='" + info + '\'' +
                '}';
    }
}

 

二、饱汉式

1

/**
 * Created by admin on 2019/3/12 22:17
 *
 * @Author: created by admin
 * @Date: created in 22:17 2019/3/12
 * @param: bindingResult
 * @param: result
 * @return:
 * @throws:
 * @Description:  线程不安全
 * @version:
 */

public class Singleton4 {

    private static Singleton4 instance;
    private Singleton4() {
    }

    public static Singleton4 getInstance () throws InterruptedException {
        if(instance == null) {
            Thread.sleep(100);
            instance = new Singleton4();
        }
        return instance;
    }
}

 

2

/**
 * Created by admin on 2019/3/12 22:17
 *
 * @Author: created by admin
 * @Date: created in 22:17 2019/3/12
 * @param: bindingResult
 * @param: result
 * @return:
 * @throws:
 * @Description:  线程安全的 双端检索dcl+volatile
 * @version:
 */

public class Singleton5 {

    private static volatile Singleton5 instance;
    private Singleton5() {
    }

    public static Singleton5 getInstance () throws InterruptedException {
        if(instance == null) {
            synchronized (Singleton5.class) {
                if(instance == null) {
                    Thread.sleep(100);
                    instance = new Singleton5();
                }
            }
        }
        return instance;
    }
}

3

/**
 * Created by admin on 2019/3/12 22:17
 *
 * @Author: created by admin
 * @Date: created in 22:17 2019/3/12
 * @param: bindingResult
 * @param: result
 * @return:
 * @throws:
 * @Description: 静态内部类不会自动随着外部类的家中和自动化而初始化,屙屎单独加载和初始化
 * 因为是在内部类加载和初始化时候创建的,因此是线程安全的。
 * @version: 线程安全,用内部类,饱汉式
 */

public class Singleton6 {


    private Singleton6 () {

    }

    private static class inner {
        private static final Singleton6 INSTANCE = new Singleton6();
    }

    public static Singleton6 getInstance() {
        return inner.INSTANCE;
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值