Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Singleton Class

 
Greenhorn
Posts: 26
  • 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 doubt with singleton class
Usually we all declare a singleton class as following only



Can any one tell me other possibilities of creating singleton class?
Problem with an above approach is instance is static variable
If two threads hits my getInstance() method at the same time, same sec
Then I will have two instances for sure…
So what’s the best way to create thread safe singleton class?
Another point is I hate to use the word ‘synchronized’ directly
Their should be some approach available?
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The easiest way would be to use eager instantiation, instead of the lazy approach where the first call to getInstance() is responsible for initializtion of the static instance member.
 
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first of all make the constructor private. Otherwise someone would create a sub-class from it and create multiple instances. The solution to your second problem is either to use eager instantiation or synchronize the getInstance method. The eager instantiation would work like this

 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And if you still want lazy evaluation, you can make the entire getInstance() method synchronized.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And if you want to prevent incurring the unneccesary synchronization overhead for each and every call to getInstance() you could leave the getInstance() method unsynchronized and use an initialization holder to still achieve lazy initialization. The same could be achieved using the double-checked locking approach, but that approach should ideally be avoided.
 
Marshal
Posts: 75690
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the beginner's forum, and it is probably best to explain new concepts fully.

Please explain double-checked locking; it used not to work, and used to be regarded as an error. That may no longer be true.
 
vinora kumar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks friends
 
Rob Spoor
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See http://www.ibm.com/developerworks/java/library/j-dcl.html for the double-checking locking mechanism.
 
Master Rancher
Posts: 4189
57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This may be the beginner forum, but I think the topic asked about is inherently more complex than is usually appropriate for this forum. Perhaps it should be moved.

The article Rob links to is a bit old. It explains the problems with double-checked locking prior to JDK 5. However in JDK 5 they improved the volatile keyword to make it a bit more useful, and now it is possible to reliably implement DCL in Java, if and only if you make your variable volatile.

More discussion of this and other approaches can be found here. Note that the comment by Allen Holub is from pretty old, and does not really apply any more. Volatile is significantly quicker than it used to be. You can also find more info here.
 
Campbell Ritchie
Marshal
Posts: 75690
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree with Mike S; once you get onto "volatile" you are well out of beginner's.
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys! I don't realize what's actually the difference between this:

# public class ClassicSingleton {
# private static ClassicSingleton instance = null;
# private ClassicSingleton() {
# // Exists only to defeat instantiation.
# }
# public static ClassicSingleton getInstance() {
# if(instance == null) {
# instance = new ClassicSingleton();
# }
# return instance;
# }
# }



and the eager instantiation:

# public class ClassicSingleton {
# private static ClassicSingleton instance = new ClassicSingleton();
# private ClassicSingleton() {
# // Exists only to defeat instantiation.
# }
# public static ClassicSingleton getInstance() {
# return instance;
# }
# }
 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:first of all make the constructor private. Otherwise someone would create a sub-class from it and create multiple instances. ...
[/code]

I think it would make more sense and explicit if we declare the class final.
 
Vadim Vararu
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The correct solution for concurrency problem is to syncronize getInstance() method and that's all. Why to reinvent the wheel?
 
I've never won anything before. Not even a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic