/** constructor */
public ObjectPool(int size) {
this.size = size;
this.in = new Vector(size);
this.out = new Vector(size);
}
/** factory method to create new Object for
the pool */
protected PoolObject newPoolObject() throws
RemoteException {
return new PoolObject();
}
/** check out an Object from the pool */
public synchronized Object checkOut() {
Object o = null;
if (out.size() < size) {
if (in.isEmpty()) {
try {
o = newPoolObject();
}
catch (RemoteException e) {
}
}
else {
o = in.elementAt(0);
in.removeElementAt(0);
}
out.addElement(o);
}
return o;
}
/** check an Object back into the pool */
public synchronized void checkIn(Object o)
{
int x = out.indexOf(o);
/* rely on Vector's use of equals() method
** to equate stubs with remote objects if
necessary
*/
if (x < 0) throw new NoSuchElementException();
Object oo = out.elementAt(x);
/* get the real Object, not a stub */
out.removeElementAt(x);
in.addElement(oo);
}
}
Listing 2.
public XXPool extends ObjectPool {
class XXObject extends ObjectPool.PoolObject
implements Remote {
... // instance variables and methods for
your Object
XXObject() throws RemoteException {}
}
public XXPool(int size) {
super(size);
}
protected PoolObject newPoolObject() throws
RemoteException {
return new XXObject();
}
}
Listing 3.
public XXPool {
static public class XXObject implements Serializable
{
private Object poolObject;
... // instance variables and methods for
your Object
XXObject(Object poolObject) {
this.poolObject = poolObject;
}
}
ObjectPool objectPool;
public XXPool(int size) {
this.objectPool = new ObjectPool(size);
}
public XXObject checkOut() {
Object o = objectPool.checkOut();
if (o == null) return null;
return new XXObject(o);
}
public void checkIn(XXObject o) {
objectPool.checkIn(o.poolObject);
}
}