• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread-safe lazy Singleton creation

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read somewhere a while back that you need to be careful when using Singletons. A simple piece of code to create Singelton would be:

In a threaded environment would it be better to use:
public static synchronized Singleton getInstance() {...}
Or is this overkill? Cheers, Neil
[modified getInstance to static]
[ August 23, 2002: Message edited by: Neil Laurance ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would certainly be better to synchronize the method - unless you don't mind working with partially initialized objects...
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Why even bother with lazy initialization (and therefore synchronization)? Why not just instantiate at the point of declaration:
private static Singleton singleton = new Singleton() ;
2. Shouldn't getInstance() be static?
 
Neil Laurance
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Matola:
2. Shouldn't getInstance() be static?


Oops. Yes.
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The volatile modifier would fix that particular threading issue, I believe.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David Weitzman:
The volatile modifier would fix that particular threading issue, I believe.


AFAIK it doesn't, as it only applies to the reference, not to the object itself.
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For discussion of attempts to work around the use of "synchronized", see The "Double-Checked Locking is Broken" Declaration, and its references.
(Or just listen to Ilja.)
[ August 23, 2002: Message edited by: John Dale ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic