public class StackObjectPool<T> extends BaseObjectPool<T> implements ObjectPool<T>
| 构造器和说明 |
|---|
StackObjectPool()
已过时。
to be removed in pool 2.0 - use
StackObjectPool(PoolableObjectFactory) |
StackObjectPool(int maxIdle)
已过时。
to be removed in pool 2.0 - use
StackObjectPool(PoolableObjectFactory, int) |
StackObjectPool(int maxIdle,
int initIdleCapacity)
已过时。
to be removed in pool 2.0 - use
StackObjectPool(PoolableObjectFactory, int, int) |
StackObjectPool(PoolableObjectFactory<T> factory)
Create a new StackObjectPool using the specified factory to create new instances.
|
StackObjectPool(PoolableObjectFactory<T> factory,
int maxIdle)
Create a new SimpleObjectPool using the specified factory to create new instances,
capping the number of "sleeping" instances to maxIdle.
|
StackObjectPool(PoolableObjectFactory<T> factory,
int maxIdle,
int initIdleCapacity)
Create a new StackObjectPool using the specified
factory to create new instances,
capping the number of "sleeping" instances to maxIdle, and initially allocating a container
capable of containing at least initIdleCapacity instances. |
| 限定符和类型 | 方法和说明 |
|---|---|
void |
addObject()
Create an object, and place it on top of the stack.
|
T |
borrowObject()
Borrows an object from the pool.
|
void |
clear()
Clears any objects sitting idle in the pool.
|
void |
close()
Close this pool, and free any resources associated with it.
|
PoolableObjectFactory<T> |
getFactory()
Returns the
PoolableObjectFactory used by this pool to create and manage object instances. |
int |
getMaxSleeping()
Returns the maximum number of idle instances in the pool.
|
int |
getNumActive()
Return the number of instances currently borrowed from this pool.
|
int |
getNumIdle()
Return the number of instances
currently idle in this pool.
|
void |
invalidateObject(T obj)
Invalidates an object from the pool.
|
void |
returnObject(T obj)
Returns an instance to the pool.
|
void |
setFactory(PoolableObjectFactory<T> factory)
已过时。
to be removed in pool 2.0
|
isClosed@Deprecated public StackObjectPool()
StackObjectPool(PoolableObjectFactory)set the factory or
else this pool will not behave correctly. Clients may first populate the pool
using returnObject(java.lang.Object) before they can be borrowed
but this usage is discouraged.@Deprecated public StackObjectPool(int maxIdle)
StackObjectPool(PoolableObjectFactory, int)set the factory or
else this pool will not behave correctly. Clients may first populate the pool
using returnObject(java.lang.Object) before they can be borrowed
but this usage is discouraged.maxIdle - cap on the number of "sleeping" instances in the poolStackObjectPool(PoolableObjectFactory, int)@Deprecated
public StackObjectPool(int maxIdle,
int initIdleCapacity)
StackObjectPool(PoolableObjectFactory, int, int)set the factory or
else this pool will not behave correctly. Clients may first populate the pool
using returnObject(java.lang.Object) before they can be borrowed
but this usage is discouraged.maxIdle - cap on the number of "sleeping" instances in the poolinitIdleCapacity - initial size of the pool (this specifies the size of the container,
it does not cause the pool to be pre-populated.)StackObjectPool(PoolableObjectFactory, int, int)public StackObjectPool(PoolableObjectFactory<T> factory)
factory - the PoolableObjectFactory used to populate the poolpublic StackObjectPool(PoolableObjectFactory<T> factory, int maxIdle)
factory - the PoolableObjectFactory used to populate the poolmaxIdle - cap on the number of "sleeping" instances in the poolpublic StackObjectPool(PoolableObjectFactory<T> factory, int maxIdle, int initIdleCapacity)
Create a new StackObjectPool using the specified factory to create new instances,
capping the number of "sleeping" instances to maxIdle, and initially allocating a container
capable of containing at least initIdleCapacity instances. The pool is not pre-populated.
The initIdleCapacity parameter just determines the initial size of the underlying
container, which can increase beyond this value if maxIdle > initIdleCapacity.
Negative values of maxIdle are ignored (i.e., the pool is created using
DEFAULT_MAX_SLEEPING) as are non-positive values for initIdleCapacity.
factory - the PoolableObjectFactory used to populate the poolmaxIdle - cap on the number of "sleeping" instances in the poolinitIdleCapacity - initial size of the pool (this specifies the size of the container,
it does not cause the pool to be pre-populated.)public T borrowObject() throws java.lang.Exception
Borrows an object from the pool. If there are idle instances available on the stack,
the top element of the stack is popped to activate, validate and return to the client. If there
are no idle instances available, the makeObject
method of the pool's PoolableObjectFactory is invoked to create a new instance.
All instances are activated and
validated before being returned to the
client. If validation fails or an exception occurs activating or validating an instance
popped from the idle instance stack, the failing instance is
destroyed and the next instance on
the stack is popped, validated and activated. This process continues until either the
stack is empty or an instance passes validation. If the stack is empty on activation or
it does not contain any valid instances, the factory's makeObject method is used
to create a new instance. If a null instance is returned by the factory or the created
instance either raises an exception on activation or fails validation, NoSuchElementException
is thrown. Exceptions thrown by MakeObject are propagated to the caller; but
other than ThreadDeath or VirtualMachineError, exceptions generated by
activation, validation or destroy methods are swallowed silently.
borrowObject 在接口中 ObjectPool<T>borrowObject 在类中 BaseObjectPool<T>java.lang.Exception - if an instance cannot be obtained from the poolpublic void returnObject(T obj) throws java.lang.Exception
BaseObjectPoolreturnObject 在接口中 ObjectPool<T>returnObject 在类中 BaseObjectPool<T>obj - instance to return to the pooljava.lang.Exceptionpublic void invalidateObject(T obj) throws java.lang.Exception
Invalidates an object from the pool.
By contract, obj must have been obtained
using borrowObject or a related method as defined in
an implementation or sub-interface.
This method should be used when an object that has been borrowed is determined (due to an exception or other problem) to be invalid.
invalidateObject 在接口中 ObjectPool<T>invalidateObject 在类中 BaseObjectPool<T>obj - a borrowed instance to be disposed.java.lang.Exception - Exceptionpublic int getNumIdle()
getNumIdle 在接口中 ObjectPool<T>getNumIdle 在类中 BaseObjectPool<T>public int getNumActive()
getNumActive 在接口中 ObjectPool<T>getNumActive 在类中 BaseObjectPool<T>public void clear()
PoolableObjectFactory.destroyObject(Object).clear 在接口中 ObjectPool<T>clear 在类中 BaseObjectPool<T>public void close()
throws java.lang.Exception
Close this pool, and free any resources associated with it. Invokes
clear() to destroy and remove instances in the pool.
Calling addObject() or borrowObject() after invoking
this method on a pool will cause them to throw an
IllegalStateException.
close 在接口中 ObjectPool<T>close 在类中 BaseObjectPool<T>java.lang.Exception - never - exceptions clearing the pool are swallowedpublic void addObject()
throws java.lang.Exception
Create an object, and place it on top of the stack. This method is useful for "pre-loading" a pool with idle objects.
Before being added to the pool, the newly created instance is
validated and
passivated. If validation
fails, the new instance is destroyed.
Exceptions generated by the factory makeObject or passivate are
propagated to the caller. Exceptions destroying instances are silently swallowed.
If a new instance is created and successfully validated and passivated and adding this
instance to the pool causes maxSleeping to be exceeded, the oldest
(bottom) instance in the pool is destroyed to make room for the newly created instance, which
is pushed on top of the stack.
addObject 在接口中 ObjectPool<T>addObject 在类中 BaseObjectPool<T>java.lang.Exception - when the factory has a problem creating or passivating an object.@Deprecated public void setFactory(PoolableObjectFactory<T> factory) throws java.lang.IllegalStateException
factory this pool uses
to create new instances. Trying to change
the factory while there are borrowed objects will
throw an IllegalStateException.setFactory 在接口中 ObjectPool<T>setFactory 在类中 BaseObjectPool<T>factory - the PoolableObjectFactory used to create new instances.java.lang.IllegalStateException - when the factory cannot be set at this timepublic PoolableObjectFactory<T> getFactory()
PoolableObjectFactory used by this pool to create and manage object instances.public int getMaxSleeping()