在Effective Java一书中提到double-check模式,并指出该模式在java中通常并不适用。该模式的结构如下
public Resource getResource(){ if(resource==null){ synchronized(this){ if(resource==null){ resource = new Resource(); } } } return resource; }
该模式是对下面代码的改进
public synchonized Resource getResource(){ if(resource == null){ resource = new Resource(); } return resource; }
为什么要有这个改进呢?首先第二段代码是对resource延迟初始化,等到第一次调用getResource()方法时才初始化。但是以后每次调用此方法都需要同步,而其实这个对象只会初始化一次。于是有了上面的改进
在java中double-check模式无效的原因是不同步情况下引用类型不是线程安全的。不过对于除了long和double的基本类型,double-check模式是使用的。
但是java的内存模式是不断改进的。Doug Lea在它的文章中写道“根据最新的JSR133的java内存模型,如果将引用类型声明为volatile,double-check模式就可以工作了”
如下
private volatile Resource resource; public Resource getResource(){ if (resource == null){ synchronized(this){ if (resource==null){ resource = new Resource(); } } } return resource; }
想起写过的一段代码异曲同工,有一个synchronized void start()方法,内部逻辑只走一次。有一个isStarted变量用于判断start()逻辑是否走过,代码如下
if(!isStarted){ synchronized void start(){ if(!isStarted){ 逻辑实现 isStarted = true; } } };
相关推荐
从单例谈double-check必要性,多种单例各取所需.doc
主要为大家详细介绍了Java中的双重检查(Double-Check),感兴趣的小伙伴们可以参考一下
NULL 博文链接:https://langyu.iteye.com/blog/799544
test.check(和double-check )的核心思想是,您无需为单元测试枚举预期的输入和输出,而是编写关于函数的属性,这些属性对于所有输入都应为true。 这使您可以编写简洁而强大的测试。 为什么要用叉子? 虽然test....
BOOL CMainFrame::OnCreateClient( LPCREATESTRUCT /*lpcs*/, CCreateContext* pcc) ... // Double-check for ON_NOTIFY in message map above ASSERT(AFX_IDW_PANE_FIRST == sw.IdFromRowCol(0,0)); return TRUE; }
选择适当的选项,点击“Find Orphaned Options” --->进入“To double-check options in the Orphaned Options list”页面--->单击下方的“Select All“ ,选择页面中的内容---->点击”View Selected Options ...
java代码-double check单例模式
Step 4: Double-Check the Data jq: JSON CLI Parser With No Arguments Select Certain Fields The Restaurant Collection 11 The MySQL Document Store The X DevAPI mysqlsh Connections Session Types ...
全新的一章,讨论了不同的混合构造,如ManualResetEventSlim、SemaphoreSlim、CountdownEvent、Barrier、ReaderWriterLock(Slim)、OneManyResourceLock、Monitor,3种用于处理double-check锁的方式,.NET 4.0中...
使用double-check来保证thread safety。但是如果处理大量数据时,该锁才成为严重的性能瓶颈。 1、静态成员实例的懒汉模式: class Singleton { private: static Singleton* m_instance; Singleton(){} public: ...
使用double-check来保证thread safety。但是如果处理大量数据时,该锁才成为严重的性能瓶颈。 1、静态成员实例的懒汉模式: class Singleton { private: static Singleton* m_instance; Singleton(){} public: ...
#Double Delete Double check delete button UI原型。 ## Demo ## CRED灵感来自由真棒Dribbble 图标由
使用double-check来保证thread safety.但是如果处理大量数据时,该锁才成为严重的性能瓶颈。 1、静态成员实例的懒汉模式: 1 class Singleton 2 { 3 private: 4 static Singleton* m_instance; 5 ...
doublecheck_std_services
Add User Database Table and Double-Check Database Security 118 Create Authentication API 119 Wrapping It Up 120 Chapter 8 Encryption 121 What Is Encryption? 121 Choosing an Encryption Type 123 ...
双数组Trie(Double-ArrayTrie)是trie树的一个简单而有效的实现,由两个整数数组构成,一个是base[],另一个是check[]。设数组下标为i,如果base[i],check[i]均为0,表示该位置为空。如果base[i]为负值,表示该状态为...
SPI digital protocol is used to communicate with the chip to configure and check for updates to the orientation, Directional-Tap TM /Double-Tap TM detection, Free fall detection, and activity ...
Double-click on the queue QAS. Choose Refresh. Position your cursor on the change request number created by Group A in exercise 8.1. Choose Request Import. Choose Continue and confirm if necessary....