`

why is static inner class singleton thread safe?

阅读更多

http://en.wikipedia.org/wiki/Initialization_on_demand_holder_idiom

 

public class Singleton {

    // Private constructor prevents instantiation from other classes
    private Singleton() {
        System.out.println("constructor");
    }

    /**
     * SingletonHolder is loaded on the first execution of
     * Singleton.getInstance() or the first access to SingletonHolder.INSTANCE,
     * not before.
     */
    private static class SingletonHolder {
        public static final Singleton INSTANCE = new Singleton();
    }

    public static Singleton getInstance() {
        return SingletonHolder.INSTANCE;
    }

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

}

 

When to use it

Use this pattern if the initialization of the class is expensive and it cannot be done safely at class-loading time and the initialization is highly concurrent. The crux of the pattern is the safe removal of the synchronization overhead associated with accessing a singleton instance.

其中有一种使用该模式的情况就是在class-loading阶段如果初始化不安全

Since the class initialization phase is guaranteed by the JLS to be serial, i.e., non-concurrent, no further synchronization is required in the static getInstance method during loading and initialization.
类初始化阶段是连续的,也就是非并发的

 

不过构造时也会引起double-checked locking问题 就是会存在某一个瞬间引用不为空但构造未完成。

 

from: http://topic.csdn.net/u/20110222/13/87f22039-9ef0-48d0-8270-c91516e5cee5.html

分享到:
评论

相关推荐

    设计模式之单例模式程序

    public class Singleton { private static String name; public static String getName() { return name; } public static void setName(String name) { Singleton.name = name; } private Singleton...

    Python使用metaclass实现Singleton模式的方法

    本文实例讲述了Python使用metaclass实现Singleton模式的方法。分享给大家供大家参考。具体实现方法如下: class Singleton(type): def __call__(cls, *args, **kwargs): print "Singleton call" if not hasattr...

    php设计模式 Singleton(单例模式)

    private function __construct() { } static public function getInstance() { if(is_null(self::$_instance)) { self::$_instance = new Singleton(); } return self::$_instance; } public functi

    perl-Class-Singleton-1.4-6.el6.noarch.rpm

    perl-Class-Singleton-1.4-6.el6.noarch.rpm perl-Class-Singleton-1.4-6.el6.noarch.rpm

    java开发大猫聊天室源码-dp:设计模式学习

    java开发大猫聊天室源码 作者: 原文地址: UML和代码 单例模式 饿汉式 类加载的时候就会初始化这个实例, JVM保证唯一实例,线程安全, 但是可以通过反射破坏 ...Class.forName("singleton.Singleto

    Java面试-代码与编程题.doc

    Singleton模式主要作用是保证在Java应用程序中,一个类Class只有一个实例存在。 一般Singleton模式通常有几种种形式: 第一种形式: 定义一个类,它的构造函数为private的,它有一个static的private的该类变量,在类...

    Android Singleton单例模式Demo

    Android Singleton单例模式运用详解

    java-thread-safe-singleton-pattern.rar_starvation

    一个java线程安全的单例模式:饥饿模式和延迟加载

    C++完美实现Singleton模式

    C++完美实现Singleton模式

    Java 单例模式Singleton

    简单的单例模式举例Singleton 分为恶汉式 懒汉式

    Java学习测试题,看你会多少(主要基础类)

    privatestatic Singleton instance; privatestaticfinal String CONFIG_FILE_PATH = "E:\\config.properties"; private Properties config; private Singleton() { config = new Properties(); ...

    单例极致 singleton C++

    1、没有构造函数(DEFINE_SINGLETON_DEFAULT); 2、有构造函数,构造函数没有参数(DEFINE_SINGLETON_CONSTRUCT_NO_PARAM); 3、有构造函数,构造函数有没有参数版本(DEFINE_SINGLETON_CONSTRUCT_WITH_DEFAULT)...

    基于java的企业级应用开发:Bean的作用域.ppt

    * * * * Bean的作用域 作用域的种类 Spring 4.3中为Bean的实例定义了7种作用域,如下表所示: 注意:在上表7种作用域中,singleton和prototype是最常用的两种作用域。 在Spring配置文件中,可以使用元素的scope属性...

    Flutter中EventBus的使用

    参考文档:电梯直达 EventBusUtils //订阅者回调签名 typedef void EventCallback(arg);... static EventBus _singleton = new EventBus._internal(); /// 工厂构造函数 factory EventBus() => _singleton; /// 保

    单例模式Singleton(java源码)

    Singleton模式包含的角色只有一个,就是Singleton。Singleton拥有一个私有构造函数,确保用户无法通过new直接实例化它。除此之外,该模式中包含一个静态私有成员变量instance与静态公有方法Instance()。Instance()...

    Singleton 单例模式的介绍以及解析

    单例模式 Singleton 单例模式线程安全问题和拓展

    单例模式

    private static Singleton instance=new Singleton(); private Singleton(){} static Singleton getInstance() { return instance; } } 懒汉式 class Singleton { private static Singleton instance=null; ...

    Singleton pattern单例模式应用

    Singleton pattern单例模式应用

    Singleton两种代码实现

    单例模式,Singleton两种代码实现。一般实现方法,泛型实现方法(推荐)

Global site tag (gtag.js) - Google Analytics