@InterfaceAudience.Private public class WeakObjectPool<K,V> extends Object
WeakReference
based shared object pool.
The objects are kept in weak references and
associated with keys which are identified by the equals
method.
The objects are created by WeakObjectPool.ObjectFactory
on demand.
The object creation is expected to be lightweight,
and the objects may be excessively created and discarded.
Thread safe.Modifier and Type | Class and Description |
---|---|
static interface |
WeakObjectPool.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.
|
Constructor and Description |
---|
WeakObjectPool(WeakObjectPool.ObjectFactory<K,V> objectFactory)
Creates a new pool with the default initial capacity (16)
and the default concurrency level (16).
|
WeakObjectPool(WeakObjectPool.ObjectFactory<K,V> objectFactory,
int initialCapacity)
Creates a new pool with the given initial capacity
and the default concurrency level (16).
|
WeakObjectPool(WeakObjectPool.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 |
---|---|
V |
get(K key)
Returns a shared object associated with the given
key ,
which is identified by the equals method. |
void |
purge()
Removes stale references of shared objects from the pool.
|
int |
size()
Returns an estimated count of objects kept in the pool.
|
public static final int DEFAULT_INITIAL_CAPACITY
public static final int DEFAULT_CONCURRENCY_LEVEL
public WeakObjectPool(WeakObjectPool.ObjectFactory<K,V> objectFactory)
objectFactory
- the factory to supply new objects on demandNullPointerException
- if objectFactory
is nullpublic WeakObjectPool(WeakObjectPool.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 WeakObjectPool(WeakObjectPool.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()
public V get(K key)
key
,
which is identified by the equals
method.NullPointerException
- if key
is nullpublic int size()
purge()
beforehand.Copyright © 2007–2019 The Apache Software Foundation. All rights reserved.