- 浏览: 748064 次
- 性别:
- 来自: 上海
-
文章分类
- 全部博客 (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 1126java代码: Integer i = null; ... -
why concrete class must implement abstract method while abstract class need not
2011-05-11 13:27 1517yes, just as the title...why co ... -
Enum singleton - Elvis Stealer
2011-04-26 14:41 1636import java.io.ByteArrayInputSt ... -
why can inner class be instantiated in this way?
2011-04-06 10:24 1021public class Outer { pub ... -
into native source code
2011-03-23 23:09 966once there was a problem occurr ... -
about Thread.join method
2011-03-23 08:09 1276import java.util.Timer; import ... -
why string concatenation yields no interned one
2011-03-14 16:48 1114public class Test { public st ... -
why can bit mask suppress sign extention
2011-03-10 09:16 1182yes, bit mask can suppress sign ... -
About gc two objects which are inter referenced to each other
2011-03-08 11:00 1053my workmate told me such a sena ... -
circumvents exception checking
2011-03-03 11:02 969import java.io.IOException; ... -
关于维基中singleton pattern的一段
2011-02-24 10:00 1095Another notable difference is t ... -
why List hasn't clone method but the ArrayList has
2011-02-16 15:27 1173List list = new ArrayList(); ... -
deep copy
2011-02-16 15:20 1041static public Object deepCopy(O ... -
Java is Pass-by-Value
2011-02-14 13:28 1096This often heard saying is not ... -
不是说字符串不可变吗
2011-02-14 11:28 898from csdn: http://topic.csdn.ne ... -
没有实现抽象方法的具体类居然编译通过了
2011-02-04 22:29 976来自csdn: http://topic.csdn.net/ ... -
关于iterator的fail-fast
2011-01-20 11:15 997是使用集合的iterator后,再改变就可能抛出这个异常 ... -
生成几个String的问题
2011-01-11 16:26 1013public class Test extends java. ... -
无符号右移>>>
2011-01-11 15:46 2345public class Test { public st ... -
Struts2之log信息不出的问题
2010-12-30 11:18 3024刚开始学习Struts2的时候,用的是maven方式,但是却不 ...
相关推荐
1. 饿汉式(Static Inner Class Singleton): 这种方法在类加载时就完成了初始化,所以是线程安全的。它通过将单例类的构造函数设置为私有,并在内部类中创建一个静态的单例对象,保证了只有一个实例存在。例如: ...
private static class SingletonInstance { private static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonInstance.INSTANCE; } } ``` 7. ...
private static final Singleton INSTANCE = new Singleton(); } private Singleton() {} public static Singleton getInstance() { return SingletonHolder.INSTANCE; } } ``` 上述代码是典型的双检锁/...
private static class SingletonInstance { private static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonInstance.INSTANCE; } } ``` 优点:...
private static final Singleton INSTANCE = new Singleton(); private Singleton() {} public static Singleton getInstance() { return INSTANCE; } } ``` 2. 懒汉式单例: 这种方式在第一次调用`...
private static final Singleton INSTANCE = new Singleton(); private Singleton() {} public static Singleton getInstance() { return INSTANCE; } } ``` 2. 懒汉式(Lazy Initialization): 类加载时...
static Singleton* Instance() { if (_instance == nullptr) { _instance = new Singleton; } return _instance; } protected: Singleton() {} virtual ~Singleton() {} private: static Singleton* _...
private static final Singleton INSTANCE = new Singleton(); private Singleton() {} public static Singleton getInstance() { return INSTANCE; } } ``` 2. 饿汉式(静态代码块) 与静态常量类似,但将...
private static class SingletonInstance { private static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonInstance.INSTANCE; } } ``` 以上就是...
Private static class final Inner{ Private static final Singleton INSTATICE= new Singleton(); } Public static Singleton getInstance(){ Return Inner.INSATICE; } } ``` 这种方式的优点是实例对象的...
private static final Singleton INSTANCE = new Singleton(); private Singleton() {} public static Singleton getInstance() { return INSTANCE; } } ``` 2. **饿汉式(静态代码块)** 类加载时初始化...
private static final Singleton INSTANCE = new Singleton(); private Singleton() {} public static Singleton getInstance() { return INSTANCE; } } ``` 2. 懒汉式(线程不安全): 这种方式在类加载时并不...
private static final Singleton INSTANCE = new Singleton(); private Singleton() {} public static Singleton getInstance() { return INSTANCE; } } ``` 这种实现方式在类加载时就完成了实例化,线程安全...
private static final Singleton INSTANCE = new Singleton(); private Singleton() {} public static Singleton getInstance() { return INSTANCE; } } ``` 2. **懒汉式**: 懒汉式是在首次调用`getInstance...
4. **静态内部类(Static Inner Class)**:利用类加载机制保证初始化实例时只有一个线程,避免了同步锁,同时也延迟了初始化。 ```java public class Singleton { private Singleton() {} private static class ...
private static readonly Lazy<Singleton> lazy = new Lazy<Singleton>(() => new Singleton(), isThreadSafe: true); private Singleton() {} public static Singleton Instance => lazy.Value; } ``` 6...
private static class SingletonInstance { private static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonInstance.INSTANCE; } } ``` 这种实现...
private static class SingletonInstance { private static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonInstance.INSTANCE; } } ``` 类加载...
private static final Singleton INSTANCE = new Singleton(); private Singleton() {} public static Singleton getInstance() { return INSTANCE; } } ``` 2. 懒汉式(Lazy Initialization):在第一次调用...
private static final Singleton INSTANCE = new Singleton(); private Singleton() {} public static Singleton getInstance() { return INSTANCE; } } ``` 2. 线程安全的同步方法: 在多线程环境中,为了...