Class ConcurrentReferenceHashMap.Segment<K,V>
- Type Parameters:
K- the type of keys maintained by this Segment.V- the type of mapped values.
- All Implemented Interfaces:
Serializable,Lock
- Enclosing class:
ConcurrentReferenceHashMap<K,V>
Segments maintain a table of entry lists that are ALWAYS kept in a consistent state, so they can be read without locking. Next fields of nodes are immutable (final). All list additions are performed at the front of each bin. This makes it easy to check changes, and also fast to traverse. When nodes would otherwise be changed, new nodes are created to replace them. This works well for hash tables since the bin lists tend to be short. (The average length is less than two for the default load factor threshold.)
Read operations can thus proceed without locking, but rely on selected uses of volatiles to ensure that completed write operations performed by other threads are noticed. For most purposes, the "count" field, tracking the number of elements, serves as that volatile variable ensuring visibility. This is convenient because this field needs to be read in many read operations anyway:
- All (unsynchronized) read operations must first read the "count" field, and should not look at table entries if it is 0.
- All (synchronized) write operations should write to the "count" field after structurally changing any bin. The operations must not take any action that could even momentarily cause a concurrent read operation to see inconsistent data. This is made easier by the nature of the read operations in Map. For example, no operation can reveal that the table has grown but the threshold has not yet been updated, so there are no atomicity requirements for this with respect to reads.
As a guide, all critical volatile reads and writes to the count field are marked in code comments.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intThe number of elements in this segment's region.private final booleanprivate final ConcurrentReferenceHashMap.ReferenceTypeprivate final floatThe load factor for the hash table.private intNumber of updates that alter the size of the table.private ReferenceQueue<Object> The collected weak-key reference queue for this segment.private static final longprivate ConcurrentReferenceHashMap.HashEntry<K,V>[] The per-segment table.private intThe table is rehashed when its size exceeds this threshold.private final ConcurrentReferenceHashMap.ReferenceType -
Constructor Summary
ConstructorsConstructorDescriptionSegment(int initialCapacity, float loadFactor, ConcurrentReferenceHashMap.ReferenceType keyType, ConcurrentReferenceHashMap.ReferenceType valueType, boolean identityComparisons) -
Method Summary
Modifier and TypeMethodDescription(package private) V(package private) VapplyIfPresent(K key, int hash, BiFunction<? super K, ? super V, ? extends V> remappingFunction) (package private) voidclear()(package private) booleancontainsKey(Object key, int hash) (package private) booleancontainsValue(Object value) (package private) V(package private) ConcurrentReferenceHashMap.HashEntry<K, V> getFirst(int hash) Gets properly casted first entry of bin for given hash.(package private) Vprivate boolean(package private) static <K,V> ConcurrentReferenceHashMap.Segment<K, V>[] newArray(int i) (package private) ConcurrentReferenceHashMap.HashEntry<K, V> newHashEntry(K key, int hash, ConcurrentReferenceHashMap.HashEntry<K, V> next, V value) (package private) VThis method must be called with exactly one ofvalueandfunctionnon-null.private VputInternal(K key, int hash, V value, Function<? super K, ? extends V> function, boolean onlyIfAbsent) (package private) VReads value field of an entry under lock.(package private) intrehash()(package private) VRemoves match on key only if value is null, else match both.private VremoveInternal(Object key, int hash, Object value, boolean refRemove) (package private) void(package private) V(package private) booleanprivate VreplaceInternal(K key, int hash, V newValue) private booleanreplaceInternal2(K key, int hash, V oldValue, V newValue) (package private) voidsetTable(ConcurrentReferenceHashMap.HashEntry<K, V>[] newTable) Sets table to new HashEntry array.Methods inherited from class java.util.concurrent.locks.ReentrantLock
getHoldCount, getOwner, getQueuedThreads, getQueueLength, getWaitingThreads, getWaitQueueLength, hasQueuedThread, hasQueuedThreads, hasWaiters, isFair, isHeldByCurrentThread, isLocked, lock, lockInterruptibly, newCondition, toString, tryLock, tryLock, unlock
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
count
private transient volatile int countThe number of elements in this segment's region. -
modCount
private transient int modCountNumber of updates that alter the size of the table. This is used during bulk-read methods to make sure they see a consistent snapshot: If modCounts change during a traversal of segments computing size or checking containsValue, then we might have an inconsistent view of state so (usually) we must retry. -
threshold
private transient int thresholdThe table is rehashed when its size exceeds this threshold. (The value of this field is always(int)(capacity * loadFactor).) -
table
The per-segment table. -
loadFactor
private final float loadFactorThe load factor for the hash table. Even though this value is same for all segments, it is replicated to avoid needing links to outer object. -
refQueue
The collected weak-key reference queue for this segment. This should be (re)initialized whenever table is assigned, -
keyType
-
valueType
-
identityComparisons
private final boolean identityComparisons
-
-
Constructor Details
-
Segment
Segment(int initialCapacity, float loadFactor, ConcurrentReferenceHashMap.ReferenceType keyType, ConcurrentReferenceHashMap.ReferenceType valueType, boolean identityComparisons)
-
-
Method Details
-
newArray
-
apply
-
applyIfPresent
-
clear
void clear() -
containsKey
-
containsValue
-
get
-
getFirst
Gets properly casted first entry of bin for given hash. -
getValue
-
keyEq
-
newHashEntry
ConcurrentReferenceHashMap.HashEntry<K,V> newHashEntry(K key, int hash, ConcurrentReferenceHashMap.HashEntry<K, V> next, V value) -
put
This method must be called with exactly one ofvalueandfunctionnon-null. -
putInternal
-
readValueUnderLock
Reads value field of an entry under lock. Called if value field ever appears to be null. This is possible only if a compiler happens to reorder a HashEntry initialization with its table assignment, which is legal under memory model but is not known to ever occur. -
rehash
int rehash() -
remove
Removes match on key only if value is null, else match both. -
removeInternal
-
removeStale
void removeStale() -
replace
-
replace
-
replaceInternal
-
replaceInternal2
-
setTable
Sets table to new HashEntry array. Call only while holding lock or in constructor.
-