which is correct SIngleton thread safe implementation?

Hi all,

Does the following implementation represent a perfect thread safe implementation?

public class Singleton {

private volatile static Singleton test1;

// Private constructor suppresses generation of a (public) default//constructor

private Singleton() {

}

public static Singleton getInstance() {

if(test1 == null) {

// Not guaranteed to work

synchronized (Singleton.class) {

if(test1 == null) {

test1 = new Singleton();

}

}

}

return test1;

}

}

I have read that ( Ref: http://en.wikipedia.org/wiki/Singleton_pattern) that above implementation is wrong. However the wiki page example missing

The Volatile key word that I use.

i.e private volatile static Singleton test1;

Is it wrong with missing volatile key word only or even with volatile key word is it a wrong implementation?

If the code given in my question is correct, is it correct only with J2SE 5.0 (i.e not working correctly with prior versions of JDK ?

Regards,

JavaTouch

http://javatouch.googlepages.com/home

Message was edited by:

javatouch

[1185 byte] By [javatoucha] at [2007-10-3 7:28:11]
# 1

// No lazy instantiation. Sufficient for almost all cases.

public final class Single {

private static final Single instance = new Single();

private Single() {}

public static final Single instance() {

return instance;

}

}

OR

// Lazy instantiation, synchronized instance() method

public final class Single {

private static final Single instance;

private Single() {}

public static final synchrronized Single instance() {

if (instance == null) {

instance = new Single();

}

return instance;

}

}

OR

// lazy instantiation, no synced instance() method

public final class Single {

private Single() {}

public static final Single instance() {

return Nested.instance;

}

public static final class Nested {

private static final Single instance = new Single();

}

}

With the new Memory Model in 5.0, DCL can be made threadsafe, but there's still no real benefit to using DCL anyway, so I'd avoid it. I'm not going to read your unformatted code to determine if it's threadsafe.

jverda at 2007-7-15 2:27:19 > top of Java-index,Other Topics,Patterns & OO Design...
# 2
Hi Jevrd,Thanks for giving the correct thread safe examples....Sorry that my code was not properly formatted...-JavaTouch
javatoucha at 2007-7-15 2:27:19 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

Hi All,

I just need to know following implementation is threadsafe. (I took this example from HEad first design patterns book). I am asking this becaus I have heard that Double-checked locking is not guaranteed to work in Java.

Does this 100% reliable thread safe implementation?

public class Singleton {

private volatile static Singleton uniqueInstance;

/* Private constructor suppresses generation of a (public) default

constructor */

private Singleton() {

}

public static Singleton getInstance() {

if(uniqueInstance== null) {

synchronized (Singleton.class) {

if(uniqueInstance== null) {

uniqueInstance= new Singleton();

}

}

}

return uniqueInstance;

}

}

Thanks

JavaTouch

javatoucha at 2007-7-15 2:27:19 > top of Java-index,Other Topics,Patterns & OO Design...
# 4
> Does this 100% reliable thread safe implementation?NODefinitely not in VM's < 1.5
YoGeea at 2007-7-15 2:27:19 > top of Java-index,Other Topics,Patterns & OO Design...
# 5
What is this buying you anyway? The only justification for DCL of a Singleton that I can see is when you aren't sure it'll ever be used and instantiation is expensive. Otherwise, this always feels like cleverness for its own sake to me.%
duffymoa at 2007-7-15 2:27:19 > top of Java-index,Other Topics,Patterns & OO Design...
# 6
I think the above DCL is safe on Java >= 5.0. I know there's some safe DCL implementation, but I'm not completely sure if that's it or if you have to do it a little differently.As % says, though, it really is pointless.
jverda at 2007-7-15 2:27:19 > top of Java-index,Other Topics,Patterns & OO Design...
# 7
Thanks all..Now I am clear.. My conclusion from the replies is that I can use the DCL solution safely in java >=5.0 Still the other thread safe solutions mentioned are effective and serve well in earlier platforms as well and therefore prefered.JavaTouch
javatoucha at 2007-7-15 2:27:19 > top of Java-index,Other Topics,Patterns & OO Design...
# 8

> My conclusion from the replies is that I can use the

> DCL solution safely in java >=5.0

You can, BUT

* I'm not sure if the particular implementation you have will work. Think so, but not sure.

* There's NO good reason to use it. None. Zero. Nada. Zilch. Bupkus.

> Still the other thread safe solutions mentioned are

> effective and serve well in earlier platforms as well

> and therefore prefered.

They're preferred even in >= 5.0. They're preferred even if the earlier JMM never broke DCL in the first place.

jverda at 2007-7-15 2:27:19 > top of Java-index,Other Topics,Patterns & OO Design...
# 9

Here's the proper way to write a Singleton:

public class Singleton

{

private static final Singleton instance = new Singleton();

private Singleton(); {}

public static final Singleton getInstance() { return instance; }

}

No DCL needed. Depending on the character of the rest of the data members, absolutely thread safe.

%

duffymoa at 2007-7-15 2:27:19 > top of Java-index,Other Topics,Patterns & OO Design...
# 10
I see that jverd beat me to it by several days. It bears repeating, though.%
duffymoa at 2007-7-15 2:27:19 > top of Java-index,Other Topics,Patterns & OO Design...