Let's say I have a class that requires configuration, dependency injection etc.
public class MyClass {
private String someConfig;
private SomeMutableClass anotherConfig;
MyClass() {
// impractical to set everything in ctor
// otherwise I'd declare someConfig final and
// not worry about MT safety.
}
void setConfig(cfg) {
this.someConfig = cfg;
}
void anotherConfig(cfg) {
this.anotherConfig = cfg;
}
...
// below is code that uses the config set before, possibly by
// multiple threads.
}
This is a contrived example, but what if I can't easily do all the config in the ctor? Let's say the config is done early in execution and doesn't change. Strictly speaking due to the memory model I'd have to synchronize all references to someConfig. Can this requirement be relaxed in practice?