Retrieve a resource from the pool
while(!quit) {
// first, try to return a resource from the pool
if (!inResources.isEmpty()) {
Object o = inResources.remove(0);
// if the resource is invalid, create a replacement
if(!factory.validateResource(o))
o = factory.createResource();
outResources.add(o);
return o;
}
// next, create a new resource if we haven't
// reached the limit yet
if(curObjects < maxObjects) {
Object o = factory.createResource();
outResources.add(o);
curObjects++;
return o;
}
// if no resources are available, wait until one
// is returned
try { wait(); } catch(Exception ex) {}
}
// pool is destroyed
return null;