public class Pool<T> extends Object implements 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 FormModifier 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> |
Constructor and Description |
---|
Pool(Class<T> type)
Construct an Pool using the
StandardProxyFactory for elements that do not have to
be resetted. |
Pool(Class<T> type,
ProxyFactory proxyFactory)
Construct a populated Pool with a specific proxy factory for elements that do not have to
be resetted.
|
Pool(Class<T> type,
Resetter<? super T> resetter)
Construct an Pool using the
StandardProxyFactory . |
Pool(Class<T> type,
Resetter<? super T> resetter,
ProxyFactory proxyFactory)
Construct a populated Pool with a specific proxy factory.
|
Pool(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(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.
|
void |
release(T object)
Release a pool instance manually.
|
int |
size()
Retrieve the number of instances managed by the pool.
|
public Pool(Class<T> type)
StandardProxyFactory
for elements that do not have to
be resetted.type
- the type of the instancespublic Pool(Class<T> type, Resetter<? super T> resetter)
StandardProxyFactory
.type
- the type of the instancesresetter
- the resetter of the pooled elementspublic Pool(Class<T> type, ProxyFactory proxyFactory)
type
- the type of the instancesproxyFactory
- the proxy factory to usepublic Pool(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(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.public static <T> Pool.PoolResettedBy<T> create(Class<T> type)
type
- the type of the instancespublic void add(T... instances)
instances
- the instancesNullPointerException
- if instance is null
public T get()
System.gc()
first.public void release(T object)
object
- the instance to releaseClassCastException
- if object was not Poolable
.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()
Copyright © 2005–2015 Codehaus. All rights reserved.