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.
加了同步也不能保证读到非过期的数据,问题不在这里。同步的作用是不会读到不完整的数据,这才是互斥的原因,int的更新未必是原子操作。
AtomicInteger,不需要synchronized,这种类型的一旦开始,就一直运行到结束,中间不会有任何Context switch
参见Synchronized Methods:
也就是说加锁的第二个作用是保证了内存壁垒。