Whats wrong in the code snippet below ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lifeisgreat20009
    New Member
    • Oct 2007
    • 70

    Whats wrong in the code snippet below ?

    (This is not a trick question, there is a potential real problem in using this code)

    Code:
    class Singleton{
             public static Singleton Instance() {
                      if (_instance == null)
                           _instance = new Singleton();
                      return _instance;
             }
             protected Singleton() {}
             private static Singleton _instance = null;
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Multiple Threads, each trying to obtain your 'singleton'. Synchronize that static method.

    kind regards,

    Jos

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by lifeisgreat2000 9
      (This is not a trick question, there is a potential real problem in using this code)

      Code:
      class Singleton{
               public static Singleton Instance() {
                        if (_instance == null)
                             _instance = new Singleton();
                        return _instance;
               }
               protected Singleton() {}
               private static Singleton _instance = null;
      }
      Actually no syntactical error with your code. Then how did you say that it's error?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by dmjpro
        Actually no syntactical error with your code. Then how did you say that it's error?
        It helps if you read the entire thread first before you hit the 'Submit Reply' button.

        kind regards,

        Jos

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          @Joash
          Sorry!! :(

          If i write the code like this...
          Code:
          public SingleTone{
          private static _instance = new SingleTone();
          public static getInstance(){
          return _instance;
          }
          }
          Then should i synchronize it?

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Why not find an article about the Singleton and read it?

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by dmjpro
              @Joash
              Sorry!! :(

              If i write the code like this...
              Code:
              public SingleTone{
              private static _instance = new SingleTone();
              public static getInstance(){
              return _instance;
              }
              }
              Then should i synchronize it?
              Think: a class is only loaded once by a classloader.

              kind regards,

              Jos

              ps. btw it's 'Singleton', not 'SingleTone'.

              Comment

              • N002213F
                New Member
                • Sep 2007
                • 38

                #8
                further to Jos' idea of synchronising the static method, the constructor should be private so that noone instantiates it except itself.

                Comment

                Working...