FileDocCategorySizeDatePackage
ObjectReturner.javaAPI DocExample1223Thu Nov 08 00:22:38 GMT 2001com.ora.rmibook.chapter12.pool.thread2

ObjectReturner.java

package com.ora.rmibook.chapter12.pool.thread2;


import com.ora.rmibook.chapter12.pool.*;
import java.util.*;


public class ObjectReturner implements Runnable {
    private Vector _objectsToReturn;
    private ThreadedPool2 _owner;
    public ObjectReturner(ThreadedPool2 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();
            }
        }
    }
}