我看过Java API for ReentrantLock,我看到的是synchronized关键字没有使用同步。是在AbstractQueuedSynchronizer ( ReentrantLock在尝试获取锁时引用的)中的以下方法中同步对象吗?由于compareAndSwapInt是本机方法,因此同步是在本机级别/代码中进行的吗?
protected final boolean compareAndSetState(int expect, int update) {
// See below for intrinsics setup to support this
return unsafe.compareAndSwapInt(this, stateOffset, expect, update);
}发布于 2012-04-27 21:57:02
每个JDK的实现可能会有所不同。例如,Sun (现在的Oracle)实现是通过sun.misc.Unsafe ( http://www.docjar.com/docs/api/sun/misc/Unsafe.html )实现的
我曾经在博客中介绍过Java concurrency in unsafe :)
发布于 2012-04-27 21:56:53
您是对的:在Oracle中,ReentrantLock是按照原生compare-and-swap原语(加上它上面的大量compare-and-swap代码)实现的,而不是按照synchronized关键字实现的。
https://stackoverflow.com/questions/10351918
复制相似问题