Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Initialize-On-Demand idiom vs simple static initializer in Singleton implementation  RSS feed

 
Femi Byte
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the Initialize-On-Demand idiom really necessary when implementing a thread safe singleton using static initialization, or would a simple static declaration of the instance suffice?

Simple declaration of instance as static field:


vs


I ask this because Brian Goetz recommends the 1st approach in this article:

http://www.ibm.com/developerworks/java/library/j-dcl/index.html

while he suggests the latter in this article

http://www.ibm.com/developerworks/library/j-jtp03304/

Does the latter approach provide any benefits that the former doesn't?
 
Madhan Sundararajan Devaki
Ranch Hand
Posts: 312
Java MS IE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the article, the latter provides lazy initialisation.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't need on on demand initialisation then don't use it and do it simply. Its faster start up time vs a hit when you use it, both solutions fix the traditional singleton initialisation problem.

Your question is more about on demand vs not than threading, though obviously the articles you quote are all about how to achieve this simply.

To be honest real world it makes very little difference given the simple examples shown.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!