Specific Type ObjectPool

原创 2006年06月10日 14:57:00

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

}

相关文章推荐

ObjectPool_v1.0

  • 2008年08月01日 15:16
  • 63KB
  • 下载

PoolableObjectFactory和ObjectPool

commons-pool包里主要包括三个重要的接口: ObjectPool用于管理要被池化的对象的借出和归还; ObjectPoolFactory用于大量生成相同类型和设置的ObjectPool...

使用对象池(ObjectPool)方式处理子弹的发射逻辑

对应对象池个概念,网上有很多,而且关于在unity里面使用对象池的文章也很多,我这里就直接上代码好了: using UnityEngine; using System.Collect...

Unity优化之ObjectPool-关于初始化

在Unity制作游戏的使用过程中为避免卡顿经常会使用ObjectPool.但要记得PaticleSystem和TrailRender的初始化....
  • ainn_pp
  • ainn_pp
  • 2014年08月22日 14:34
  • 1067

ObjectPool

ObjectPool ObjectPool 定义了一个简单的池接口 BaseObjectPool以下三个类的父类     A simple base implementa...

Unity3D对象池技术(ObjectPool)小例子

ObjectPool 对象池脚本这里面用继承单例框架脚本,此脚本在我的另一篇博客上单例框架脚本using UnityEngine; using System.Collections; using Sy...

MPAndroidChart 教程:图表的具体设置 Specific chart settings(六)

本章将重点介绍各个图表类型特定的设置。线状图,柱状图,散点图,烛状图 & 气泡图 : setAutoScaleMinMaxEnabled(boolean enabled) : 标志,指示自动缩放在y轴...

【Boost】boost库中thread多线程详解9——thread_specific_ptr线程局部存储

大多数函数都不是可重入的。这也就是说在某一个线程已经调用了一个函数时,如果你再调用同一个函数,那么这样是不安全的。一个不可重入的函数通过连续的调用来保存静态变量或者是返回一个指向静态数据的指针。 举例...

Thread-Specific Storage

This article told the last pattern illustrated in POSA2 - Thread Specific Storage, which is such a c...
  • qwidget
  • qwidget
  • 2011年07月30日 13:51
  • 825

Using AutoHotKey to open a specific OneNote page

Using AutoHotKey to open a specific OneNote page John Guin John Guin Microsoft ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Specific Type ObjectPool
举报原因:
原因补充:

(最多只允许输入30个字)