The question actually refers to a different question, which was closed as duplicate because it was probably not well formulated.
What would be an effective alternative lazy initialization idiom instead of double-checked locking for this code sample (in a multithreaded environment):
public class LazyEvaluator {
private final Object state;
private volatile LazyValue lazyValue;
public LazyEvaluator(Object state) {
this.state = state;
}
public LazyValue getLazyValue() {
if (lazyValue == null) {
synchronized (this) {
if (lazyValue == null) {
lazyValue = new LazyValue(someSlowOperationWith(state), 42);
}
}
}
return lazyValue;
}
public static class LazyValue {
private String name;
private int value;
private LazyValue(String name, int value) {
this.name = name;
this.value = value;
}
private String getName() {
return name;
}
private int getValue() {
return value;
}
}
}
EDIT Updated to include a slow operation and added explicit mention about multithreaded environment