Domanda

In ConcurrentHashMap we have segments which basically extend ReentrantLock.

static final class Segment<K,V> extends ReentrantLock implements Serializable

Does this ReentrantLock use it's fairness property?

public ReentrantLock(boolean fair) {
    sync = fair ? new FairSync() : new NonfairSync();
}

So lets say thread t1 has read lock on a partition of ConcurrentHashMap and another two threads t2 and t3 are waiting for read and write locks respectively on the same partition. So which one(t2 or t3) will acquire the the lock once t1 releases it's lock.

As per my knowledge it would be the one who is waiting for the longest if fairness is set to true. But is it set to true in the case of concurrentHashMap? If no can we say with any certainty which thread would get the next lock?

È stato utile?

Soluzione

From ConcurrentHashMap source code we can see that it uses a subclass of 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;
   }
   ...

As we can see its only constructor implicitly calls ReentrantLock's no-args constructor which creates a non-fair lock. It means ConcurrentHashMap's locks are always non-fair

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top