In a concurrent program, is this safe:
private volatile int i;
public int getI() {
return i;
}
public synchronized incrementI() {
i++;
}
?
From what I know about synchronize
, the changes are guaranteed to be available on i
only for threads that acquires the lock monitor on the same object.
So I think, the code above is not safe. Please just confirm this if true, otherwise, please explain.
Thanks.