package com.ora.rmibook.chapter12.pool.finalthread;
import java.util.*;
public class ObjectReturner implements Runnable {
private Vector _objectsToReturn;
private ThreadedPool_Final _owner;
public ObjectReturner(ThreadedPool_Final owner) {
_owner = owner;
_objectsToReturn = new Vector();
}
public void run() {
while (true) {
Object objectToReturn;
while (0 == _objectsToReturn.size()) {
synchronized (_objectsToReturn) {
try {
_objectsToReturn.wait();
} catch (InterruptedException e) {
}
}
}
int lastIndex = _objectsToReturn.size() - 1;
objectToReturn = _objectsToReturn.remove(lastIndex);
_owner.returnObjectToPool(objectToReturn);
}
}
public void validateAndReturn(Object object) {
synchronized (_objectsToReturn) {
_objectsToReturn.add(object);
if (1 == _objectsToReturn.size()) {
_objectsToReturn.notify();
}
}
}
}
|