commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oliver Heger <oliver.he...@oliver-heger.de>
Subject Re: svn commit: r819141 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang/concurrent/LazyInitializer.java test/org/apache/commons/lang/concurrent/ test/org/apache/commons/lang/concurrent/LazyInitializerTest.java
Date Mon, 28 Sep 2009 19:53:49 GMT
sebb schrieb:
> On 28/09/2009, sebb <sebbaz@gmail.com> wrote:
>> On 28/09/2009, Stephen Colebourne <scolebourne@btopenworld.com> wrote:
>>  > sebb wrote:
>>  >
>>  > > Where is that described? The first reference I found says that the
>>  > > local variable is a performance optimisation; it's the volatile
>>  > > keyword that makes it thread-safe:
>>  > >
>>  > >
>>  > http://technology.amis.nl/blog/4384/the-double-checked-locking-confusion
>>  > > also
>>  > >
>>  > http://jeremymanson.blogspot.com/2008/05/double-checked-locking.html
>>  > >
>>  > > But of course these could be wrong...
>>  > >
>>  >
>>  > http://java.sun.com/developer/technicalArticles/Interviews/bloch_effective_08_qa.html
>>  >
>>  >  "The idiom is very fast but also complicated and delicate, so don't be
>>  > tempted to modify it in any way"
>>  >
>>
>>
>> That does not necessarily mean that the version without the temporary
>>  variable is wrong.
>>
>>  As I see it, the fact that the variable is volatile is enough to
>>  ensure as-sequential operation, because the write of the volatile
>>  variable happens-before the read.
>>
> 
> I've just discovered that the book also says:
> 
> "This code may appear a bit convoluted.
> In particular, the need for the local variable <result> may be unclear ...
> While not strictly necessary this may improve performance ..."
> 
> So the local variable is indeed for performance reasons - it reduces
> the number of reads of the volatile variable to once in the case where
> the variable has already been initialised.
> 

Yes, this was also my understanding.

Well, obviously it is easy to get this pattern wrong or at least with 
reduced performance. This makes it a good candidate for a low-level 
library like [lang].

Oliver

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message