I faced the following question during an interview:
Lets assume a simple class
public class Example{
private int a;
public void update(){
a = some new value;
}
public int getA(){
return a;
}
}
Now there are 2 threads (T1 and T2) which read and update the a value in the following sequence:
T2 (call update() and the value was set to 1)
T1 (call getA())
T2 (call update() and the value was set to 2)
T1 (call getA())
Is it possible for the last call getA() of thread T1 to return the value 1? If yes under what circumstances?
visibilityproblem. As stated here. As @fge stated, there's no guarantee of thehappens-beforerelationship.