Results 1 to 15 of 15
  1. #1
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Question Using Synchronized keyword in Singleton Design Pattern

    Below is a code of a singleton design pattern. Got some doubt mentioned at the bottom.

    public class SingleTon implements Cloneable
    {
    private SingleTon()
    {

    }

    private static SingleTon singleTonInstance;

    public static synchronized SingleTon getInstance()
    {

    if (singleTonInstance == null)
    singleTonInstance = new SingleTon();

    return singleTonInstance;

    }

    protected Object clone() throws CloneNotSupportedException
    {
    throw new CloneNotSupportedException();
    }
    }

    Statement : If getInstance() is called by thousand threads at a time and lets say getInstance() takes 1 sec for each execution; then it takes 1000 sec for all the threads to complete.
    Question : What could be the solution to this?

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

  3. #3
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Default

    Thanks. wz really helpfull.

  4. #4
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    hi r035198x,

    I got the solution. but still i have a doubt that how is the Bill Pugh approach a thread safe one. Could you please ellaborate?

    watining for your response
    cbani

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    As explained on wiki, the SingletonHolder class is only loaded on the very first call of getInstance. The instance is created on class loading. So creation of the instance though started by a thread by calling getInstance, is actually completed by the JVM when loading the SingletonHolder class.
    Last edited by r035198x; 01-08-2010 at 02:10 PM.

  6. #6
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Question

    As said : So creation of the instance though started by a thread by calling getInstance, is actually completed by the JVM when loading the SingletonHolder class.

    Now What I want to understand from the above statement is if two thread are calling getInstance at the same time wont it create 2 instances?

    If no then how exactly is it doing the same?

    Till now what i have understood from the wiki is the only use of inner class is just not create the instance at the class laoding time. instead created at the first call to getnstance.

    please correct me if i am going wrong anywhere.

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Two instances wont get created because the code fore creating the instance is in a static initializer of the class which is called by the JVM when the class is loaded. The class is only ever loaded once and the initialzer runs soon after (only) once. So the instance is not created on the calling thread but by the JVM. That's why I said that the creation is completed by the JVM not by the calling thread.

  8. #8
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Default

    Hi r035198x,

    One last question.

    So, instead of writing an inner class if i write in below way what would be the difference other than lazy loading?

    public class SingleTon
    {

    private static final Singleton INSTANCE = new Singleton();

    private SingleTon()
    {

    }

    private static SingleTon singleTonInstance;

    public static SingleTon getInstance()
    {

    return INSTANCE ;

    }

    }

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Your SingleTon class will be loaded and the instance created before any calls to getInstance are made at all. i.e Your solution does not have lazy initilization of the singleton since your SingleTon class is always loaded.

    More helpful links are

    Initialization on demand holder idiom - Wikipedia, the free encyclopedia

    Double-checked locking - Wikipedia, the free encyclopedia

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    To be honest, the only advantage of the lazy loading one comes if you have a Class with other static methods in it beyond the getInstance(), otherwise it's next to pointless.

  11. #11
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Smile

    Hi,

    thanks got it. :)

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by Tolls View Post
    To be honest, the only advantage of the lazy loading one comes if you have a Class with other static methods in it beyond the getInstance(), otherwise it's next to pointless.
    Added with the advantages of lazy loading itself ofcourse. Most singletons are so expensive to create that it's best not to create them expect if they are really required.

  13. #13
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Well, the class won't be loaded until the getInstance() is called anyway...in almost every circumstance I can think of. So the inner class gains you nothing, as far as I can see.

  14. #14
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by Tolls View Post
    Well, the class won't be loaded until the getInstance() is called anyway...in almost every circumstance I can think of. So the inner class gains you nothing, as far as I can see.
    But you don't want to design your class assuming that you won't need to add any more static methods.
    In JEE applications the Singleton might be loaded anyway even before getInstance is called due to its location.
    Last edited by r035198x; 01-08-2010 at 04:36 PM. Reason: each ->its

  15. #15
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Any singleton with static methods beyond getInstance() is probably a util class in disguise...and therefore not really correct.

    Never encountered a web app loading up stuff before it's actually used, I have to say. Then again, probably not looking all that closely to see...:)

Similar Threads

  1. using singleton pattern in java
    By anandjain1984 in forum New To Java
    Replies: 1
    Last Post: 12-16-2009, 02:57 PM
  2. Singleton Pattern
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-24-2008, 03:21 PM
  3. singleton pattern
    By Peter in forum Advanced Java
    Replies: 1
    Last Post: 07-09-2007, 04:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •