• 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

Eagerly created singleton design

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,


Has anybody else here read the above singleton design and if so, what are your thoughts Regarding this design in mulithreaded environment?will multiple different objects create or single object create by multiple threads?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please search this forum; there are questions about singletons several times a month. Also, I think this is too complicated for "beginning", so I shall move the thread.
 
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surya - normally you'd also provide a private constructor. Otherwise, it's easy for anyone to create their own instance.

You'd also normally want to provide some public way to obtain a reference to the SINGLE reference. Right now it's private, and no other code references it. Maybe you intend to use SINGLE inside onlyOneInstance(), but that method returns a void, so it can't make the reference available to the outside world. To be honest, I have no idea what onlyOneInstance() might do, as shown.

Aside from those issues...

surya.raaj prakash wrote:Has anybody else here read the above singleton design and if so, what are your thoughts Regarding this design in mulithreaded environment?


Yes, this is simpler than a lazy-loaded singleton, so people don't discuss it as much. But a properly-coded eagerly-loaded singleton can be quite thread-safe, yes.

surya.raaj prakash wrote:will multiple different objects create or single object create by multiple threads?


Um, if you make a private constructor (the only constructor), and if you provide a public method that returns the instance... then there should be only one instance, shared by all threads.

I'm going to skip all the usual anti-singleton rhetoric, as I expect someone will be along shortly to point you towards all that. I just wanted to address the specifics of your post.
 
pie. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic