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?
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
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.
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.
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
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:
volatile
enum
instead of a class
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+.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.