Specific Type ObjectPool

/*
*Because of the penalty of the performance, We'd better try it in another way.
*
*A generic pool requires acccess to the internal state variables of the managed objects,
*so that the objects can be initialized for reuse.
* If we want to hide state information or make it immutable in our objects,
*We can solve both these problems by building the object pool into the actual object class,
*rather than making it a separate add-on.
*
*Note that: "Immutable" is only from user's standpoint.
*/

import java.util.*;
import java.awt.*;

public class ImmutableRectangle{
     //Free pool capacity
     private static final int FREE_POOL_SIZE = 40;
     //Pool owned by class
     private static final ImmutableRectangle[] freeStack = new ImmutableRectangle[FREE_POOL_SIZE];
     private static int countFree;

     //Intinsic states of the rectangle
     private int xValue;
     private int yValue;
     private int widthValue;
     private int heightValue;

     private ImmutableRectangle(){}

     public static synchronized ImmutableRectangle getInstance(int x, int y, int width, int height){
          ImmutableRectangle result;
          //Check if the pool is empty, create a new object if so
          if (countFree ==0){
               result = new ImmutableRectangle();
               System.out.println("Create a new object.");
          }
          //Remove object from the end of free pool
          else{
               result = freeStack[--countFree];
               System.out.println("Get instance from the free pool.");
          }

          //Initialize the object to the specified state.
          result.xValue = x;
          result.yValue = y;
          result.widthValue = width;
          result.heightValue = height;

          return result;
     }

     public static ImmutableRectangle getInstance(int width, int height){
          return getInstance(0, 0, width, height);
     }

     public static ImmutableRectangle getInstance(Point p, Dimension d){
          return getInstance(p.x, p.y, d.width, d.height);
     }

     public static ImmutableRectangle getInstance(){
          return getInstance(0, 0, 0, 0);
     }

     public static synchronized void freeInstance(ImmutableRectangle rect){
          if (countFree < FREE_POOL_SIZE)
          freeStack[countFree++] = rect;
     }

     public int getX(){
          return xValue;
     }

     public int getY(){
          return yValue;
     }

     public int getWidth(){
          return widthValue;
     }

     public int getHeight(){
          return heightValue;
     }

     public static int getCountFree(){
          return countFree;
     }

}

class ImmutableRectangleTest{
     private int loopTimes = 100;
 
     private class AnotherThread extends Thread{
          public void run(){
               for (int i = 0; i < loopTimes; i++){
                    ImmutableRectangle rect = ImmutableRectangle.getInstance(i, i, i, i);
                    //Do something with the object
                    //………………
                    //Return "immutable" rectangle to pool.
                    ImmutableRectangle.freeInstance(rect);
               }
          }
     }
     AnotherThread t = new AnotherThread();

     public static void main(String[] args){
          ImmutableRectangleTest irt = new ImmutableRectangleTest();
          irt.t.start();

          int loopTimes = 100;
          for (int i = 0; i < loopTimes; i++){
               ImmutableRectangle rect = ImmutableRectangle.getInstance(i, i, i, i);
               //Do something with the object
               //………………
               //Return "immutable" rectangle to pool.
               ImmutableRectangle.freeInstance(rect);
          }

          try{
               irt.t.join();   
          }catch (InterruptedException e){}
          System.out.println("The number of objects in the pool at last is " + ImmutableRectangle.getCountFree());
     }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值