• 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

Implementing Singleton

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

I have a query regarding Singleton and multithreading...people use synchronised block to create single instance so that multithreading issues are handled.
But instead of using synchronised, we can create the instance when we declare variable itself. I guess this would avoid us to use synchronised.

Is my approach correct?
Code can be as:

Class A{

private static A obj = new A();

private A(){.....}

public static getInstance(){return obj;}

}
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

manish ghildiyal wrote:Is my approach correct?


In 99.999% of cases: yes. The only possible exception might be if the object in question takes an extraordinarily long time to create or consumes a vast amount of machine resources, in which case the initialization on demand idiom might be more appropriate.

But basically: do it the way you showed unless you have an overwhelming reason not to.

Winston
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

manish ghildiyal wrote:Is my approach correct?


In 99.999% of cases: yes. The only possible exception might be if the object in question takes an extraordinarily long time to create or consumes a vast amount of machine resources, in which case the initialization on demand idiom might be more appropriate.



I maintain that the number is 100%.

Using lazy instantiation is never necessary. Even if it takes a huge amount of time and resources to create, if you need it, you need it, and you'll have to bear that cost of creation. And because Java loads classes on demand anyway, you won't bear that cost until you're actually about to use the singleton anyway, so you won't be bearing it at program startup for something you never end up using. The only way you'd bear the cost unnecessarily is if you're using other static methods in that class, but not the singleton. And if you're doing that, then your design is broken anyway.

 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:The only way you'd bear the cost unnecessarily is if you're using other static methods in that class, but not the singleton. And if you're doing that, then your design is broken anyway.


Good point. And that's quite apart from whether you actually want singletons to start with.

@manish: Listen to Jeff. He speaketh not with forked tongue.

Winston
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic