3

In some sites/blogs, I have found Singleton code as

public class MySingleton{

    private static MySingleton instance = null;
    .......
    .......
}

and in some other, I have found Singleton code as

public class MySingleton{

    private volatile static MySingleton instance = null;
    .......
    .......
}

Is it necessary to make MySingleton reference as volatile? Will it be of any significance?

Anand
  • 20,708
  • 48
  • 131
  • 198

5 Answers5

1

Volatile keyword in Java is used as an indicator to Java compiler and Thread that do not cache value of this variable and always read it from main memory

** Edit **

private volatile static MySingleton instance = null;

In above code snippet the volatile keyword ensures that multiple threads handle the instance variable correctly when it is being initialized to the MySingleton instance.

Read more: http://javarevisited.blogspot.com/2011/06/volatile-keyword-java-example-tutorial.html#ixzz2BYyA5Jnm

tokhi
  • 21,044
  • 23
  • 95
  • 105
0

Well, yes, if it is mutable and appears in multithreaded code. You just need to learn what volatile means, there's nothing special about whether a singleton will require its use.

djechlin
  • 59,258
  • 35
  • 162
  • 290
  • In case of multi-threaded environment, we synchronize the method or use synchronized block which returns the Singleton..so what is the need of volatile? – Anand Nov 07 '12 at 18:29
  • @djechlin if the getter that gives access to that field uses a form of synchronization, volatile is not required. – assylias Nov 07 '12 at 18:30
0

It is impossible to tell - the fields are private in both examples, so depending on how they are pusblished, you might or might not need them to be volatile. The standard idiom is to use an enum instead.

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
0

Either you want to keep things simple with an enum

public enum MySingleton {
    INSTANCE
}

Or you have a good reason to make it more complicated

public class MySingleton {
    private MySingleton() { }

    private static MySingleton instance = null;

    public synchronized static MySingleton getInstance() {
         if (instance == null)
             instance = new MySingleton();
         return instance;
    }
}

Either way, using volatile doesn't help.

IMHO, There isn't a good Singleton pattern where you would use volatile so if it is needed, the code need fixing or simplifying.


AFAIK the only case where you would use volatile is for double-checked locking

public class MySingleton {
    private MySingleton() { }

    private static volatile MySingleton instance = null;

    public static MySingleton getInstance() {
         if (instance == null)
             synchronzied(MySingleton.class) {
                 if (instance == null)
                     instance = new MySingleton();
             }
         return instance;
    }
}

For comparison, the enum example above does much the sample thing.

http://javarevisited.blogspot.co.uk/2011/06/volatile-keyword-java-example-tutorial.html

http://www.ibm.com/developerworks/java/library/j-dcl/index.html

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • *"There isn't a good pattern where you would use volatile"* is maybe a bit strong, unless you meant a good *singleton* pattern. – assylias Nov 07 '12 at 18:31
  • @assylias You are right, but of the many patterns for singleton, AFAIK only double-checked locking uses volatile and while it is no longer buggy it is needlessly complicated. – Peter Lawrey Nov 07 '12 at 18:33
  • I initially read it as don't use volatile (at all) - when you meant don't use volatile when writing a singleton. ;-) – assylias Nov 07 '12 at 18:34
  • 1
    @assylias There are uses for volatile, but not in most Singleton patterns. (e.g. a flag to stop a thread should be `volatile`) – Peter Lawrey Nov 07 '12 at 18:41
0

This is only necessary if you have a mutable singleton field in a multi-threaded system, particularly in situations where the singleton is lazily initialized.

This is part of a larger discussion of how to lazily initialize singletons in multi-threaded systems in Java. There are several mechanisms that are used for this, but the most popular are:

The currently accepted method of holding lazily initialized singletons for a multi-threaded system is the enum approach, though all 4 of them work in Java 1.5+.

Community
  • 1
  • 1
Brian
  • 17,079
  • 6
  • 43
  • 66
  • For lazy initialization? Can you cite that? Most IDE's use the holder idiom for their singleton templates. – Brian Nov 07 '12 at 18:37
  • The singleton won't be initialised until the enum class is loaded. See for example: http://stackoverflow.com/a/7790749/829571 or this: http://stackoverflow.com/a/3635619/829571 – assylias Nov 07 '12 at 18:40
  • 1
    Fair enough, I just have one concern. What about `public static` variables or methods, independent of the singleton's initialization, that are in the singleton's class? When accessing a static method or field of the enum, the instance would be loaded, which wouldn't happen with the holder idiom, which *only* initializes when `getInstance` is called. They'd have to be refactored into a separate class (which isn't necessarily a bad thing). Beyond that, I definitely see your point, and I'll update my answer and add this pattern to my toolbelt, thanks :) – Brian Nov 07 '12 at 18:48