Topic
6 replies Latest Post - ‏2007-09-20T03:26:23Z by SystemAdmin
SystemAdmin
SystemAdmin
757 Posts
ACCEPTED ANSWER

Pinned topic Implementing a Singleton with a volatile instance instead of synchonization

‏2007-09-01T01:14:41Z |
The standard Singleton implementation (in Java) is something like:

code
private static MySingleton _theSingleton = null;
private MySingleton(){}
public static MySingleton getInstance(){
if (_theSingleton == null){
synchronized(MySingleton.class){
_theSingletong = new MySingleton();
}
}
return _theSingleton;
}
[/code]

After reading the 19 Jun 2007 article "Java theory and practice: Managing volatility", I was left wondering whether the code below would make a better (simpler) implementation of a Singleton in Java:

code
private static [b]volatile[/b] MySingleton _theSingleton = null;
private MySingleton(){}
public static MySingleton getInstance(){
if (_theSingleton == null){
_theSingletong = new MySingleton();
}
return _theSingleton;
}
[/code]

This implements the [b]one-time publication[/b] pattern described in the article and eliminates the need for synchronization, since we are guaranteed to see the most updated value of _theSingleton -the only one we want.
No?
Updated on 2007-09-20T03:26:23Z at 2007-09-20T03:26:23Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    757 Posts
    ACCEPTED ANSWER

    Re: Implementing a Singleton with a volatile instance instead of synchonization

    ‏2007-09-01T01:38:38Z  in response to SystemAdmin
    Hmmm, ignore my post above. It is obvious we need synchronization to guarantee only one instance at any time and volatile in this scenario doesn't buy us anything.

    Cheers
    j
  • SystemAdmin
    SystemAdmin
    757 Posts
    ACCEPTED ANSWER

    Re: Implementing a Singleton with a volatile instance instead of synchonization

    ‏2007-09-01T04:30:12Z  in response to SystemAdmin
    The synchronized block in your first example must start before you say if (_theSingleton == null).
    • SystemAdmin
      SystemAdmin
      757 Posts
      ACCEPTED ANSWER

      Re: Implementing a Singleton with a volatile instance instead of synchonization

      ‏2007-09-03T00:15:01Z  in response to SystemAdmin
      How does this compare to the singleton approach:

      private static SomeClass instance = new SomeClass();

      public static SomeClass getInstance() {
      return instance;
      }
      • SystemAdmin
        SystemAdmin
        757 Posts
        ACCEPTED ANSWER

        Re: Implementing a Singleton with a volatile instance instead of synchonization

        ‏2007-09-07T11:40:51Z  in response to SystemAdmin
        It certainly looks concise and neat :)
        It seems to me that in the "traditional" version the Singleton class is only instantiated on demand, which is not the case in yours right? The member instantiation "arguably" wastes memory.
        • SystemAdmin
          SystemAdmin
          757 Posts
          ACCEPTED ANSWER

          Re: Implementing a Singleton with a volatile instance instead of synchonization

          ‏2007-09-10T18:30:03Z  in response to SystemAdmin
          Please ignore my comments above -your implementation is perfect! I wonder why we so often see the lazy instantiation version published as "the reference" (talk about myths that spread)
          Thanks for that!

          Cheers
          j
          • SystemAdmin
            SystemAdmin
            757 Posts
            ACCEPTED ANSWER

            Re: Implementing a Singleton with a volatile instance instead of synchonization

            ‏2007-09-20T03:26:23Z  in response to SystemAdmin
            Beware that you may be unnessicarily initialising the singleton on reference to any other public static members, as this is done on class load.

            so depending on the class, you can use either method.
            each has its pros/cons, with performance as well.