Java: I have shared variable(of type Object) whose value will be changed very frequently by different threads.
Accessing this variable’s value from other method by some other set of threads, will give any corrupted value (the value that is not at all assigned) ?
Does any problem will happen when the variable is accessed at the time of reference swapping ???
// this method will be called very frequently
public void changeValue(Object value)
{
this.value = value;
}
// will this method call return an invalid memory reference ?
public Object getValue()
{
return value;
}
Assigning a new Object to a variable is an atomic operation, but if you don’t make the reference volatile, or synchronize access to this reference, or use an AtomicReference, you will have visibility problems and see stale values.