在ConcurrentHashMap中,我们有基本上扩展了ReentrantLock的片段。
static final class Segment<K,V> extends ReentrantLock implements Serializable这个ReentrantLock是否使用它的公平性属性?
public ReentrantLock(boolean fair) {
sync = fair ? new FairSync() : new NonfairSync();
}因此,假设线程t1在ConcurrentHashMap的一个分区上具有读锁,另外两个线程t2和t3分别在同一分区上等待读和写锁。那么,一旦t1释放它的锁,哪个(t2或t3)将获得锁。
据我所知,如果公平设置为真,那么等待时间最长的将是那个人。但在concurrentHashMap的情况下,它是否设置为true?如果没有,我们可以肯定地说哪个线程会获得下一个锁?
发布于 2013-07-09 13:20:22
从ConcurrentHashMap源代码中,我们可以看到它使用了ReentrantLock的子类
static final class Segment<K,V> extends ReentrantLock
...
Segment(float lf, int threshold, HashEntry<K,V>[] tab) {
this.loadFactor = lf;
this.threshold = threshold;
this.table = tab;
}
...正如我们所看到的,它唯一的构造函数隐式调用了ReentrantLock的no-args构造函数,从而创建了一个非公平锁。这意味着ConcurrentHashMap的锁总是不公平的
https://stackoverflow.com/questions/17540409
复制相似问题