zoukankan      html  css  js  c++  java
  • 单例(Singleton)模式

    最简单的单例「饿汉式」 

    public class Singleton{

    private static Singleton instance=new Singleton();
        //other fields
        private Singleton(){}
        public static Singleton getInstance(){
             return instance;
        }
        //other methods
    }

    出于性能等方面的考虑,希望延迟实例化单例对象(static 属性在加载类时会被初始化),方案二应运而生,称之为「懒汉式」

    public class Singleton {

    private static Singleton instance=null;
        // 私有构造方法,防止从外部使用 new 方法创建对象
    private Singleton() {
        }
        public static Singleton getInstance() {
    if (instance == null) {
    instance = new Singleton();
    }
    return instance;
    }
    }
    不幸的是,在高并发的环境中,getInstance()方法返回了多个指向不同的该类实例,也就是说:线程不安全。

    线程安全写法A
    public class Singleton {

    private static Singleton instance=null;
      // 私有构造方法,防止从外部使用 new 方法创建对象
    private Singleton() {
      }
      public static synchronized Singleton getInstance() {
    if (instance == null) {
    instance = new Singleton();
    }
    return instance;
    }
    }
    方案A中虽然实现了多线程的安全访问,但在多线程高并发访问情况下,加上 synchronized关键字会使得性能大不如前。

    线程安全写法B

    public class Singleton {

    private static volatile Singleton instance=null;
      // 私有构造方法,防止从外部使用 new 方法创建对象
    private Singleton() {
      }
      public static synchronized Singleton getInstance() {
    if (instance == null) {
    synchronized(Singleton.class){
    if (instance == null) {
    instance = new Singleton();
    }
    }
    }
    return instance;
    }
    }
    方案B,我们称之为 Double-Checked Locking (双重检查加锁)模式。在此方案中,还是使用 synchronized,只不过这次只是保证实例化的这段逻辑被一个线程执行。

    前述的方法都存在小小的缺陷,有没有一种既能实现延迟加载,又能实现线程安全的方案呢

    终极方案

    public class Singleton{
         private Singleton(){}

    private static class LazyHolder{
    private static final Singleton instance = new Singleton();
    }

    public static Singleton getInstance(){
    return LazyHodler.instance;
    }
    }

  • 相关阅读:
    vim 乱码问题的方法参考
    Node.js的原型继承函数 util.inherits
    UML类图画法及其之间的几种关系
    repo 小结
    Js_Eval方法
    Js_数组操作
    Js_字体滚动换颜色
    Js_图片轮播
    Jq_网站顶部定时折叠广告
    Js_网站右下角悬浮视窗可关闭广告
  • 原文地址:https://www.cnblogs.com/rgbw/p/4654636.html
Copyright © 2011-2022 走看看