Package org.apache.hadoop.hbase.util
Class ObjectPool<K,V>
java.lang.Object
org.apache.hadoop.hbase.util.ObjectPool<K,V>
- Direct Known Subclasses:
SoftObjectPool
,WeakObjectPool
A thread-safe shared object pool in which object creation is expected to be lightweight, and the
objects may be excessively created and discarded.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
AnObjectFactory
object is used to create new shared objects on demand. -
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The default concurrency level, used when not otherwise specified in a constructor.static final int
The default initial capacity, used when not otherwise specified in a constructor.private final ObjectPool.ObjectFactory<K,
V> private final Lock
For preventing parallel purgeprotected final ConcurrentMap<K,
Reference<V>> Does not permit null keys.protected final ReferenceQueue<V>
-
Constructor Summary
ConstructorDescriptionObjectPool
(ObjectPool.ObjectFactory<K, V> objectFactory) Creates a new pool with the default initial capacity (16) and the default concurrency level (16).ObjectPool
(ObjectPool.ObjectFactory<K, V> objectFactory, int initialCapacity) Creates a new pool with the given initial capacity and the default concurrency level (16).ObjectPool
(ObjectPool.ObjectFactory<K, V> objectFactory, int initialCapacity, int concurrencyLevel) Creates a new pool with the given initial capacity and the given concurrency level. -
Method Summary
Modifier and TypeMethodDescriptioncreateReference
(K key, V obj) Create a reference associated with the given objectReturns a shared object associated with the givenkey
, which is identified by theequals
method.abstract K
getReferenceKey
(Reference<V> ref) Get key of the given referencevoid
purge()
Removes stale references of shared objects from the pool.int
size()
Returns an estimated count of objects kept in the pool.
-
Field Details
-
staleRefQueue
-
objectFactory
-
referenceCache
Does not permit null keys. -
purgeLock
For preventing parallel purge -
DEFAULT_INITIAL_CAPACITY
The default initial capacity, used when not otherwise specified in a constructor.- See Also:
-
DEFAULT_CONCURRENCY_LEVEL
The default concurrency level, used when not otherwise specified in a constructor.- See Also:
-
-
Constructor Details
-
ObjectPool
Creates a new pool with the default initial capacity (16) and the default concurrency level (16).- Parameters:
objectFactory
- the factory to supply new objects on demand- Throws:
NullPointerException
- ifobjectFactory
isnull
-
ObjectPool
Creates a new pool with the given initial capacity and the default concurrency level (16).- Parameters:
objectFactory
- the factory to supply new objects on demandinitialCapacity
- the initial capacity to keep objects in the pool- Throws:
NullPointerException
- ifobjectFactory
isnull
IllegalArgumentException
- ifinitialCapacity
is negative
-
ObjectPool
public ObjectPool(ObjectPool.ObjectFactory<K, V> objectFactory, int initialCapacity, int concurrencyLevel) Creates a new pool with the given initial capacity and the given concurrency level.- Parameters:
objectFactory
- the factory to supply new objects on demandinitialCapacity
- the initial capacity to keep objects in the poolconcurrencyLevel
- the estimated count of concurrently accessing threads- Throws:
NullPointerException
- ifobjectFactory
isnull
IllegalArgumentException
- ifinitialCapacity
is negative orconcurrencyLevel
is non-positive
-
-
Method Details
-
purge
Removes stale references of shared objects from the pool. References newly becoming stale may still remain. The implementation of this method is expected to be lightweight when there is no stale reference with the Oracle (Sun) implementation ofReferenceQueue
, becauseReferenceQueue.poll
just checks a volatile instance variable inReferenceQueue
. -
createReference
Create a reference associated with the given object- Parameters:
key
- the key to store in the referenceobj
- the object to associate with- Returns:
- the reference instance
-
getReferenceKey
Get key of the given reference- Parameters:
ref
- The reference- Returns:
- key of the reference
-
get
Returns a shared object associated with the givenkey
, which is identified by theequals
method.- Throws:
NullPointerException
- ifkey
isnull
-
size
Returns an estimated count of objects kept in the pool. This also counts stale references, and you might want to callpurge()
beforehand.
-