I have the following:
private int i;
public int getI() {
return i;
}
public synchronized int getISync() {
return i;
}
From a strict safety point of view (neglecting the fact that getISync
implies locks), is getI
immune to thread interference? I mean, is it safe? Does getISync
offer something more, or is just the same thing in regard of safety? If there's a difference, please explain.
N.B. I know that on int
fields (in this example), read and write operations are natively considered atomic.