Jakarta Commons——对象池

来源:互联网 发布:人工智能预测彩票 编辑:程序博客网 时间:2024/06/05 21:52

作用

      主要提供对象池的服务, 需要:

              client(从池中取得对象和放回对象);

              ObjectFactory继承于BasePoolableObjectFactory,用于激活和创建对象;

              new ReaderUtil(new StackObjectPool(new StringBufferFactory()))

基本组件      

ObjectPool

ObjectPool defines a trivially simple pooling interface:

public interface ObjectPool {

   Object borrowObject();

   void returnObject(Object borrowed);

}

Some client classes won't integrate with Pool any more than this. Clients written to this interface can use arbitraryObjectPool implementations interchangeably.

BaseObjectPool provides an abstract base implementation of ObjectPool. Clients are encouraged but not required to extendBaseObjectPool for newObjectPool implementations.

KeyedObjectPool defines a similar interface for pools composed of heterogeneous objects:

public interface KeyedObjectPool {

   Object borrowObject(Object key);

   void returnObject(Object key, Object borrowed);

}

PoolableObjectFactory

The Pool package makes it possible separate the way in which instances are pooled from the way in which instances are created and destroyed.PoolableObjectFactory supports this by providing a generic interface for the lifecycle of a pooled object:

public interface PoolableObjectFactory {

   Object makeObject();

   void activateObject(Object obj);

   void passivateObject(Object obj);

   boolean validateObject(Object obj);

   void destroyObject(Object obj);

}

ObjectPool implementations may be written to accept arbitraryPoolableObjectFactorys. This makes is possible for clients to select pooling-behavior distinct from the kinds of objects that are pooled.

BasePoolableObjectFactory provides an abstract base implementation of PoolableObjectFactory that makes implementations a snap.

KeyedPoolableObjectFactory defines a similar interface for KeyedObjectPools:

public interface KeyedPoolableObjectFactory {

   Object makeObject(Object key);

   void activateObject(Object key, Object obj);

   void passivateObject(Object key, Object obj);

   boolean validateObject(Object key, Object obj);

   void destroyObject(Object key, Object obj);

}

BaseKeyedPoolableObjectFactory provides an abstract base implementation of KeyedPoolableObjectFactory that makes implementations a snap.

The org.apache.commons.pool.impl package provides somePool implementations.

StackObjectPool

StackObjectPool will pool a finite number of "idle" instances, but will create new instances a needed in order to support high demand.

StackKeyedObjectPool offers the same behavior for keyed pools.

GenericObjectPool

GenericObjectPool provides a wide variety of configuration options, including the ability to cap the number of idle or active instances, to evict instances as they sit idle in the pool, etc.

GenericKeyedObjectPool offers the same behavior for keyed pools.

SoftReferenceObjectPool

SoftReferenceObjectPool can grow as needed, but allows the garbage collector to evict idle instances from the pool as needed.

 

原创粉丝点击