ObjectPool

/*
*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());
      }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值