I have looked at the Java API for ReentrantLock and what I can see is that no synchronization is used with the synchronized keyword. Is it in the below method in AbstractQueuedSynchronizer (that ReentrantLock is refering to when trying to aquire a lock) that synchronizes the object? Since the compareAndSwapInt is a native method, is the synchronization made at the native level/code?

protected final boolean compareAndSetState(int expect, int update) {
    // See below for intrinsics setup to support this
    return unsafe.compareAndSwapInt(this, stateOffset, expect, update);
}
有帮助吗?

解决方案

The implementation will likely be different per JDK. Sun (now Oracle) implementation, for example, does it via sun.misc.Unsafe ( http://www.docjar.com/docs/api/sun/misc/Unsafe.html )

I once blogged about how Java concurrency in unsafe :)

其他提示

You're correct: in Oracle's JDK, ReentrantLock is implemented in terms of the native compare-and-swap primitive (plus a fair amount of Java code on top of it), and not in terms of the synchronized keyword.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top