线程A正在修改value。线程B正在读,这里读的就是过期的数据了。只有给get,set同时加上同步才可以保证读到的不是过期数据。不明白,为什么get,set同时加上同步可以保证不出现这种情况?继续往下看,好像明白一点,共享变量在公共的锁中是相互可见的。
3 回答

呼啦一阵风
TA贡献1802条经验 获得超6个赞
Second, when a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads.
也就是说加锁的第二个作用是保证了内存壁垒。
添加回答
举报
0/150
提交
取消