Topic: 单子模式的双重检测 |
Print this page |
1.单子模式的双重检测 | Copy to clipboard |
Posted by: andy_wang_5 Posted on: 2007-09-25 11:28 public class LazySingleton { public static LazySingleton m_instance = null; /** *私有的默认构造函数,保证外界没办法直接实列化。 */ private LazySingleton() {} /** *静态工厂方法,返回此类的惟一实列 */ public static LazySingleton getInstance() { if (m_instance == null) { //more than one threads might be here!!! synchronized (LazySingleton.class) { if (m_instance == null) { m_instance = new LazySingleton(); } } } return m_instance; } } 为什么如上的双重检测不适用于java,而使用与C++ ?请高手指点。 |
2.Re:单子模式的双重检测 [Re: andy_wang_5] | Copy to clipboard |
Posted by: JiafanZhou Posted on: 2007-09-25 17:11 C++ uses a different thread scheduling mechanism which is different from the one in Java in essence. |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |