• Post Reply Bookmark Topic Watch Topic
  • New Topic

Implementing Singleton  RSS feed

 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • 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;}

}
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!