你需要的是一個ObjectPool。看看Apache Commons池http://commons.apache.org/pool
在應用程序啓動時,您應該創建一個包含商業庫的許可證或對象的對象池(不知道它們具有什麼樣的公共接口)。
public class CommercialObjectFactory extends BasePoolableObjectFactory {
// for makeObject we'll simply return a new commercial object
@Override
public Object makeObject() {
return new CommercialObject();
}
}
GenericObjectPool pool = new GenericObjectPool(new CommercialObjectFactory());
// The size of pool in our case it is N
pool.setMaxActive(N)
// We want to wait if the pool is exhausted
pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK)
而當你需要在你的代碼中的商業對象。
CommercialObject obj = null;
try {
obj = (CommercialObject)pool.borrowObject();
// use the commerical object the way you to use it.
// ....
} finally {
// be nice return the borrwed object
try {
if(obj != null) {
pool.returnObject(obj);
}
} catch(Exception e) {
// ignored
}
}
如果這不是你想要的,那麼你將需要提供更多關於你的商業圖書館的細節。
來源
2008-12-22 06:59:10
TRF