ReentrantReadWriteLock 是作为自旋锁实现的吗?
Is ReentrantReadWriteLock implemented as a spin-lock?
ReentrantReadWriteLock
是如何工作的?是自旋锁吗?
问题来自Elasticsearch,当它显示
java.lang.ThreadLocal$ThreadLocalMap.expungeStaleEntry(Unknown Source)
java.lang.ThreadLocal$ThreadLocalMap.remove(Unknown Source)
java.lang.ThreadLocal$ThreadLocalMap.access0(Unknown Source)
java.lang.ThreadLocal.remove(Unknown Source)
java.util.concurrent.locks.ReentrantReadWriteLock$Sync.tryReleaseShared(Unknown Source)
java.util.concurrent.locks.AbstractQueuedSynchronizer.releaseShared(Unknown Source)
java.util.concurrent.locks.ReentrantReadWriteLock$ReadLock.unlock(Unknown Source)
在所有快照中的热线程中,同时 cpu 使用率很高。看起来像自旋锁。
线程本地数据会产生相关费用。你在这里看到的正是这个。你甚至可以在 ReentrantReadWriteLock 中看到评论提到这个并通过缓存线程本地数据的数据来优化它:
评论:
/**
* The hold count of the last thread to successfully acquire
* readLock. This saves ThreadLocal lookup in the common case
* where the next thread to release is the last one to
* acquire.
* [...]
*/
private transient HoldCounter cachedHoldCounter;
ReentrantReadWriteLock 不使用自旋锁。它使用 Sync 对象,该对象使用来自 AbstractQueueSynchronizer 的 wait/notify(由 LockSupport.park(this) 实现)。
ReentrantReadWriteLock
是如何工作的?是自旋锁吗?
问题来自Elasticsearch,当它显示
java.lang.ThreadLocal$ThreadLocalMap.expungeStaleEntry(Unknown Source)
java.lang.ThreadLocal$ThreadLocalMap.remove(Unknown Source)
java.lang.ThreadLocal$ThreadLocalMap.access0(Unknown Source)
java.lang.ThreadLocal.remove(Unknown Source)
java.util.concurrent.locks.ReentrantReadWriteLock$Sync.tryReleaseShared(Unknown Source)
java.util.concurrent.locks.AbstractQueuedSynchronizer.releaseShared(Unknown Source)
java.util.concurrent.locks.ReentrantReadWriteLock$ReadLock.unlock(Unknown Source)
在所有快照中的热线程中,同时 cpu 使用率很高。看起来像自旋锁。
线程本地数据会产生相关费用。你在这里看到的正是这个。你甚至可以在 ReentrantReadWriteLock 中看到评论提到这个并通过缓存线程本地数据的数据来优化它:
评论:
/**
* The hold count of the last thread to successfully acquire
* readLock. This saves ThreadLocal lookup in the common case
* where the next thread to release is the last one to
* acquire.
* [...]
*/
private transient HoldCounter cachedHoldCounter;
ReentrantReadWriteLock 不使用自旋锁。它使用 Sync 对象,该对象使用来自 AbstractQueueSynchronizer 的 wait/notify(由 LockSupport.park(this) 实现)。