简体   繁体   中英

How does ReentrantLock synchronize?

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 :)

您是正确的:在Oracle的JDK中, ReentrantLock是根据本机比较和交换原语(在其之上加上大量Java代码)实现的,而不是通过synchronized关键字实现的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM