@InterfaceAudience.Private public abstract class ObjectPool<K,V> extends Object
Modifier and Type | Class and Description |
---|---|
static interface |
ObjectPool.ObjectFactory<K,V>
An
ObjectFactory object is used to create
new shared objects on demand. |
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_CONCURRENCY_LEVEL
The default concurrency level,
used when not otherwise specified in a constructor.
|
static int |
DEFAULT_INITIAL_CAPACITY
The default initial capacity,
used when not otherwise specified in a constructor.
|
private ObjectPool.ObjectFactory<K,V> |
objectFactory |
private Lock |
purgeLock
For preventing parallel purge
|
protected ConcurrentMap<K,Reference<V>> |
referenceCache
Does not permit null keys.
|
protected ReferenceQueue<V> |
staleRefQueue |
Constructor and Description |
---|
ObjectPool(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.
|
Modifier and Type | Method and Description |
---|---|
abstract Reference<V> |
createReference(K key,
V obj)
Create a reference associated with the given object
|
V |
get(K key)
Returns a shared object associated with the given
key ,
which is identified by the equals method. |
abstract K |
getReferenceKey(Reference<V> ref)
Get key of the given reference
|
void |
purge()
Removes stale references of shared objects from the pool.
|
int |
size()
Returns an estimated count of objects kept in the pool.
|
protected final ReferenceQueue<V> staleRefQueue
private final ObjectPool.ObjectFactory<K,V> objectFactory
protected final ConcurrentMap<K,Reference<V>> referenceCache
public static final int DEFAULT_INITIAL_CAPACITY
public static final int DEFAULT_CONCURRENCY_LEVEL
public ObjectPool(ObjectPool.ObjectFactory<K,V> objectFactory)
objectFactory
- the factory to supply new objects on demandNullPointerException
- if objectFactory
is nullpublic ObjectPool(ObjectPool.ObjectFactory<K,V> objectFactory, int initialCapacity)
objectFactory
- the factory to supply new objects on demandinitialCapacity
- the initial capacity to keep objects in the poolNullPointerException
- if objectFactory
is nullIllegalArgumentException
- if initialCapacity
is negativepublic ObjectPool(ObjectPool.ObjectFactory<K,V> objectFactory, int initialCapacity, int concurrencyLevel)
objectFactory
- the factory to supply new objects on demandinitialCapacity
- the initial capacity to keep objects in the poolconcurrencyLevel
- the estimated count of concurrently accessing threadsNullPointerException
- if objectFactory
is nullIllegalArgumentException
- if initialCapacity
is negative or
concurrencyLevel
is non-positivepublic void purge()
ReferenceQueue
, because
ReferenceQueue.poll
just checks a volatile instance variable in ReferenceQueue
.public abstract Reference<V> createReference(K key, V obj)
key
- the key to store in the referenceobj
- the object to associate withpublic abstract K getReferenceKey(Reference<V> ref)
ref
- The referencepublic V get(K key)
key
,
which is identified by the equals
method.NullPointerException
- if key
is nullCopyright © 2007–2019 The Apache Software Foundation. All rights reserved.