Why synchronized in singleton
I will describe what I encounter to convince you it is really needed to do so.I have the
Class A
in the Thread-use
:Singleton s1 = Singleton.getInstance();
synchronized(s1){
while (!B.ready){
s1.wait();
}
// read from singleton
}
Then
Class B
in Thread-readData
:
// notice the volatile
static volatile boolean ready = false;
Singleton s2 = Singleton.getInstance();
synchronized(s2){
// read data than write to singleton
ready = true;
s2.notify();
}
Then Singleton
:public static Singleton getInstance() {
if(instance == null) {
instance = new Singleton();
}
return instance;
}
This
getInstance()
is not synchronized now. If the first thread enter the if
clause first, then the second thread get the cpu also enter it (if clause
) when the instance are still null
, so they create two Singleton
and return separately. As a result, the s1
and s2
point to the different objects which causes a deadlock, i.e. makes the wait
and notify
not working any more.Simple Synchronization
private static Singleton instance;
public static synchronized Singleton getInstance() {
if(instance == null) {
instance = new Singleton();
}
return instance;
}
// or
public static Singleton getInstance() {
synchronized (Singleton.class) {
if(instance == null) {
instance = new Singleton();
}
}
return instance;
}
More Solutions
You may complain that above solution always need to acquire lock to proceed which may be not very efficient. But And at SO1, there shows two more ways to do so.First is with the
enum
:public enum Singleton {
SINGLE;
public void myMethod(){
}
}
Another one is to utilize
classLoader
which is synchronized, so we can eliminate explicit synchronization.public class MySingleton {
private static class Loader {
static MySingleton INSTANCE = new MySingleton();
}
private MySingleton () {}
public static MySingleton getInstance() {
return Loader.INSTANCE;
}
}
Even though we don't need acquire lock here, we should notice that through many optimizations of JVM, we have reach the point when no contended lock is not so expensive and should not be deemed as a very evil demon to kill. Readbility is much more important.
A somewhat
complicated way -- double checked locking,
which should avoid to be used, because it's much more easier to write wrong than correctly. And we can always use previous provided way if we need lazy initialization and don't always to acquire lock first.
Written with StackEdit.
评论
发表评论