你需要的是一个对象池。查看 Apache Commons Pool 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
}
}
如果这不是您想要的,那么您将需要提供有关您的商业图书馆的更多详细信息。