@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.限定符和类型 | 类和说明 |
---|---|
static interface |
WeakObjectPool.ObjectFactory<K,V>
An
ObjectFactory object is used to create
new shared objects on demand. |
限定符和类型 | 字段和说明 |
---|---|
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.
|
构造器和说明 |
---|
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.
|
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–2022 The Apache Software Foundation. All rights reserved.