1 2 Previous Next 15 Replies Latest reply: Sep 3, 2010 8:40 AM by 800387 RSS

    Thread-safe Singleton

    843853
      Hi,

      I want to create a thread safe singleton class. But dont want to use the synchronized method or block. One way i could think of was initializing the object in static block. This way the instance will be created only once. But what if instance becomes null after some time. How will it get initialized again. Can anyone help me in creating a thread safe singleton class.
      Also i would really really appreciate if some one can point me to a good tutorial on design patters, I searched on google.. Found many.. But not finding any of them satisfying.

      Thanks
        • 1. Re: Thread-safe Singleton
          843853
          http://en.wikipedia.org/wiki/Singleton_pattern. I found this to be very clear and precise.
          • 2. Re: Thread-safe Singleton
            843853
            Use an Enum with one element. It's what Joshua Bloch would do.
            • 3. Re: Thread-safe Singleton
              796262
              learn_java5 wrote:
              I want to create a thread safe singleton class.
              Why?
              But dont want to use the synchronized method or block.
              Why not?
              But what if instance becomes null after some time.
              Why would it?
              How will it get initialized again.
              What?
              Also i would really really appreciate if some one can point me to a good tutorial on design patters, I searched on google.. Found many.. But not finding any of them satisfying.
              What about them isn't "satisfying"?
              • 4. Re: Thread-safe Singleton
                843853
                Look into the use of volatile instead of synchronized, however the cost of using volatile is comparable to that of synchronizing
                • 5. Re: Thread-safe Singleton
                  843853
                  I got a suggestion. When I try to prevent some object close the singleton, accidentally maybe, I use a structure to put token.

                  Every time I request the singleton, I put a token in a queue, and every time I try to close the singleton, first check if the queue is empty, if not, I do nothing.
                  • 6. Re: Thread-safe Singleton
                    jschellSomeoneStoleMyAlias
                    JustNerd wrote:
                    I got a suggestion. When I try to prevent some object close the singleton, accidentally maybe, I use a structure to put token.

                    Every time I request the singleton, I put a token in a queue, and every time I try to close the singleton, first check if the queue is empty, if not, I do nothing.
                    Description seems more than a little fuzzy.

                    From what I can tell however the idea sounds like a bad one.
                    • 7. Re: Thread-safe Singleton
                      843853
                      JustNerd wrote:
                      I got a suggestion. When I try to prevent some object close the singleton, accidentally maybe, I use a structure to put token.

                      Every time I request the singleton, I put a token in a queue, and every time I try to close the singleton, first check if the queue is empty, if not, I do nothing.
                      What do you mean by that; close the singleton?
                      • 8. Re: Thread-safe Singleton
                        800387
                        JustNerd wrote:
                        I got a suggestion. When I try to prevent some object close the singleton, accidentally maybe, I use a structure to put token.
                        As other have said, what does it mean to "close" a singleton? A singleton may have a method close() within it, but that has nothing to do with the pattern itself. The only time I ever think of a Singleton "closing" (which I take to mean being unavailable afterwards) is when the JVM exits, in which case nothing is available in your program, so what is the point?
                        Every time I request the singleton, I put a token in a queue, and every time I try to close the singleton, first check if the queue is empty, if not, I do nothing.
                        Again, you could have a Queue (or some other collection, or any number of other variables) in your singleton. But that has nothing to do with the pattern.

                        The following is only my surmise, but it sounds like you are using a singleton to manage some resource, such as a JDBC connection, and that is why you are referring to close() and checking a queue for something. But these are your specific requirements. The pattern itself is silent on this.

                        - Saish
                        • 9. Re: Thread-safe Singleton
                          843853
                          kilyas wrote:
                          Look into the use of volatile instead of synchronized, however the cost of using volatile is comparable to that of synchronizing
                          Hi kilyas,

                          Can you please explain in detail

                          Regards
                          Balaji

                          Edited by: Balu_ch on Sep 6, 2010 6:56 AM
                          • 10. Re: Thread-safe Singleton
                            843853
                            Balu_ch wrote:
                            kilyas wrote:
                            Look into the use of volatile instead of synchronized, however the cost of using volatile is comparable to that of synchronizing
                            Can you please explain in detail
                            Google can.

                            Google ( [java volatile vs synchronized|http://www.google.com/search?q=java+volatile+vs+synchronized] ).
                            • 11. Re: Thread-safe Singleton
                              843853
                              tschodt wrote:
                              Balu_ch wrote:
                              kilyas wrote:
                              Look into the use of volatile instead of synchronized, however the cost of using volatile is comparable to that of synchronizing
                              Can you please explain in detail
                              Google can.

                              Google ( [java volatile vs synchronized|http://www.google.com/search?q=java+volatile+vs+synchronized] ).
                              Hi, I think we need to use both (volatile and synchronized). Can some please explain how "volatile" alone can be used to ensure thread safe singleton? Below is the code taken from wikipedia
                              public class Singleton {
                                 // volatile is needed so that multiple thread can reconcile the instance
                                 // semantics for volatile changed in Java 5.
                                 private volatile static Singleton singleton;
                               
                                 private Singleton()
                                 {
                               
                                 }
                               
                                 // synchronized keyword has been removed from here
                                 public static Singleton getSingleton(){
                                   // needed because once there is singleton available no need to acquire
                                   // monitor again & again as it is costly
                                   if(singleton==null) {
                                     synchronized(Singleton.class){
                                        // this is needed if two threads are waiting at the monitor at the
                                        // time when singleton was getting instantiated
                                        if(singleton==null)
                                        singleton= new Singleton();
                                     }
                                  }
                                 return singleton;
                                }
                               
                              }
                              • 12. Re: Thread-safe Singleton
                                800387
                                Why are you even bothering with all this synchronization when you can use a static initializer and avoid all the issues?

                                - Saish
                                • 13. Re: Thread-safe Singleton
                                  843853
                                  Balu_ch wrote:
                                  I think we need to use both (volatile and synchronized).
                                  You do not need either. Just use an enum singleton...
                                  Also, I assume you know you can have a singleton instance per classloader unless you bind your singleton to some unique resource.
                                  • 14. Re: Thread-safe Singleton
                                    843853
                                    Thanks Saish and tschodt for you replies. I agree with both of your suggestions
                                    1 2 Previous Next