<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: The Double Checked Locking confusion</title>
	<atom:link href="http://technology.amis.nl/2008/12/14/the-double-checked-locking-confusion/feed/" rel="self" type="application/rss+xml" />
	<link>http://technology.amis.nl/2008/12/14/the-double-checked-locking-confusion/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-double-checked-locking-confusion</link>
	<description></description>
	<lastBuildDate>Fri, 12 Apr 2013 10:04:09 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
	<item>
		<title>By: michael</title>
		<link>http://technology.amis.nl/2008/12/14/the-double-checked-locking-confusion/#comment-5632</link>
		<dc:creator>michael</dc:creator>
		<pubDate>Sat, 03 Jan 2009 17:43:29 +0000</pubDate>
		<guid isPermaLink="false">http://technology.amis.nl/blog/?p=4384#comment-5632</guid>
		<description><![CDATA[Why the result variable is needed there?
Why is it not enough to add the volatile keyword?
when doing if (instance == null ) - read operation is performed.

Thanks]]></description>
		<content:encoded><![CDATA[<p>Why the result variable is needed there?<br />
Why is it not enough to add the volatile keyword?<br />
when doing if (instance == null ) &#8211; read operation is performed.</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alan van Dam</title>
		<link>http://technology.amis.nl/2008/12/14/the-double-checked-locking-confusion/#comment-5631</link>
		<dc:creator>Alan van Dam</dc:creator>
		<pubDate>Wed, 17 Dec 2008 10:03:59 +0000</pubDate>
		<guid isPermaLink="false">http://technology.amis.nl/blog/?p=4384#comment-5631</guid>
		<description><![CDATA[@Emiel, Your right the synchronise will not only create a monitor but it will also create memory bariers. But the problem here is the first check for null on the instance variable. This check is outside the synchronized block. So whithout the volatile there are no guarantees that it thread A sees the same as thread B that just created the instance. By using the volatile the second thread also need to update its memory. For a better explanation read http://www.javaworld.com/jw-02-2001/jw-0209-double.html?page=1]]></description>
		<content:encoded><![CDATA[<p>@Emiel, Your right the synchronise will not only create a monitor but it will also create memory bariers. But the problem here is the first check for null on the instance variable. This check is outside the synchronized block. So whithout the volatile there are no guarantees that it thread A sees the same as thread B that just created the instance. By using the volatile the second thread also need to update its memory. For a better explanation read <a href="http://www.javaworld.com/jw-02-2001/jw-0209-double.html?page=1" rel="nofollow">http://www.javaworld.com/jw-02-2001/jw-0209-double.html?page=1</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Emiel Paasschens</title>
		<link>http://technology.amis.nl/2008/12/14/the-double-checked-locking-confusion/#comment-5630</link>
		<dc:creator>Emiel Paasschens</dc:creator>
		<pubDate>Tue, 16 Dec 2008 15:20:43 +0000</pubDate>
		<guid isPermaLink="false">http://technology.amis.nl/blog/?p=4384#comment-5630</guid>
		<description><![CDATA[Why is volatile needed in the last example?
The synchronised keyword not only locks the specific code block to be accessed by one thread, but it will also synchronise local (thread) caches of all variables.
See also http://www.javaperformancetuning.com/news/qotm030.shtml]]></description>
		<content:encoded><![CDATA[<p>Why is volatile needed in the last example?<br />
The synchronised keyword not only locks the specific code block to be accessed by one thread, but it will also synchronise local (thread) caches of all variables.<br />
See also <a href="http://www.javaperformancetuning.com/news/qotm030.shtml" rel="nofollow">http://www.javaperformancetuning.com/news/qotm030.shtml</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alan van Dam</title>
		<link>http://technology.amis.nl/2008/12/14/the-double-checked-locking-confusion/#comment-5629</link>
		<dc:creator>Alan van Dam</dc:creator>
		<pubDate>Mon, 15 Dec 2008 15:01:58 +0000</pubDate>
		<guid isPermaLink="false">http://technology.amis.nl/blog/?p=4384#comment-5629</guid>
		<description><![CDATA[@Jean-Francois, Peter: You are both right. They were static before but then I thought maybe is better to avoid having this singleton thing and don&#039;t be confusioning with more static keywords. But I forgot to rename my class Foo to Bar. Then again this looks a bit weird but I tried to get the focus on the real issue here.
I will fix this soon. Thanks for your comments.]]></description>
		<content:encoded><![CDATA[<p>@Jean-Francois, Peter: You are both right. They were static before but then I thought maybe is better to avoid having this singleton thing and don&#8217;t be confusioning with more static keywords. But I forgot to rename my class Foo to Bar. Then again this looks a bit weird but I tried to get the focus on the real issue here.<br />
I will fix this soon. Thanks for your comments.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jean-Francois Poilpret</title>
		<link>http://technology.amis.nl/2008/12/14/the-double-checked-locking-confusion/#comment-5628</link>
		<dc:creator>Jean-Francois Poilpret</dc:creator>
		<pubDate>Mon, 15 Dec 2008 10:44:43 +0000</pubDate>
		<guid isPermaLink="false">http://technology.amis.nl/blog/?p=4384#comment-5628</guid>
		<description><![CDATA[Hi, I think you have forgotten &quot;static&quot; for examples 3, 4, 5 and 6 above. Both &quot;instance&quot; and &quot;getInstance()&quot; should be static else your code is impossible to execute (considering that Foo constructor is private which is a must for a singleton.

It is a pity to take the poor old singleton pattern (which is bad, I hope everybody is convinced of this) as an example for the double checked locking pattern. There are better examples (that make sense in the real world) than the singleton that anyway is a bad example by itself.]]></description>
		<content:encoded><![CDATA[<p>Hi, I think you have forgotten &#8220;static&#8221; for examples 3, 4, 5 and 6 above. Both &#8220;instance&#8221; and &#8220;getInstance()&#8221; should be static else your code is impossible to execute (considering that Foo constructor is private which is a must for a singleton.</p>
<p>It is a pity to take the poor old singleton pattern (which is bad, I hope everybody is convinced of this) as an example for the double checked locking pattern. There are better examples (that make sense in the real world) than the singleton that anyway is a bad example by itself.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Veentjer</title>
		<link>http://technology.amis.nl/2008/12/14/the-double-checked-locking-confusion/#comment-5627</link>
		<dc:creator>Peter Veentjer</dc:creator>
		<pubDate>Mon, 15 Dec 2008 09:26:53 +0000</pubDate>
		<guid isPermaLink="false">http://technology.amis.nl/blog/?p=4384#comment-5627</guid>
		<description><![CDATA[You can forget my first reply.. The submit functionality is quite lame, so sorry for spamming :)]]></description>
		<content:encoded><![CDATA[<p>You can forget my first reply.. The submit functionality is quite lame, so sorry for spamming <img src='http://technology.amis.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Veentjer</title>
		<link>http://technology.amis.nl/2008/12/14/the-double-checked-locking-confusion/#comment-5626</link>
		<dc:creator>Peter Veentjer</dc:creator>
		<pubDate>Mon, 15 Dec 2008 09:25:28 +0000</pubDate>
		<guid isPermaLink="false">http://technology.amis.nl/blog/?p=4384#comment-5626</guid>
		<description><![CDATA[You have forgotten a few statics:

 public class Foo {
   private final Foo instance = new Foo();
 }

should be

public class Foo {
   private static final Foo instance = new Foo();
 }

The first example leads to a stackoverflow :)

But nice blogpost. Concurrency is a very hard topic to get right, and to be honest I&#039;m really questioning the approach Java (and other normal oo languages) are taking. Perhaps Software Transactional Memory could mean to modern languages like garbage collection meant to java.]]></description>
		<content:encoded><![CDATA[<p>You have forgotten a few statics:</p>
<p> public class Foo {<br />
   private final Foo instance = new Foo();<br />
 }</p>
<p>should be</p>
<p>public class Foo {<br />
   private static final Foo instance = new Foo();<br />
 }</p>
<p>The first example leads to a stackoverflow <img src='http://technology.amis.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>But nice blogpost. Concurrency is a very hard topic to get right, and to be honest I&#8217;m really questioning the approach Java (and other normal oo languages) are taking. Perhaps Software Transactional Memory could mean to modern languages like garbage collection meant to java.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben Manes</title>
		<link>http://technology.amis.nl/2008/12/14/the-double-checked-locking-confusion/#comment-5625</link>
		<dc:creator>Ben Manes</dc:creator>
		<pubDate>Mon, 15 Dec 2008 08:28:56 +0000</pubDate>
		<guid isPermaLink="false">http://technology.amis.nl/blog/?p=4384#comment-5625</guid>
		<description><![CDATA[In your singleton-like example, the best is to use class-loader synchronization.  I&#039;m glad this is finally becoming popular, because so few people I&#039;ve worked with would believe me until Josh started recommending it.  Use either a static inner class to hold the parent&#039;s instance or, if you prefer the newest style, use the enum trick.

For normal lazy initialization, the simplest approach (which is also quite efficent) is to use a FutureTask.  I&#039;m not sure why this isn&#039;t popular, perhaps it doesn&#039;t look magical enough to have the same coolness factor.  Regardless, simply wrap the initialization logic in a Callable, create a FutureTask with it, and execute run() before the get() call.  The first thread to call run() will change the task&#039;s state and execute, while the rest will no-op.  You can reuse this logic via a simple utility - mine is called a &quot;LazyFuture&quot;.  The resulting code is fast, simple, and provides the desired behavior in a less error-prone manner.  You also won&#039;t run into bad practices, such as synchronizing on &quot;this&quot;, because the library does it cleanly for you.  The only downside is that you don&#039;t get to act like such a hot-shot since the code isn&#039;t complex enough to brag about. ;)]]></description>
		<content:encoded><![CDATA[<p>In your singleton-like example, the best is to use class-loader synchronization.  I&#8217;m glad this is finally becoming popular, because so few people I&#8217;ve worked with would believe me until Josh started recommending it.  Use either a static inner class to hold the parent&#8217;s instance or, if you prefer the newest style, use the enum trick.</p>
<p>For normal lazy initialization, the simplest approach (which is also quite efficent) is to use a FutureTask.  I&#8217;m not sure why this isn&#8217;t popular, perhaps it doesn&#8217;t look magical enough to have the same coolness factor.  Regardless, simply wrap the initialization logic in a Callable, create a FutureTask with it, and execute run() before the get() call.  The first thread to call run() will change the task&#8217;s state and execute, while the rest will no-op.  You can reuse this logic via a simple utility &#8211; mine is called a &#8220;LazyFuture&#8221;.  The resulting code is fast, simple, and provides the desired behavior in a less error-prone manner.  You also won&#8217;t run into bad practices, such as synchronizing on &#8220;this&#8221;, because the library does it cleanly for you.  The only downside is that you don&#8217;t get to act like such a hot-shot since the code isn&#8217;t complex enough to brag about. <img src='http://technology.amis.nl/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>
