【发布时间】:2017-03-14 11:48:32
【问题描述】:
我正在调查java.util.concurrent.locks.AbstractQueuedSynchronizer源代码。
从多个地方调用compareAndSetState方法。
/**
* 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.
*
* @param expect the expected value
* @param update the new value
* @return {@code true} if successful. False return indicates that the actual
* value was not equal to the expected value.
*/
protected final boolean compareAndSetState(int expect, int update) {
// See below for intrinsics setup to support this
return unsafe.compareAndSwapInt(this, stateOffset, expect, update);
}
参数expect和update很明显,对应原子参数。但是this 是对象(而不是int)。
这与expect相比如何?
【问题讨论】:
标签: java concurrency synchronization atomic-swap