ReentrantReadWriteLock
如何运作?它是旋转锁吗?
问题来自Elasticsearch,当它显示
时 java.lang.ThreadLocal$ThreadLocalMap.expungeStaleEntry(Unknown Source)
java.lang.ThreadLocal$ThreadLocalMap.remove(Unknown Source)
java.lang.ThreadLocal$ThreadLocalMap.access$200(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使用率同时很高。它看起来像旋转锁定。
答案 0 :(得分:0)
线程本地数据存在相关成本。你在这看到的就是这个。您甚至可以在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)实现)。