|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.sadun.util.pool.ObjectPool
An object pool, which holds n identical copies of an object and allows access and release of each of them.
An object can be created by providing its class - in which case the default constructor will be used to create the instances; or by providing the construction parameters as an object array.
Optionally, after creation, a Setup object
can
be used to differentiate or further set up the created object.
This version supports Enterprise Java Beans creation via a JNDI name and home interface
Nested Class Summary | |
static class |
ObjectPool.BaseFactory
A base implementation of ExtendedObjectPool.Factory
relying on reflection, holding a Class object, an optional parameter array
and an optional Setup object.
|
static interface |
ObjectPool.Factory
If an object to be pooled can't be constructed directly by invoking a constructor, a Factory can be provided to do the construction job. |
static class |
ObjectPool.ObjectFactory
An ExtendedObjectPool.Factory implementation which uses reflection
to create instances of a certain class.
|
static class |
ObjectPool.ObjectPoolException
An exception thrown in case of pooling operation errors. |
protected static class |
ObjectPool.PooledObjectWrapper
|
Field Summary | |
protected java.util.Set |
free
|
protected ObjectPool.PooledObjectWrapper[] |
pool
|
protected java.util.Set |
used
|
Constructor Summary | |
ObjectPool(int n,
java.lang.Class cls)
Create a pool of n object of the given class using the default constructor. |
|
ObjectPool(int n,
java.lang.Class cls,
java.lang.Object[] params)
Create a pool of n object of the given class using the given construction parameters. |
|
ObjectPool(int n,
java.lang.Class cls,
java.lang.Object[] params,
Setup ps)
Create a pool of n object of the given class using the given construction parameters. |
|
ObjectPool(int n,
ObjectPool.Factory factory)
Create a pool of n objects using the given factory |
|
ObjectPool(int n,
java.lang.String clsName)
Create a pool of n object of the given class (by name) using the default constructor. |
|
ObjectPool(int n,
java.lang.String clsName,
java.lang.Object[] params)
Create a pool of n object of the given class (by name) using the given construction parameters. |
|
ObjectPool(int n,
java.lang.String clsName,
java.lang.Object[] params,
Setup ps)
Create a pool of n object of the given class (by name) using the given construction parameters. |
Method Summary | |
java.lang.Object |
acquire()
Attempt to acquire an object. |
java.lang.Object |
acquire(boolean waitIfUnavailable)
Attempt to acquire an object. |
java.lang.Object |
acquire(int i)
Attempt to acquire the i-th object. |
void |
destroy(boolean waitForReleasedObjects)
Destroys all the objects in the pool by invoking the Factory.destroy() method
on the pool's object factory. |
protected ObjectPool.Factory |
getFactory()
Returns the factory. |
int |
getFreeCount()
Return the number of available objects in the pool |
java.lang.Class |
getObjectType()
Return the object type pooled by this pool |
int |
getSize()
Return the size of the pool |
int |
getUsedCount()
Return the number of used objects in the pool |
static ObjectPool |
newPool(int poolSize,
java.lang.String clsName)
A convenience method to create a new pool of objects of a given class, by name, using their default constructor. |
static ObjectPool |
newPool(int poolSize,
java.lang.String clsName,
java.lang.Object[] params)
A convenience method to create a new pool of objects of a given class, by name, using the given parameters. |
void |
release(int i)
Releases the i-th object, notifying the waiting thread (if any) that one pooled object has become available. |
void |
release(java.lang.Object obj)
Release an object, notifying waiting thread (if any) that one pooled object has become available. |
void |
release(java.lang.Object obj,
boolean releaseWaitingCalls)
Release an object. |
java.lang.Object |
renew(java.lang.Object obj)
Renew one object in the pool. |
protected void |
setFactory(ObjectPool.Factory factory)
Sets the factory. |
java.lang.String |
toString()
Return a string description of the pool |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
protected java.util.Set free
protected java.util.Set used
protected ObjectPool.PooledObjectWrapper[] pool
Constructor Detail |
public ObjectPool(int n, ObjectPool.Factory factory)
public ObjectPool(int n, java.lang.String clsName, java.lang.Object[] params, Setup ps) throws java.lang.ClassNotFoundException
If some post-construction setup is needed, it can be provided as an Setup object.
n
- the size of the poolclsName
- the name of the class of the objects to poolparams
- the construction parameters, or null
java.lang.ClassNotFoundException
- if the given class name cannot be resolvedpublic ObjectPool(int n, java.lang.String clsName, java.lang.Object[] params) throws java.lang.ClassNotFoundException
n
- the size of the poolclsName
- the name of the class of the objects to poolparams
- the construction parameters, or null
java.lang.ClassNotFoundException
- if the given class name cannot be resolvedpublic ObjectPool(int n, java.lang.String clsName) throws java.lang.ClassNotFoundException
n
- the size of the poolclsName
- the name of the class of the objects to pool
java.lang.ClassNotFoundException
- if the given class name cannot be resolvedpublic ObjectPool(int n, java.lang.Class cls, java.lang.Object[] params, Setup ps)
If some post-construction setup is needed, it can be provided as an Setup object.
n
- the size of the poolcls
- the class of the objects to poolparams
- the construction parameters, or nullpublic ObjectPool(int n, java.lang.Class cls, java.lang.Object[] params)
n
- the size of the poolcls
- the class of the objects to poolparams
- the construction parameters, or nullpublic ObjectPool(int n, java.lang.Class cls)
n
- the size of the poolcls
- the class of the objects to poolMethod Detail |
public int getFreeCount()
public int getUsedCount()
public int getSize()
public java.lang.Object acquire(boolean waitIfUnavailable)
waitIfUnavailable
- if true, in case all the pooled objects
are used, the call will block until an object is released.
If false, in the same condition the method returns null.
public java.lang.Object acquire()
If there aren't any objects available, this method blocks until one becomes available.
public java.lang.Object acquire(int i)
If the object is not available, this method blocks until the object becomes available.
public void release(int i)
public void release(java.lang.Object obj, boolean releaseWaitingCalls)
releaseWaitingCalls
- if true, the method notifies waiting objects that
one pooled object has become available.public void release(java.lang.Object obj)
public java.lang.Object renew(java.lang.Object obj)
A new instance is created substituting the passed object in the pool, and the new instance is returned. The object is released() but any thread waiting on that object shifts waiting for another object.
public java.lang.String toString()
public static ObjectPool newPool(int poolSize, java.lang.String clsName) throws ObjectPool.ObjectPoolException
poolSize
- the size of the poolclsName
- the name of the class of the pooled objects
ObjectPool.ObjectPoolException
- if the pool cannot be initializedpublic static ObjectPool newPool(int poolSize, java.lang.String clsName, java.lang.Object[] params) throws ObjectPool.ObjectPoolException
poolSize
- the size of the poolclsName
- the name of the class of the pooled objectsparams
- the parameters used to initialize the objects of the pool
ObjectPool.ObjectPoolException
- if the pool cannot be initializedpublic java.lang.Class getObjectType()
protected ObjectPool.Factory getFactory()
protected void setFactory(ObjectPool.Factory factory)
factory
- The factory to setpublic void destroy(boolean waitForReleasedObjects)
Factory.destroy()
method
on the pool's object factory.
waitForReleasedObjects
- waits until all the objects are released before
destroying them
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |