Could someone validate my understanding of the memory fence established after a constructor executes. For example, suppose I have a class called Stock.
public final class Stock{
private final String ticker;
private double qty;
private double price;
public Stock ( String ticker, double qty, double price ){
this.ticker = ticker;
this.qty = qty;
this.price = price;
//I am assuming a memory fence gets inserted here.
}
public final void updateQty( double qty ){
this.qty = qty;
}
public final void updatePrice( double price ){
this.price = price;
}
}
Further, assume that the constructor is executed by Thread1 and then updateQty()
and updatePrice()
are called several time by Thread2 (always by Thread2).
My contention is that after Thread1 creates the object, the object's "visibility" is established with all the other threads in the jvm. And since the two mutable variables are only changed by Thread2, I don't need any locking. Am I correct?