收集了部分关于Singleton的资料
2009-05-15 17:09
387 查看
收集了部分Singletong Pattern的文章。突然发觉原来Singleton模式还没那么简单啊。
感觉比较好的实现有
1.volatile 关键字的使用
Double-checked synchronization,将instance声明为volatile即可,在jdk5以上版本。
2.一种比较好的实现方式是Initialization on Demand Holder (IODH):
3. 在effective java里的一种新型的singleton:
// Enum singleton - the preferred approach
public enum Elvis {
INSTANCE;
public void leaveTheBuilding() { ... }
}
参考资料:
http://www.jdon.com/jivejdon/thread/17133.html
http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
http://www.javaeye.com/topic/363729
http://www.chinaunix.net/jh/4/713279.html
http://www.javaeye.com/topic/13894
http://www.javaeye.com/topic/211471?page=1
http://www.javaeye.com/topic/211937
http://www.blogjava.net/samyang/archive/2007/12/21/169310.html
感觉比较好的实现有
1.volatile 关键字的使用
Double-checked synchronization,将instance声明为volatile即可,在jdk5以上版本。
// Works with acquire/release semantics for volatile // Broken under current semantics for volatile class Foo { private volatile Helper helper = null; public Helper getHelper(); { if (helper == null); { synchronized(this); { if (helper == null); helper = new Helper();; } } return helper; } } // Works with acquire/release semantics for volatile // Broken under current semantics for volatile class Foo { private volatile Helper helper = null; public Helper getHelper(); { if (helper == null); { synchronized(this); { if (helper == null); helper = new Helper();; } } return helper; } }
2.一种比较好的实现方式是Initialization on Demand Holder (IODH):
public class Singleton { static class SingletonHolder { static Singleton instance = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.instance; } }
3. 在effective java里的一种新型的singleton:
// Enum singleton - the preferred approach
public enum Elvis {
INSTANCE;
public void leaveTheBuilding() { ... }
}
参考资料:
http://www.jdon.com/jivejdon/thread/17133.html
http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
http://www.javaeye.com/topic/363729
http://www.chinaunix.net/jh/4/713279.html
http://www.javaeye.com/topic/13894
http://www.javaeye.com/topic/211471?page=1
http://www.javaeye.com/topic/211937
http://www.blogjava.net/samyang/archive/2007/12/21/169310.html
相关文章推荐
- VC学习资料收集(2):关于数据类型转换
- VC学习资料收集(7):关于《连连看》的算法
- 在网上收集了一部分关于使用Google API进行手机定位的资料和大家分享
- 正在学习关于sniffer的编程,收集到的资料(有经过修正程序)
- 收集的一些关于3维方面的资料
- 关于sql server全文检索的一些资料收集2
- 关于vs2005的一些资料收集
- 网上收集的关于SQL线程、进程、连接、.net连接池等方面的资料
- 关于反射的资料收集
- 关于GRUB收集的一点资料
- 关于红酒的资料收集-1
- 关于code generation的一些资料收集
- 关于图像识别的一些资料收集
- 关于sql server全文检索的一些资料收集2
- 关于C++实现的Singleton收集 2
- 关于JPA的资料收集
- 关于路径资料的一些收集
- 关于C# Xamarin 资料收集
- 关于 darwin 转发服务器收集的资料
- 关于Controller方面的总结__以及资料收集