<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.0.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: One more look at Double-Checked Locking</title>
	<link>http://tech.puredanger.com/2007/06/15/double-checked-locking/</link>
	<description>Alex Miller's technical blog</description>
	<pubDate>Thu, 28 Aug 2008 03:14:04 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.2</generator>

	<item>
		<title>by: Erkin Kanlioglu</title>
		<link>http://tech.puredanger.com/2007/06/15/double-checked-locking/#comment-41905</link>
		<pubDate>Tue, 22 Apr 2008 14:20:49 +0000</pubDate>
		<guid>http://tech.puredanger.com/2007/06/15/double-checked-locking/#comment-41905</guid>
					<description>Hi Alex

Thanks for quick response. Definitely We can simulate partially constructed objects for collaborations (listeners, parent-child bidirectional relations).

Cheers</description>
		<content:encoded><![CDATA[<p>Hi Alex</p>
<p>Thanks for quick response. Definitely We can simulate partially constructed objects for collaborations (listeners, parent-child bidirectional relations).</p>
<p>Cheers
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Alex</title>
		<link>http://tech.puredanger.com/2007/06/15/double-checked-locking/#comment-41901</link>
		<pubDate>Tue, 22 Apr 2008 14:10:59 +0000</pubDate>
		<guid>http://tech.puredanger.com/2007/06/15/double-checked-locking/#comment-41901</guid>
					<description>@Erkin: Re-reading my original post, that may not be correct.  Even if it was, I think it's highly improbably that I could create an example showing it. :)  The one case where a partially constructed object could occur would be if the &quot;this&quot; reference escaped during the construction of the object.  The classic case for this is registering yourself as a listener with this during construction (for instance registering the singleton with some other singleton).  If you do that then another object will have access to the singleton's fields before they may have been set, creating a possible data race.  You can find more info on this if you search for things like &quot;safe publication&quot; or &quot;escape during construction&quot;.  Here's one &lt;a href=&quot;http://www.ibm.com/developerworks/java/library/j-jtp0618.html&quot; rel=&quot;nofollow&quot;&gt;excellent article&lt;/a&gt; on the topic from Brian Goetz, which is also covered in his book Java Concurrency in Practice.</description>
		<content:encoded><![CDATA[<p>@Erkin: Re-reading my original post, that may not be correct.  Even if it was, I think it&#8217;s highly improbably that I could create an example showing it. :)  The one case where a partially constructed object could occur would be if the &#8220;this&#8221; reference escaped during the construction of the object.  The classic case for this is registering yourself as a listener with this during construction (for instance registering the singleton with some other singleton).  If you do that then another object will have access to the singleton&#8217;s fields before they may have been set, creating a possible data race.  You can find more info on this if you search for things like &#8220;safe publication&#8221; or &#8220;escape during construction&#8221;.  Here&#8217;s one <a href="http://www.ibm.com/developerworks/java/library/j-jtp0618.html" rel="nofollow">excellent article</a> on the topic from Brian Goetz, which is also covered in his book Java Concurrency in Practice.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Erkin Kanlioglu</title>
		<link>http://tech.puredanger.com/2007/06/15/double-checked-locking/#comment-41895</link>
		<pubDate>Tue, 22 Apr 2008 13:37:14 +0000</pubDate>
		<guid>http://tech.puredanger.com/2007/06/15/double-checked-locking/#comment-41895</guid>
					<description>Hi Alex

Sorry talking about this topic after one year... I have a small request. Can you write an example code which shows that double checked locking might create partially singleton object ?

I'm asking this question because I have just read information below from jvm spec...

******Just before a reference to the newly created object is returned as the result*****, the indicated constructor is processed to initialize the new object using the following procedure:

   1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.
   2. If this constructor begins with an explicit constructor invocation of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.
   3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.
   4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5. (In some early implementations, the compiler incorrectly omitted the code to initialize a field if the field initializer expression was a constant expression whose value was equal to the default initialization value for its type.)
   5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally. 

What I understood from this is JVM will not return a reference which is not fully constructed.

Note: I read the book you mentioned. And There was no example about partially constructed object in case of double checked locking.</description>
		<content:encoded><![CDATA[<p>Hi Alex</p>
<p>Sorry talking about this topic after one year&#8230; I have a small request. Can you write an example code which shows that double checked locking might create partially singleton object ?</p>
<p>I&#8217;m asking this question because I have just read information below from jvm spec&#8230;</p>
<p>******Just before a reference to the newly created object is returned as the result*****, the indicated constructor is processed to initialize the new object using the following procedure:</p>
<p>   1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.<br />
   2. If this constructor begins with an explicit constructor invocation of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.<br />
   3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.<br />
   4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5. (In some early implementations, the compiler incorrectly omitted the code to initialize a field if the field initializer expression was a constant expression whose value was equal to the default initialization value for its type.)<br />
   5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally. </p>
<p>What I understood from this is JVM will not return a reference which is not fully constructed.</p>
<p>Note: I read the book you mentioned. And There was no example about partially constructed object in case of double checked locking.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: synchronized keyword in singleton - DZone Forums</title>
		<link>http://tech.puredanger.com/2007/06/15/double-checked-locking/#comment-30144</link>
		<pubDate>Sun, 02 Mar 2008 06:29:55 +0000</pubDate>
		<guid>http://tech.puredanger.com/2007/06/15/double-checked-locking/#comment-30144</guid>
					<description>[...] You may find my article on Singleton construction, double-checked locking, etc of interest.   Alex Miller - Pure Danger Tech [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] You may find my article on Singleton construction, double-checked locking, etc of interest.   Alex Miller - Pure Danger Tech [&#8230;]
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Alex</title>
		<link>http://tech.puredanger.com/2007/06/15/double-checked-locking/#comment-6789</link>
		<pubDate>Mon, 27 Aug 2007 19:18:32 +0000</pubDate>
		<guid>http://tech.puredanger.com/2007/06/15/double-checked-locking/#comment-6789</guid>
					<description>@Peter - I think in general usage you're right, however this is not true in all cases.  There are certain operations that can be performed on a singleton class (like calling a static method) that will cause the singleton to be loaded from a static variable where it will not be loaded from a static holder class.  Here's an example:

&lt;pre&gt;
public class InitTest {

  public static void main(String arg[]) {
    System.out.println(&quot;Force singleton class to load&quot;);
    Singleton.test();
   
    System.out.println(&quot;\ngetInstance1&quot;);
    Singleton.getStaticInstance();
    
    System.out.println(&quot;\ngetInstance2&quot;);
    Singleton.getHolderInstance();
  } 
}

class Singleton {
  static {
    System.out.println(&quot;Loading Singleton class&quot;);
  }
  
  private static Singleton s1 = new Singleton();
  
  private static class SingletonHolder {
    static final Singleton s2 = new Singleton();
  }
  
  Singleton() {
    System.out.println(&quot;Constructing Singleton&quot;);
  }
  
  public static Singleton getStaticInstance() {
    return s1;
  }
  
  public static Singleton getHolderInstance() {
    return SingletonHolder.s2;
  }
  
  public static void test() {
    // do nothing with s1 or s2
  }
}
&lt;/pre&gt;

which when run prints:

&lt;pre&gt;
Force singleton class to load
Loading Singleton class
Constructing Singleton

getInstance1

getInstance2
Constructing Singleton
&lt;/pre&gt;

As you can see, calling the static Singleton.test() method causes s1 to be loaded in the static variable, but s2 is not constructed until it is actually asked for, so the &quot;initialize on demand&quot; idiom is slightly lazier.  But, I admit that this scenario is probably not common and that most singleton classes have just a static getInstance() which will effectively behave the same with either a static variable or an &quot;initialize on demand&quot; class.</description>
		<content:encoded><![CDATA[<p>@Peter - I think in general usage you&#8217;re right, however this is not true in all cases.  There are certain operations that can be performed on a singleton class (like calling a static method) that will cause the singleton to be loaded from a static variable where it will not be loaded from a static holder class.  Here&#8217;s an example:</p>
<pre>
public class InitTest {

  public static void main(String arg[]) {
    System.out.println("Force singleton class to load");
    Singleton.test();

    System.out.println("ngetInstance1");
    Singleton.getStaticInstance();

    System.out.println("ngetInstance2");
    Singleton.getHolderInstance();
  }
}

class Singleton {
  static {
    System.out.println("Loading Singleton class");
  }

  private static Singleton s1 = new Singleton();

  private static class SingletonHolder {
    static final Singleton s2 = new Singleton();
  }

  Singleton() {
    System.out.println("Constructing Singleton");
  }

  public static Singleton getStaticInstance() {
    return s1;
  }

  public static Singleton getHolderInstance() {
    return SingletonHolder.s2;
  }

  public static void test() {
    // do nothing with s1 or s2
  }
}
</pre>
<p>which when run prints:</p>
<pre>
Force singleton class to load
Loading Singleton class
Constructing Singleton

getInstance1

getInstance2
Constructing Singleton
</pre>
<p>As you can see, calling the static Singleton.test() method causes s1 to be loaded in the static variable, but s2 is not constructed until it is actually asked for, so the &#8220;initialize on demand&#8221; idiom is slightly lazier.  But, I admit that this scenario is probably not common and that most singleton classes have just a static getInstance() which will effectively behave the same with either a static variable or an &#8220;initialize on demand&#8221; class.
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
