This is a followup from Java double checked locking.
The following code snippet has 2 interesting characteristics.
1) It requires a call to a separate init() method before the object is ready for use. So volatile doesn't help (I know, why don't I just put the code in init() into the constructor? It's here for the purposes of illustration).
2) It uses a tmp variable to do the initialization and assigns to instance after initialization is complete.
if (instance == null) {
synchronized (mutex) {
if (instance == null) {
AClass tmpInstance = new AClass();
tmpInstance.init();
instance = tmpInstance;
}
}
}
So, is this subject to the reordering problem, i.e., could instance be assigned to tmpInstance prior to tmpInstance.init() being called?
Thanks, Rich