Given the following code:
class MyObj{
AtomicLong counter;
public MyObj(){
count = new AtomicLong();
}
}
MyObj obj = new MyObj();
// start a few threads that referencey obj and access counter
I would like to know if there is a potential scenario here, that the created threads will see the state of MyObj as incomplete, due to compiler reodering/inlining? For instance, they might see counter = null
, or counter partially created, as it is not a final field?
There are two situations:
MyObj obj = new MyObj()
, there is a happens-before relationship that guarantees that the threads will all see the correctly constructed objectobj == null
or obj != null && obj.counter == null
or obj != null && obj.counter != null
).