Package org.eclipse.emf.common.util
Class Pool<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
org.eclipse.emf.common.util.WeakInterningHashSet<E>
org.eclipse.emf.common.util.Pool<E>
- All Implemented Interfaces:
Serializable,Iterable<E>,Collection<E>,Set<E>,InterningSet<E>
- Direct Known Subclasses:
SegmentSequence.SegmentSequencePool,SegmentSequence.StringArrayPool,URI.URIPool
A thread safe implementation of a
weak interning hash set particularly well-suited for implementing a pool of instances.
All access is thread safe, guarded with a shared read lock and an exclusive getWriteLock() lock
to support multiple simultaneous readers while ensuring that writes are properly serial.
The locks are held for the minimal period to allow maximal concurrency.
Removals, i.e., remove, removeAll, clear, and retainAll, are not supported.- Since:
- 2.9
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classAn access unit is used during access to the pool.protected static classprotected static classprotected static classNested classes/interfaces inherited from class org.eclipse.emf.common.util.WeakInterningHashSet
WeakInterningHashSet.Entry<E>, WeakInterningHashSet.SelfCleaningEntry<E> -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intRecord the number of accesses so that thecleanup()can be called occasionally.protected intprotected final Pool.AccessUnit.Queue<E> protected final LockTo support maximum concurrency, a pair of read and write locks is maintained; this is theread lock.protected final ReentrantReadWriteLockprotected final LockTo support maximum concurrency, a pair of read and write locks is maintained; this is thewrite lock.Fields inherited from class org.eclipse.emf.common.util.WeakInterningHashSet
capacityIndex, containsNull, entries, externalQueue, internalQueue, modCount, NULL_ENTRY, PRIME_CAPACITIES, size, threshold -
Constructor Summary
ConstructorsModifierConstructorDescriptionPool()Creates an instance with a capacity of 1031.Pool(int minimumCapacity) Creates an instance.protectedPool(int minimumCapacity, Pool.AccessUnit.Queue<E> primaryAccessUnits) protectedPool(int minimumCapacity, Pool.AccessUnit.Queue<E> primaryAccessUnits, ReferenceQueue<Object> queue) -
Method Summary
Modifier and TypeMethodDescriptionprotected final voidaccess(boolean isReadLocked, Pool.AccessUnit<E> accessUnit) Collect all the values with a matching hash code.booleanSpecialized to ensure thread safety.protected final EaddEntry(boolean isExclusive, E internalizedValue, Pool.AccessUnit<E> accessUnit) Adds an entry to the pool, but first checks if the entry has been added by another thread since the time when the pool wasaccessedwithout holding any locks.protected voidcleanup()Specialized to ensure that the write lock is held during cleanup.voidclear()Throws anUnsupportedOperationExceptionbecause removal is not supported..booleanSpecialized to ensure thread safety.booleancontainsAll(Collection<?> collection) Specialized to ensure thread safety.protected voidCallssuper.WeakInterningHashSet.cleanup().protected EdoIntern(boolean isExclusive, Pool.AccessUnit<E> accessUnit) Returns the interned version of the value accessed by this access unit andfreesthe access unit for reuse.protected booleanEnsures that 3/4 of current capacity is larger than the current size, i.e., that theWeakInterningHashSet.sizeinvalid input: '<'=WeakInterningHashSet.threshold.booleanSpecialized to ensure thread safety.Returns either the instance already contained in the set that's equal to the given object, ornullif the object is not in the set.protected WeakInterningHashSet.Entry<E> getEntry(int hashCode) Gets the first entry with the matching hash code.Returns this pool'sread lock.Returns this pool'swrite lock.voidgrow(int minimumCapacity) Specialized to ensure that the exclusive write lock is held during growth.inthashCode()Specialized to ensure thread safety.Specialized to ensure thread safety.iterator()Callers of the iterator must ensure that they hold the sharedread lockfor the lifetime of the iterator's usage.protected Pool.AccessUnit.Queue<E> protected WeakInterningHashSet.Entry<E> newExternalEntry(E object, int hashCode) Creates a new entry for the given referent and the given hash code managed by theWeakInterningHashSet.externalQueue.booleanThrows anUnsupportedOperationExceptionbecause removal is not supported..booleanremoveAll(Collection<?> collection) Throws anUnsupportedOperationExceptionbecause removal is not supported..booleanretainAll(Collection<?> collection) Throws anUnsupportedOperationExceptionbecause removal is not supported..Object[]toArray()Specialized to ensure thread safety.<T> T[]toArray(T[] a) Specialized to ensure thread safety.toString()Specialized to ensure thread safety.Methods inherited from class org.eclipse.emf.common.util.WeakInterningHashSet
addEntry, asInstance, dump, equals, hashCode, index, newEntries, newEntry, newInternalEntry, nullEntry, putEntry, removeEntry, removeEntry, sizeMethods inherited from class java.util.AbstractCollection
addAll, isEmptyMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.Set
addAll, isEmpty, spliterator
-
Field Details
-
accessCount
protected int accessCountRecord the number of accesses so that thecleanup()can be called occasionally. It's a state modifying operation, so it must hold the exclusivewrite lockduring execution. -
cleanupPeriod
protected int cleanupPeriodThe number ofaccessesbetween each attempt toclean upgarbage collected entries. Garbage collecting entries requires the exclusivegetWriteLock()to be held, so it's best to do this infrequently. -
readWriteLock
-
readLock
To support maximum concurrency, a pair of read and write locks is maintained; this is theread lock. -
writeLock
To support maximum concurrency, a pair of read and write locks is maintained; this is thewrite lock. -
-
-
Constructor Details
-
Pool
public Pool()Creates an instance with a capacity of 1031. -
Pool
public Pool(int minimumCapacity) Creates an instance. -
Pool
-
Pool
protected Pool(int minimumCapacity, Pool.AccessUnit.Queue<E> primaryAccessUnits, ReferenceQueue<Object> queue)
-
-
Method Details
-
ensureCapacity
protected boolean ensureCapacity()Description copied from class:WeakInterningHashSetEnsures that 3/4 of current capacity is larger than the current size, i.e., that theWeakInterningHashSet.sizeinvalid input: '<'=WeakInterningHashSet.threshold. If not, itreallocatesthe entries to the nextprime capacity, i.e., it approximate doubles the capacity, and thenrehashesthe set. The return value indicates whether or note the entries where rehashed.- Overrides:
ensureCapacityin classWeakInterningHashSet<E>
-
newExternalEntry
Description copied from class:WeakInterningHashSetCreates a new entry for the given referent and the given hash code managed by theWeakInterningHashSet.externalQueue.- Overrides:
newExternalEntryin classWeakInterningHashSet<E>
-
newDefaultAccessUnits
-
getReadLock
-
getWriteLock
Returns this pool'swrite lock. This should be used only for thread-safeiterationin whichremoveis called. -
getEntry
Gets the first entry with the matching hash code. UseWeakInterningHashSet.Entry.getNextEntry()to navigate to the next entry with the same hash code. This method does no locking so it may fail to find matches if the pool is rehashing or another thread is adding the entry.- Overrides:
getEntryin classWeakInterningHashSet<E>
-
access
Collect all the values with a matching hash code. IfisReadLockedistrueit's expected that theread lockis already locked. In this case the access will reliably collect all the matching entries currently in the pool. WhenisReadLockedisfalse, the access is done without locking and may spuriously fail to return any matches, i.e., if the pool is currently rehashing, or another thread is currently adding the value. Becauseremovalis not supported, there will never be a spurious match to a removed entry. This also incrementsaccessCountandperiodicallycallscleanup()if it's not a read locked access. -
addEntry
Adds an entry to the pool, but first checks if the entry has been added by another thread since the time when the pool wasaccessedwithout holding any locks. The access unit is used todeterminewhether an entry was already added. This returns either the value added, or the value that was already added by another thread. TheisExlusiveargument controls whether the write lock needs to be acquired (false) or is already acquired (true). -
cleanup
protected void cleanup()Specialized to ensure that the write lock is held during cleanup.- Overrides:
cleanupin classWeakInterningHashSet<E>
-
doCleanup
protected void doCleanup()Callssuper.WeakInterningHashSet.cleanup(). -
grow
public void grow(int minimumCapacity) Specialized to ensure that the exclusive write lock is held during growth.- Overrides:
growin classWeakInterningHashSet<E>
-
add
Specialized to ensure thread safety.- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceSet<E>- Overrides:
addin classWeakInterningHashSet<E>
-
intern
Specialized to ensure thread safety. If the value needs to be added, aninternalizedversion is added to pool. This implementation delegates todoIntern(boolean, AccessUnit).- Specified by:
internin interfaceInterningSet<E>- Overrides:
internin classWeakInterningHashSet<E>- Parameters:
value- the object to intern.- Returns:
- the existing instance already contained in the set equal to the given object, or the object itself (or perhaps in some implementations, an object equal to the object itself).
-
doIntern
Returns the interned version of the value accessed by this access unit andfreesthe access unit for reuse. -
get
Description copied from class:WeakInterningHashSetReturns either the instance already contained in the set that's equal to the given object, ornullif the object is not in the set.- Specified by:
getin interfaceInterningSet<E>- Overrides:
getin classWeakInterningHashSet<E>- Parameters:
value- the object to intern.- Returns:
- the existing instance already contained in the set equal to the given object, or
nullif the object is not in the set.
-
contains
Specialized to ensure thread safety. This access is done while holding only the sharedreadlock.- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceSet<E>- Overrides:
containsin classWeakInterningHashSet<E>
-
iterator
-
equals
Specialized to ensure thread safety.- Specified by:
equalsin interfaceCollection<E>- Specified by:
equalsin interfaceSet<E>- Overrides:
equalsin classAbstractSet<E>
-
hashCode
public int hashCode()Specialized to ensure thread safety.- Specified by:
hashCodein interfaceCollection<E>- Specified by:
hashCodein interfaceSet<E>- Overrides:
hashCodein classAbstractSet<E>
-
toArray
Specialized to ensure thread safety.- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceSet<E>- Overrides:
toArrayin classAbstractCollection<E>
-
toArray
public <T> T[] toArray(T[] a) Specialized to ensure thread safety.- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceSet<E>- Overrides:
toArrayin classAbstractCollection<E>
-
containsAll
Specialized to ensure thread safety.- Specified by:
containsAllin interfaceCollection<E>- Specified by:
containsAllin interfaceSet<E>- Overrides:
containsAllin classAbstractCollection<E>
-
remove
Throws anUnsupportedOperationExceptionbecause removal is not supported..- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceSet<E>- Overrides:
removein classWeakInterningHashSet<E>
-
removeAll
Throws anUnsupportedOperationExceptionbecause removal is not supported..- Specified by:
removeAllin interfaceCollection<E>- Specified by:
removeAllin interfaceSet<E>- Overrides:
removeAllin classAbstractSet<E>
-
retainAll
Throws anUnsupportedOperationExceptionbecause removal is not supported..- Specified by:
retainAllin interfaceCollection<E>- Specified by:
retainAllin interfaceSet<E>- Overrides:
retainAllin classAbstractCollection<E>
-
clear
public void clear()Throws anUnsupportedOperationExceptionbecause removal is not supported..- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceSet<E>- Overrides:
clearin classWeakInterningHashSet<E>
-
toString
Specialized to ensure thread safety.- Overrides:
toStringin classAbstractCollection<E>
-