- 浏览: 729140 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (419)
- 杂软粉墨 (2)
- 创意灵感 (3)
- 经验记录 (137)
- 开源轨迹 (2)
- sip-communicator (2)
- 闲侃杂谈 (8)
- 问题交流 (24)
- 概念模式 (32)
- 难点备案 (5)
- JwChat (1)
- 中国象棋 (1)
- 教育探索 (6)
- 英语研究 (58)
- 星际争霸 (1)
- 电信知识 (1)
- 软件架构 (3)
- 哲学探索 (26)
- 算法灵魂 (8)
- 近视探索 (6)
- 数学数学 (3)
- 牛角钻尖 (23)
- 至强文言 (3)
- 数据结构 (1)
- 宇宙物理 (2)
- 网络架构 (3)
- 游戏领域 (4)
- 图形处理 (2)
- 修炼之路 (8)
- 读书天地 (20)
- 编解乱码 (2)
- 概念探索 (8)
- 格物致知 (1)
- 其它语言 (1)
- 测试领域 (3)
- 文化风流 (1)
- JQuery (1)
- 網頁領域 (1)
- Unix/Linux (1)
- Inside JVM (1)
- 异常分析 (1)
最新评论
-
suyujie:
引用
HTML <a> 标签灰显禁用 -
suyujie:
HTML <a> 标签灰显禁用 -
suyujie:
HTML <a> 标签灰显禁用 -
suyujie:
HTML <a> 标签灰显禁用 -
iamzhoug37:
您能说一下"局部变量不受文本顺序限制" 是 ...
声明前为什么能赋值却不能输出,都是使用
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
发表评论
-
NullPointerException in ternary operator
2013-01-19 00:01 1075java代码: Integer i = null; ... -
why concrete class must implement abstract method while abstract class need not
2011-05-11 13:27 1474yes, just as the title...why co ... -
Enum singleton - Elvis Stealer
2011-04-26 14:41 1608import java.io.ByteArrayInputSt ... -
why can inner class be instantiated in this way?
2011-04-06 10:24 971public class Outer { pub ... -
into native source code
2011-03-23 23:09 910once there was a problem occurr ... -
about Thread.join method
2011-03-23 08:09 1231import java.util.Timer; import ... -
why string concatenation yields no interned one
2011-03-14 16:48 1083public class Test { public st ... -
why can bit mask suppress sign extention
2011-03-10 09:16 1141yes, bit mask can suppress sign ... -
About gc two objects which are inter referenced to each other
2011-03-08 11:00 1000my workmate told me such a sena ... -
circumvents exception checking
2011-03-03 11:02 930import java.io.IOException; ... -
关于维基中singleton pattern的一段
2011-02-24 10:00 1065Another notable difference is t ... -
why List hasn't clone method but the ArrayList has
2011-02-16 15:27 1127List list = new ArrayList(); ... -
deep copy
2011-02-16 15:20 979static public Object deepCopy(O ... -
Java is Pass-by-Value
2011-02-14 13:28 1031This often heard saying is not ... -
不是说字符串不可变吗
2011-02-14 11:28 866from csdn: http://topic.csdn.ne ... -
没有实现抽象方法的具体类居然编译通过了
2011-02-04 22:29 935来自csdn: http://topic.csdn.net/ ... -
关于iterator的fail-fast
2011-01-20 11:15 956是使用集合的iterator后,再改变就可能抛出这个异常 ... -
生成几个String的问题
2011-01-11 16:26 981public class Test extends java. ... -
无符号右移>>>
2011-01-11 15:46 2307public class Test { public st ... -
Struts2之log信息不出的问题
2010-12-30 11:18 2969刚开始学习Struts2的时候,用的是maven方式,但是却不 ...
相关推荐
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模式的方法。分享给大家供大家参考。具体实现方法如下: class Singleton(type): def __call__(cls, *args, **kwargs): print "Singleton call" if not hasattr...
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
java开发大猫聊天室源码 作者: 原文地址: UML和代码 单例模式 饿汉式 类加载的时候就会初始化这个实例, JVM保证唯一实例,线程安全, 但是可以通过反射破坏 ...Class.forName("singleton.Singleto
Singleton模式主要作用是保证在Java应用程序中,一个类Class只有一个实例存在。 一般Singleton模式通常有几种种形式: 第一种形式: 定义一个类,它的构造函数为private的,它有一个static的private的该类变量,在类...
Android Singleton单例模式运用详解
一个java线程安全的单例模式:饥饿模式和延迟加载
C++完美实现Singleton模式
简单的单例模式举例Singleton 分为恶汉式 懒汉式
privatestatic Singleton instance; privatestaticfinal String CONFIG_FILE_PATH = "E:\\config.properties"; private Properties config; private Singleton() { config = new Properties(); ...
1、没有构造函数(DEFINE_SINGLETON_DEFAULT); 2、有构造函数,构造函数没有参数(DEFINE_SINGLETON_CONSTRUCT_NO_PARAM); 3、有构造函数,构造函数有没有参数版本(DEFINE_SINGLETON_CONSTRUCT_WITH_DEFAULT)...
* * * * Bean的作用域 作用域的种类 Spring 4.3中为Bean的实例定义了7种作用域,如下表所示: 注意:在上表7种作用域中,singleton和prototype是最常用的两种作用域。 在Spring配置文件中,可以使用元素的scope属性...
参考文档:电梯直达 EventBusUtils //订阅者回调签名 typedef void EventCallback(arg);... static EventBus _singleton = new EventBus._internal(); /// 工厂构造函数 factory EventBus() => _singleton; /// 保
Singleton模式包含的角色只有一个,就是Singleton。Singleton拥有一个私有构造函数,确保用户无法通过new直接实例化它。除此之外,该模式中包含一个静态私有成员变量instance与静态公有方法Instance()。Instance()...
单例模式 Singleton 单例模式线程安全问题和拓展
private static Singleton instance=new Singleton(); private Singleton(){} static Singleton getInstance() { return instance; } } 懒汉式 class Singleton { private static Singleton instance=null; ...
Singleton pattern单例模式应用
单例模式,Singleton两种代码实现。一般实现方法,泛型实现方法(推荐)