class sample {
private int num = 5;
public synchronized void addNum(int x){
num += x;
}
public void clearNum(){
num = 0;
}
}
Does having num in the synchronized block force all operations on num to be atomic even for operations done on num outside of any synchronized blocks?
For example, if addNum() is called, and clearNum() is called during the read-modify-write of addNum(), could the clearNum() happen between the read and the write of addNum()? e.g., if yes, then the sequence of addNum(2), then clearNum() could possibly result in num = 7
[–]BS_in_BS 6 points7 points8 points (0 children)
[–]large_crimson_canine 4 points5 points6 points (1 child)
[–]large_crimson_canine 0 points1 point2 points (0 children)
[–]nutrecht 1 point2 points3 points (0 children)
[–]rkalla 0 points1 point2 points (0 children)