public class SoftReferenceObjectPool<T> extends BaseObjectPool<T> implements ObjectPool<T>
构造器和说明 |
---|
SoftReferenceObjectPool()
已过时。
to be removed in pool 2.0. Use
SoftReferenceObjectPool(PoolableObjectFactory) . |
SoftReferenceObjectPool(PoolableObjectFactory<T> factory)
Create a
SoftReferenceObjectPool with the specified factory. |
SoftReferenceObjectPool(PoolableObjectFactory<T> factory,
int initSize)
已过时。
because this is a SoftReference pool, prefilled idle obejects may be garbage collected before they are used.
To be removed in Pool 2.0.
|
限定符和类型 | 方法和说明 |
---|---|
void |
addObject()
Create an object, and place it into the pool.
|
T |
borrowObject()
Borrow 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 |
getNumActive()
Return the number of instances currently borrowed from this pool.
|
int |
getNumIdle()
Returns an approximation not less than the of the number of idle instances in the 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 SoftReferenceObjectPool()
SoftReferenceObjectPool(PoolableObjectFactory)
.SoftReferenceObjectPool
without a factory.
setFactory
should be called
before any attempts to use the pool are made.
Generally speaking you should prefer the SoftReferenceObjectPool(PoolableObjectFactory)
constructor.public SoftReferenceObjectPool(PoolableObjectFactory<T> factory)
SoftReferenceObjectPool
with the specified factory.factory
- object factory to use.@Deprecated public SoftReferenceObjectPool(PoolableObjectFactory<T> factory, int initSize) throws java.lang.Exception, java.lang.IllegalArgumentException
SoftReferenceObjectPool
with the specified factory and initial idle object count.factory
- object factory to use.initSize
- initial size to attempt to prefill the pool.java.lang.Exception
- when there is a problem prefilling the pool.java.lang.IllegalArgumentException
- when factory
is null
.public T borrowObject() throws java.lang.Exception
Borrow an object from the pool. If there are no idle instances available in the pool, the configured
factory's PoolableObjectFactory.makeObject()
method is invoked to create a new instance.
All instances are activated
and
validated
before being returned by this
method. If validation fails or an exception occurs activating or validating an idle instance,
the failing instance is destroyed
and another
instance is retrieved from the pool, validated and activated. This process continues until either the
pool is empty or an instance passes validation. If the pool 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 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.util.NoSuchElementException
- if a valid object cannot be providedjava.lang.IllegalStateException
- if invoked on a closed
pooljava.lang.Exception
- if an exception occurs creating a new instancepublic void returnObject(T obj) throws java.lang.Exception
BaseObjectPool
returnObject
在接口中 ObjectPool<T>
returnObject
在类中 BaseObjectPool<T>
obj
- instance to return to the pooljava.lang.Exception
public 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 void addObject() throws java.lang.Exception
Create an object, and place it into the pool. addObject() 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.
addObject
在接口中 ObjectPool<T>
addObject
在类中 BaseObjectPool<T>
java.lang.IllegalStateException
- if invoked on a closed
pooljava.lang.Exception
- when the factory
has a problem creating or passivating an object.public int getNumIdle()
getNumIdle
在接口中 ObjectPool<T>
getNumIdle
在类中 BaseObjectPool<T>
public int getNumActive()
getNumActive
在接口中 ObjectPool<T>
getNumActive
在类中 BaseObjectPool<T>
public void clear()
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 swallowed@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.