0

I know to be thread safe one has to syncronize the get method of a primitive property, like in this class:

public class C {
    private int a;
    public synchronized int getA() { return a; }
    public synchronized void setA(int a) { this.a= a; }
}

Is it also necessary to syncronize the get method if a is a refrence instead of a primitive?

edit: How soon will the value be updated(visible to other threads) if the getter is not synchronized. In my application, the getter is frequently called by different threads thus I want it to be unsyncronized for performance, it is ok if the getter dose not return the latest value, only the updated value never being used by other threads is not acceptable.

Yulin
  • 1,163
  • 1
  • 7
  • 10
  • Yes, it is necessary. – Louis Wasserman Jul 21 '17 at 17:46
  • Please read the quoted text in the accepted answer on the linked question. The quote applies equally to references and values. A reference is a value. – Nathan Hughes Jul 21 '17 at 17:58
  • @ Louis Wasserman in my application, the getter is frequently called by different threads thus I don't want it to be synchronized for performance, the setter is not frequently used, and is not a problem if the getter cannot get the latest value every time: a lag in updation is acceptable(but a updation being never visible to other threads is not acceptable), so how soon is the updation being visible to other threads if not the getter is not synchronized. – Yulin Jul 22 '17 at 08:58

0 Answers0