why is static inner class singleton thread safe?
http://en.wikipedia.org/wiki/Initialization_うむdemand_ホールディングスアイドル
When to use it Use this pattern if the initialization of the class is expensive and it cannot be done safely a at class-loading time andthe initialization is highly concurrent.The crux of the patteinininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininincにあります。lass-loading段階で不安全Since the class initialization phase is garanted by the JLS to be serialを初期化すれば、i.e.non-concurrent、no further synchronization is required in the static get Instance method during loring段階は連続的に初期化されます。
しかし、構造時にもdoub-checked locking問題が発生します。ある瞬間に引用が空ではなく、構造が未完成です。
from:http://topic.csdn.net/u/20110222/13/87f22039-9ef0-48d0-8270-c91516e5cee5.html
public class Singleton {
// Private constructor prevents instantiation from other classes
private Singleton() {
System.out.println("constructor");
}
/**
* SingletonHolder is loaded on the first execution of
* Singleton.getInstance() or the first access to SingletonHolder.INSTANCE,
* not before.
*/
private static class SingletonHolder {
public static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
public static void main(String[] args) {
System.out.println("main");
getInstance();
}
}
When to use it Use this pattern if the initialization of the class is expensive and it cannot be done safely a at class-loading time andthe initialization is highly concurrent.The crux of the patteinininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininincにあります。lass-loading段階で不安全Since the class initialization phase is garanted by the JLS to be serialを初期化すれば、i.e.non-concurrent、no further synchronization is required in the static get Instance method during loring段階は連続的に初期化されます。
しかし、構造時にもdoub-checked locking問題が発生します。ある瞬間に引用が空ではなく、構造が未完成です。
from:http://topic.csdn.net/u/20110222/13/87f22039-9ef0-48d0-8270-c91516e5cee5.html