There was a well-known pitfall while using the double-checked locking pattern (an example and explanation taken from "Concurrency in Practice"):
@NotThreadSafe
public class DoubleCheckedLocking {
private static Resource resource;
public static Resource getInstance() {
if (resource == null) {
synchronized (DoubleCheckedLocking.class) {
if (resource == null)
resource = new Resource();
}
}
return resource;
}
}
Some thread may see the initialized value of 'resource' variable, while the object itself is still under construction.
A question is: is the problem remains if we are constructing the resource object in some method? I.e.
resource = createResource();
Can some thread evaluate resource != null as true while the resource object is still under construction in createResource() method?