me.release=function(obj){// 确保对象已经被释放if(availableObjects.some(function(objWithTimeout){return(objWithTimeout.obj===obj);})){log("release called twice for the same resource")return;}varobjWithTimeout={obj:obj,timeout:(newDate().getTime()+idleTimeoutMillis)};availableObjects.push(objWithTimeout);// 此时 availableObjects.size()=1log("timeout:..");dispense();scheduleRemoveIdle();// 计划移走idle items(空闲项目) 延迟1000ms 执行 removeIdle}
functionremoveIdle(){vartoRemove=[],now=newDate().getTime(),i,al,tr,timeout;removeIdleScheduled=false;// go through the available(idle) items, check if they have timed out;for(i=0,al=availableObjects.length;i<al&&(refreshIdle)||(count-factory.min>toRemove.length));i+=1){timeout=availableObjects[i].timeout;if(now>=timeout){//client timed out, so destroy it toRemove.push(availableObjects[i].obj);}}for(i=0,tr=toRemove.length;i<tr;i+=1){me.destroy(toRemove[i]);}al=availableObjects.length;if(al>0){log("..");scheduleRemoveIdle();}else{log("removeIdle() all objects removed",'verbose');}}