/*
*Free object pools represent a form of object reuse.
*With the free pool approach, code using objects of the pooled type must track usage and
*explicitly return the objects to the free pool when usage is completed.
*
*When an object is needed, the free pool removes one from tha available collection and
*reinitializes it, rather than constructing a new object.
*The free pool only constructs a new object of the pooled type when the available collection is empty.
*/
/*
*Unfortunately, it's slower than just allocatiing the Rectangle object directly!!
*All the generic code (esp the extensive use of type casts) along with the synchronization of the LinkedList class,
*adds so much overhead to bookkeeping.
*So, a common problem with generic code -- it provides code reuse, but generally with a performance penalty.
*/
//This is a generic code of the ObjectPool
import java.util.*;
import java.awt.*;
public class ObjectPool{
private final Class objectType;
private final LinkedList freeStack;
//Designate a specific type of the objects pulled into the object pool.
public ObjectPool(Class type){
objectType = type;
freeStack = new LinkedList();
}
public int getPoolSize(){
return freeStack.size();
}
public synchronized Object getInstance(){
//Check if the pool is empty, create a new object if so
//try……catch…… blocks are needed when it creates new object.
if (freeStack.isEmpty()){
try{
System.out.println("Create a new object.");
return objectType.newInstance();
}
catch (InstantiationException e) {}
catch (IllegalAccessException e) {}
//Throw unchecked exception for error in pool configuration.
throw new RuntimeException ("Failed to create a new object for pool.");
}
else{
//Remove object from the end of free pool
Object result = freeStack.removeLast();
System.out.println("Get instance from the free pool.");
return result;
}
}
//return the objects to the free pool when usage is completed
public synchronized void freeInstance(Object obj){
if (objectType.isInstance(obj)) freeStack.add(obj);
else throw new IllegalArgumentException("Argument type invalid for pool");
}
}
class ObjectPoolTest{
private static final ObjectPool rectanglePool = new ObjectPool(Rectangle.class);
private static Rectangle getRectangleObject(int height, int width, int x, int y){
Rectangle rect = (Rectangle)rectanglePool.getInstance();
rect.height = height;
rect.width = width;
rect.x = x;
rect.y = y;
return rect;
}
class AnotherThread extends Thread{
private int loopTimes = 10;
AnotherThread(){
super("Another Thread");
}
public void run(){
for (int i = 0; i < loopTimes; i++){
//Construct a rectangle to b passed.
Rectangle rect = getRectangleObject(i, i, i, i);
//Do something with the object
//…………………………
//Return passed rectangle to pool.
rectanglePool.freeInstance(rect);
}
}
}
AnotherThread t = new AnotherThread();
public static void main(String[] args){
ObjectPoolTest opt = new ObjectPoolTest();
int loopTimes =10;
opt.t.start();
for (int i = 0; i < loopTimes; i++){
//Construct a rectangle to b passed.
Rectangle rect = getRectangleObject(i, i, i, i);
//Do something with the object
//…………………………
//Return passed rectangle to pool.
rectanglePool.freeInstance(rect);
}
try{
opt.t.join();
}catch (InterruptedException e){}
System.out.println("The number of objects in the pool at last is " + rectanglePool.getPoolSize());
}
}