When i read AbstractQueuedSynchronizer
in jdk1.8, i see the comment that compareAndSetState
method has memory semantics of a volatile read and write .
The comment and code are as follows:
/**
* Atomically sets synchronization state to the given updated
* value if the current state value equals the expected value.
* This operation has memory semantics of a {@code volatile} read
* and write.
*/
protected final boolean compareAndSetState(int expect, int update) {
return unsafe.compareAndSwapInt(this, stateOffset, expect, update);
}
In AbstractQueuedSynchronizer class, the stateOffset
is a volatile member called state
Just wonder what memory semantics is if state
is a non-volatile member ..