public class Pool<T>
extends java.lang.Object
implements java.io.Serializable
The pool will only manage instances that were explicitly passed into the pool before. For more sophisticated pooling strategies, derive from this class or wrap it.
The implementation will provide these instances wrapped by a proxy, that will return the instance automatically to
the pool, if it falls out of scope and is collected by the garbage collector. Since the pool only returns instances
wrapped by a proxy that implements the Poolable interface, this can be used to release the instance manually
to the pool also. With an implementation of the Resetter interface each element's status can be reset or the
element can be dropped from the pool at all, if it is exhausted.
A client can use the pool's monitor for an improved synchronization. Every time an object is returned to the pool, all
waiting Threads of the monitor will be notified. This notification will happen independently of the result of the
Resetter.reset(Object) method.
A Pool instance can be created as usual with a builder, but also using various constructors to support dependency injection.
com.thoughtworks.proxy.toys.pool,
Serialized Form| Modifier and Type | Class and Description |
|---|---|
static class |
Pool.PoolBuild<T> |
protected static class |
Pool.PoolingInvoker<T>
The
Invoker of the proxy. |
static class |
Pool.PoolModeOrBuild<T> |
static class |
Pool.PoolResettedBy<T> |
static class |
Pool.PoolWith<T> |
| Modifier and Type | Field and Description |
|---|---|
private java.util.List<ObjectReference<T>> |
availableInstances |
private java.util.Map<T,java.lang.ref.WeakReference<T>> |
busyInstances |
private ProxyFactory |
factory |
private Resetter<? super T> |
resetter |
private static java.lang.reflect.Method |
returnInstanceToPool |
private SerializationMode |
serializationMode |
private static long |
serialVersionUID |
private java.lang.Class<?>[] |
types |
| Modifier | Constructor and Description |
|---|---|
private |
Pool() |
|
Pool(java.lang.Class<T> type)
Construct an Pool using the
StandardProxyFactory for elements that do not have to
be resetted. |
|
Pool(java.lang.Class<T> type,
ProxyFactory proxyFactory)
Construct a populated Pool with a specific proxy factory for elements that do not have to
be resetted.
|
|
Pool(java.lang.Class<T> type,
Resetter<? super T> resetter)
Construct an Pool using the
StandardProxyFactory. |
|
Pool(java.lang.Class<T> type,
Resetter<? super T> resetter,
ProxyFactory proxyFactory)
Construct a populated Pool with a specific proxy factory.
|
|
Pool(java.lang.Class<T> type,
Resetter<? super T> resetter,
ProxyFactory proxyFactory,
SerializationMode mode)
Construct a populated Pool with a specific proxy factory and a serialization mode.
|
| Modifier and Type | Method and Description |
|---|---|
void |
add(T... instances)
Add an array of new instances as resources to the pool.
|
static <T> Pool.PoolResettedBy<T> |
create(java.lang.Class<T> type)
Creates a factory for a pool instance which proxy the managed elements in the pool.
|
T |
get()
Get an instance from the pool.
|
int |
getAvailable()
Return the number of available instances of the pool.
|
private void |
readObject(java.io.ObjectInputStream in) |
void |
release(T object)
Release a pool instance manually.
|
private void |
returnInstanceToPool(ObjectReference<T> reference) |
int |
size()
Retrieve the number of instances managed by the pool.
|
private void |
writeObject(java.io.ObjectOutputStream out) |
private static final long serialVersionUID
private static final java.lang.reflect.Method returnInstanceToPool
private java.lang.Class<?>[] types
private ProxyFactory factory
private transient java.util.List<ObjectReference<T>> availableInstances
private SerializationMode serializationMode
public Pool(java.lang.Class<T> type)
StandardProxyFactory for elements that do not have to
be resetted.type - the type of the instancespublic Pool(java.lang.Class<T> type, Resetter<? super T> resetter)
StandardProxyFactory.type - the type of the instancesresetter - the resetter of the pooled elementspublic Pool(java.lang.Class<T> type, ProxyFactory proxyFactory)
type - the type of the instancesproxyFactory - the proxy factory to usepublic Pool(java.lang.Class<T> type, Resetter<? super T> resetter, ProxyFactory proxyFactory)
type - the type of the instancesresetter - the resetter of the pooled elementsproxyFactory - the proxy factory to usepublic Pool(java.lang.Class<T> type, Resetter<? super T> resetter, ProxyFactory proxyFactory, SerializationMode mode)
SerializationMode.STANDARD: the standard mode, i.e. all elements of the pool
are also serialized and a NotSerializableException may thrownSerializationMode.NONE: no element of the pool is also serialized and it must
be populated again after serializationSerializationMode.FORCE: all element of the pool are serialized, if possible.
Otherwise the pool is empty after serialization and must be populated again.type - the type of the instancesresetter - the resetter of the pooled elementsproxyFactory - the proxy factory to usemode - the serialization mode.private Pool()
public static <T> Pool.PoolResettedBy<T> create(java.lang.Class<T> type)
type - the type of the instancespublic void add(T... instances)
instances - the instancesjava.lang.NullPointerException - if instance is nullpublic T get()
System.gc() first.public void release(T object)
object - the instance to releasejava.lang.ClassCastException - if object was not Poolable.java.lang.IllegalArgumentException - if the object was not from this pool.public int getAvailable()
System.gc() first. The pool's monitor
will be notified, if any object was collected and the Resetter returned the object.public int size()
private void returnInstanceToPool(ObjectReference<T> reference)
private void writeObject(java.io.ObjectOutputStream out)
throws java.io.IOException
java.io.IOExceptionprivate void readObject(java.io.ObjectInputStream in)
throws java.io.IOException,
java.lang.ClassNotFoundException
java.io.IOExceptionjava.lang.ClassNotFoundException