1)instance完全创建好了,另一个线程得到的还是null:用volatile解决
2)对象内存块地址赋给了instance,但对象还没完全construct完毕:先访问一下对象,再赋instance,
class Singleton {
private static volatile Singleton instance = null;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
Singleton ins = new Singleton();
/* ensure object has been fully constructed before assigned to instance
* rule: in a thread, construction of an object -> access to the object.
*/
ins.hashCode();
instance = ins;
}
}
}
return instance;
}
}