<?xml version="1.0"?><rss version="2.0">
<channel>
  <title>Java Beans dot Asia - threads category</title>
  <link>http://javabeans.asia/categories/java/threads/</link>
  <description>Just a few simple tutorials</description>
  <language>en</language>
  <copyright>Alexander Zagniotov</copyright>
  <lastBuildDate>Tue, 23 Feb 2010 11:04:00 GMT</lastBuildDate>
  <generator>Pebble (http://pebble.sourceforge.net)</generator>
  <docs>http://backend.userland.com/rss</docs>
  <image>
    <url>/images/blog-image.jpg</url>
    <title>Java Beans dot Asia (threads category)</title>
    <link>http://javabeans.asia/</link>
  </image>
  <item>
    <title>Brainteaser: Hidden iterators</title>
    <link>http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html</link>
	 <description>
        &lt;blockquote&gt;... While locking can prevent iterators from throwing &lt;em&gt;ConcurrentMofdificationException&lt;/em&gt;, You have to remember to use locking everywhere a shared collection might be iterated. This is trickier than it sounds ...&lt;br /&gt;
&lt;/blockquote&gt;&lt;a href=&#034;http://jcip.net/&#034; target=&#034;_blank&#034;&gt;Brian Goetz&lt;/a&gt; p.83-84 &lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Question&lt;/strong&gt;: The following code could throw &lt;em&gt;ConcurrentMofdificationException&lt;/em&gt;, even though &lt;em&gt;add()&lt;/em&gt; is synchronized, why?&lt;br /&gt;
&lt;pre name=&#034;code&#034; class=&#034;java:firstline[1]&#034;&gt;public class HiddenIterator  {&lt;br /&gt;	private final Set&amp;lt;Integer&amp;gt; set &lt;br /&gt;			= new HashSet&amp;lt;Integer&amp;gt;();&lt;br /&gt;&lt;br /&gt;   public synchronized void add(Integer i) {&lt;br /&gt;	set.add(i);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public synchronized void remove(Integer i) {&lt;br /&gt;	set.remove(i);&lt;br /&gt;   }&lt;br /&gt;	&lt;br /&gt;   public void addTenThings() {&lt;br /&gt;	Random r = new Random();&lt;br /&gt;	for (int index = 0; index &amp;lt; 10;  index++;) {&lt;br /&gt;		add(r.nextInt());&lt;br /&gt;	}&lt;br /&gt;   System.out.println(&amp;quot;Added ten elements to set: &amp;quot; + set);&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
Looking forward for your answers dear readers&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;t=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;t=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;b&gt;Related Posts&lt;/b&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/08/22/brainteaser_drools_testing_objects.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser Drools: Testing Objects&#034;&gt;Brainteaser Drools: Testing Objects&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Broken case of inheritance&#034;&gt;Brainteaser: Broken case of inheritance&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Overridable methods &#034;&gt;Brainteaser: Overridable methods &lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&#034; rel=&#034;bookmark&#034; title=&#034;Java and those frameworks&#034;&gt;Java and those frameworks&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/01/brainteaser_arraylist_vs_treeset.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: ArrayList VS TreeSet&#034;&gt;Brainteaser: ArrayList VS TreeSet&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&#034; rel=&#034;bookmark&#034; title=&#034;How to set SecurityManager and Java security policy programmatically&#034;&gt;How to set SecurityManager and Java security policy programmatically&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&#034; rel=&#034;bookmark&#034; title=&#034;Hack any Java class using reflection attack&#034;&gt;Hack any Java class using reflection attack&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Broken comparator&#034;&gt;Brainteaser: Broken comparator&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;</description>
	<!--
    <description>
          ... While locking can prevent iterators from throwing ConcurrentMofdificat...&lt;p&gt;&lt;a href=&#034;http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&#034;&gt;Read more...&lt;/a&gt;&lt;/p&gt;&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;t=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;title=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&amp;amp;t=Brainteaser: Hidden iterators&#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;</description>
      
	-->
    <category>brainteaser</category>
    <category>design patterns</category>
    <category>threads</category>
    <comments>http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html#comments</comments>
    <guid isPermaLink="true">http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html</guid>
    <pubDate>Sun, 07 Sep 2008 08:05:00 GMT</pubDate>
  </item>
  <item>
    <title>Singleton pattern and problem with double checked locking </title>
    <link>http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html</link>
	 <description>
        There are several ways to initialize singleton object. Some are thread safe and some are not. Until the last two days I thought that double-checked locking idiom offers the best solution for singleton initialization in multi threaded environment. Well its not.&lt;br /&gt;
&lt;br /&gt;
To begin, I want to describe briefly several ways to initialize singleton:&lt;br /&gt;
&lt;ul&gt;
    &lt;li&gt;Lazy initialization&lt;br /&gt;
    &lt;/li&gt;
    &lt;li&gt;Eager initialization&lt;/li&gt;
    &lt;li&gt;DCL&lt;/li&gt;
    &lt;li&gt;Class holder lazy initialization&lt;/li&gt;
    &lt;li&gt;Enum singleton&lt;/li&gt;
&lt;/ul&gt;
&lt;strong&gt;Lazy initialization&lt;/strong&gt;&lt;br /&gt;
The goal of lazy initialization was to defer initialization of an object until it is actually needed, while making sure that initialization is done only once. Lazy initialization example illustrated below:
&lt;pre name=&#034;code&#034; class=&#034;java:firstline[1]&#034;&gt;public class Singleton&lt;br /&gt;{&lt;br /&gt;  private static Singleton instance = null;&lt;br /&gt;  &lt;br /&gt;  private Singleton() {&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public static Singleton getInstance() {&lt;br /&gt;    if (instance == null)  {&lt;br /&gt;      instance = new Singleton();&lt;br /&gt;     }&lt;br /&gt;    return instance;        &lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
This will work fine in single-threaded environment. Unfortunately since &lt;em&gt;getInstance&lt;/em&gt;() is not synchronized, two different instances of the object can be returned if two threads will access &lt;em&gt;getInstance&lt;/em&gt;() method concurrently. Which makes this lazy initialization unsafe in multi threaded environment.&lt;br /&gt;
&lt;br /&gt;
Unsafe lazy initialization can be fixed by making &lt;em&gt;getInstance&lt;/em&gt;() synchronized:
&lt;pre name=&#034;code&#034; class=&#034;java:firstline[1]&#034;&gt;public class Singleton&lt;br /&gt;{&lt;br /&gt;  private static Singleton instance = null;&lt;br /&gt;  &lt;br /&gt;  private Singleton() {&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public static synchronized Singleton getInstance() {&lt;br /&gt;    if (instance == null)  {&lt;br /&gt;      instance = new Singleton();&lt;br /&gt;     }&lt;br /&gt;    return instance;        &lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
However, in this case there is performance hit for every invocation of &lt;em&gt;getInstance&lt;/em&gt;() method. Synchronized methods runs very slow compared to unsynchronized methods. I haven&#039;t tested this issue my self, so i cannot tell by how much slower.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Eager initialization&lt;/strong&gt;&lt;br /&gt;
By using eager initialization, the synchronization cost incurred for each method call is eliminated:
&lt;pre name=&#034;code&#034; class=&#034;java:firstline[1]&#034;&gt;public class Singleton&lt;br /&gt;{&lt;br /&gt;  private static Singleton instance = new Singleton();&lt;br /&gt;  &lt;br /&gt;  private Singleton() {&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public static Singleton getInstance() {&lt;br /&gt;    return instance;        &lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
The problem with eager initialization that &lt;em&gt;Singleton &lt;/em&gt;object is instantiated regardless whether it is going to be used or not.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;DCL&lt;/strong&gt;&lt;br /&gt;
Double-checked locking (DCL) idiom was created to allow lazy initialization of a singleton object, without performance reducing costs as a result of synchronization. Unfortunately it does not work. Lets have a look why:
&lt;pre name=&#034;code&#034; class=&#034;java:firstline[1]&#034;&gt;public class Singleton {&lt;br /&gt;  &lt;br /&gt;  private static Singleton instance = null;&lt;br /&gt;  &lt;br /&gt;  private Singleton() {&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public static Singleton getInstance() {&lt;br /&gt;    if (instance == null)  {&lt;br /&gt;        synchronized(Singleton.class)  {&lt;br /&gt;           if (instance == null)  {&lt;br /&gt;      		instance = new Singleton();&lt;br /&gt;     	  }           &lt;br /&gt;        } &lt;br /&gt;      }&lt;br /&gt;    return instance;&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
First there is a check whether initialization is needed with out synchronization, and if the &lt;em&gt;instance&lt;/em&gt; is not null - then use it.&lt;br /&gt;
Otherwise, synchronize and check again if &lt;em&gt;instance&lt;/em&gt; is null by making sure that only one thread will initialize the shared &lt;em&gt;Singleton&lt;/em&gt;.&lt;br /&gt;
&lt;br /&gt;
The problem with DCL is that code of fetching reference to constructed &lt;em&gt;Singleton &lt;/em&gt;object&lt;em&gt; &lt;/em&gt;does not require synchronization. &lt;br /&gt;
Lets say that thread &#039;a&#039; has entered synchronized block, made the &lt;em&gt;instance&lt;/em&gt; not null&amp;nbsp; but hasn&#039;t executed the constructor yet. At this stage thread &#039;b&#039; preempts thread &#039;a&#039;. Thread &#039;b&#039; checks if the &lt;em&gt;instance&lt;/em&gt; is not null, and since its not, it returns reference to a partially constructed &lt;em&gt;Singleton &lt;/em&gt;object. &lt;br /&gt;
&lt;br /&gt;
In other words - thread &#039;b&#039; sees a current value of the &lt;em&gt;Singleton &lt;/em&gt;object reference instead of its stale value, which causes the object to be seen in invalid or incorrect state.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Class holder lazy initialization&lt;/strong&gt;&lt;br /&gt;
Instead of DCL, lazy initialization class holder idiom offers better solution for singleton initialization in multi threaded environment, with the same benefits as DCL:
&lt;pre name=&#034;code&#034; class=&#034;java:firstline[1]&#034;&gt;public class SingletonFactory {&lt;br /&gt;  &lt;br /&gt;  private static class SingletonHolder {&lt;br /&gt;     public static Singleton instance = new Singleton();&lt;br /&gt;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;  public static Singleton getInstance()  {&lt;br /&gt;     return SingletonHolder.instance;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
The &lt;em&gt;SingletonHolder &lt;/em&gt;class initialization is deferred until its actually used, and because &lt;em&gt;Singleton &lt;/em&gt;is initialized with static initializer, no additional synchronization is needed. &lt;br /&gt;
The first invocation of&amp;nbsp; &lt;em&gt;getInstance&lt;/em&gt;() by any thread causes &lt;em&gt;SingletonHolder &lt;/em&gt;to be loaded and initialized during which time the &lt;em&gt;Singleton &lt;/em&gt;is initialized through static initializer.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Enum singleton&lt;/strong&gt;&lt;br /&gt;
Joshua Bloch in his book &amp;quot;&lt;a href=&#034;http://www.sun.com/books/catalog/effective_java.xml&#034; target=&#034;_blank&#034;&gt;Effective Java&lt;/a&gt;&amp;quot; offers a new interesting solution to initialize a Singleton by using Enum with a single type:
&lt;pre name=&#034;code&#034; class=&#034;java:firstline[1]&#034;&gt;public class SingletonEnum {&lt;br /&gt;  INSTANCE;&lt;br /&gt;  &lt;br /&gt;  public void someMethod() {&lt;br /&gt;     ...&lt;br /&gt;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;  public void anotherMethod() {&lt;br /&gt;     ...&lt;br /&gt;&amp;nbsp; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
Joshua claims: &lt;blockquote&gt; &amp;quot;...a single-element enum type is the best way to implement a singleton...&amp;quot; &lt;/blockquote&gt; Some people may argue against this approach by saying that this is not a class, but an enum - it enumarates plus enums cannot be subclassed. Well in case of a singleton there is no point in subclassing, since singletons have private constructors and singletons dont really ment to be subclassed. Lets have a look if Enum singleton is indeed the best solution so far:&lt;br /&gt;
&lt;br /&gt;
1. Enum singleton is final&lt;br /&gt;
2. It is serializable&lt;br /&gt;
3. It is a single instance in multithreaded environment &lt;br /&gt;
4. Does not allow invokation of private constructors through reflection attacks&lt;br /&gt;
&lt;br /&gt;
I think its quite original solution.&lt;br /&gt;
&lt;br /&gt;
I will appreciate your comments regarding this post. Cheers.&lt;br /&gt;
&lt;br /&gt;
Resources:&lt;br /&gt;
&lt;em&gt;&lt;a href=&#034;http://www.sun.com/books/catalog/effective_java.xml&#034; target=&#034;_blank&#034;&gt;Effective Java&lt;/a&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;a href=&#034;http://jcip.net/&#034; target=&#034;_blank&#034;&gt;Java Concurrency in Practice&lt;/a&gt;&lt;br /&gt;
&lt;a href=&#034;http://www.ibm.com/developerworks/java/library/j-dcl.html&#034; target=&#034;_blank&#034;&gt;Double-checked locking and the Singleton pattern&lt;/a&gt;&lt;/em&gt;&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;t=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;t=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;b&gt;Related Posts&lt;/b&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/04/05/brainteaser_broken_case_of_inheritance.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Broken case of inheritance&#034;&gt;Brainteaser: Broken case of inheritance&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2009/04/18/brainteaser_overridable_methods.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Overridable methods &#034;&gt;Brainteaser: Overridable methods &lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/11/15/java_and_those_frameworks.html&#034; rel=&#034;bookmark&#034; title=&#034;Java and those frameworks&#034;&gt;Java and those frameworks&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/01/brainteaser_arraylist_vs_treeset.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: ArrayList VS TreeSet&#034;&gt;Brainteaser: ArrayList VS TreeSet&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/12/how_to_set_securitymanager_and_java_security_policy_programmatically.html&#034; rel=&#034;bookmark&#034; title=&#034;How to set SecurityManager and Java security policy programmatically&#034;&gt;How to set SecurityManager and Java security policy programmatically&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/13/hack_any_java_class_using_reflection_attack.html&#034; rel=&#034;bookmark&#034; title=&#034;Hack any Java class using reflection attack&#034;&gt;Hack any Java class using reflection attack&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/10/19/brainteaser_broken_comparator.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Broken comparator&#034;&gt;Brainteaser: Broken comparator&lt;/a&gt;&lt;br /&gt;&lt;a href=&#034;http://javabeans.asia/2008/09/07/brainteaser_hidden_iterators.html&#034; rel=&#034;bookmark&#034; title=&#034;Brainteaser: Hidden iterators&#034;&gt;Brainteaser: Hidden iterators&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;</description>
	<!--
    <description>
          There are several ways to initialize singleton object. Some are thread safe and some are not. Until the last two days I thought that double-checked locking idiom offers the best solution for singleton initialization in multi threaded environment. Well its not.  To begin, I want to describe briefly several ways to ...&lt;p&gt;&lt;a href=&#034;http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&#034;&gt;Read more...&lt;/a&gt;&lt;/p&gt;&lt;div class=&#034;tags&#034;&gt;&lt;span&gt;Social Bookmarks : &lt;/span&gt;&amp;nbsp;&lt;a href=&#034;http://slashdot.org/bookmark.pl?url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Slash Dot&#034;&gt;&lt;img src=&#034;common/images/slashdot.png&#034; alt=&#034;Add this post to Slashdot&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://digg.com/submit?url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Digg this post&#034;&gt;&lt;img src=&#034;common/images/digg.png&#034; alt=&#034;Add this post to Digg&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://reddit.com/submit?url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Reddit&#034;&gt;&lt;img src=&#034;common/images/reddit.png&#034; alt=&#034;Add this post to Reddit&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://del.icio.us/post?url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Save this post to Del.icio.us&#034;&gt;&lt;img src=&#034;common/images/delicious.png&#034; alt=&#034;Add this post to Delicious&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.stumbleupon.com/submit?url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Stumble this post&#034;&gt;&lt;img src=&#034;common/images/stumbleupon.png&#034; alt=&#034;Add this post to Stumble it&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.google.com/bookmarks/mark?op=edit&amp;amp;bkmk=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Google&#034;&gt;&lt;img src=&#034;common/images/google.png&#034; alt=&#034;Add this post to Google&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://technorati.com/faves?add=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Technorati&#034;&gt;&lt;img src=&#034;common/images/technorati.png&#034; alt=&#034;Add this post to Technorati&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.bloglines.com/sub/http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Bloglines&#034;&gt;&lt;img src=&#034;common/images/bloglines.png&#034; alt=&#034;Add this post to Bloglines&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.facebook.com/share.php?u=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&#034; target=&#034;_blank&#034; title=&#034;Add this post to Facebook&#034;&gt;&lt;img src=&#034;common/images/facebook.png&#034; alt=&#034;Add this post to Facebook&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://www.furl.net/storeIt.jsp?u=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;t=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Furl&#034;&gt;&lt;img src=&#034;common/images/furl.png&#034; alt=&#034;Add this post to Furl&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;amp;url=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;title=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Windows Live&#034;&gt;&lt;img src=&#034;common/images/windowslive.png&#034; alt=&#034;Add this post to Windows Live&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a href=&#034;http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;amp;u=http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html&amp;amp;t=Singleton pattern and problem with double checked locking &#034; target=&#034;_blank&#034; title=&#034;Add this post to Yahoo!&#034;&gt;&lt;img src=&#034;common/images/yahoo.png&#034; alt=&#034;Add this post to Yahoo!&#034; border=&#034;0&#034; /&gt;&lt;/a&gt;&lt;/div&gt;</description>
      
	-->
    <category>java</category>
    <category>design patterns</category>
    <category>threads</category>
    <comments>http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html#comments</comments>
    <guid isPermaLink="true">http://javabeans.asia/2008/09/06/singleton_pattern_and_problem_with_double_checked_locking.html</guid>
    <pubDate>Sat, 06 Sep 2008 02:56:00 GMT</pubDate>
  </item>
  </channel>
</rss>
