From: "Adam Heath" <[email protected]>
On 05/12/2012 04:28 PM, Jacques Le Roux wrote:
I'm sure you are both afraid of that, but in case you freithened people
Oops, was late it seems. I meant " I'm sure you are both *aware" of that :o)
about DCL I wanted to say that since jdk 1.5 way of handling volatile
it's now possible to safely use this pattern with Java
http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html#ThreadLocal
(see "Fixing Double-Checked Locking using Volatile" section)
Actually it was not my main point. Some months ago I read 2 other very
simple and interesting ways (not much code, and simple) to handle the
same goal, but I can't find the article anymore :/
Sure, volatile makes DCL *safe*, but it is still slower, in the case
when the initial fetch is null. For static initialization, it might be
ok to use it. But it's probably more confusing to unaware java
programmers(using volatile+DCL). Using CAS-type mechanisms makes it
more explicit what is actually occuring.
And, when DCL is used against map-type(or collection-type) systems,
where the value could go away at any time, removing DCL can actually
make things faster, as you are reducing contention.
Yes using volatile with DCL is defintitvely slower. What do you call CAS-type?
Jacques