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

}

Unity3d中对象池(ObjectPool)的实现

转自:http://gad.qq.com/article/detail/7172110
  • u010377179
  • u010377179
  • 2016年10月26日 08:25
  • 1169

走近“领域特定语言”(Domain-Specific Languages)

领域特定语言(Domain-Specific Languages,简称DSL),是一个很有用的术语和概念,但其边界很模糊。一些东西很明显是DSL,但另一些可能会引发争议。本文接下来就走近“领域特定语言...
  • hzbooks
  • hzbooks
  • 2013年03月26日 14:50
  • 5686

sqoop导入导出表/数据到Hive

笔记: 将关系型数据库表及表中的数据复制到hive中 sqoop import : RMDBS——>hive 语法: sqoop import --connect jdbc:mysql://IP:PO...
  • qq_26840065
  • qq_26840065
  • 2016年04月22日 15:26
  • 2537

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

对应对象池个概念,网上有很多,而且关于在unity里面使用对象池的文章也很多,我这里就直接上代码好了: using UnityEngine; using System.Collect...
  • qq_28221881
  • qq_28221881
  • 2016年10月27日 14:48
  • 964

PoolableObjectFactory和ObjectPool

commons-pool包里主要包括三个重要的接口: ObjectPool用于管理要被池化的对象的借出和归还; ObjectPoolFactory用于大量生成相同类型和设置的ObjectPool...
  • Erica_1230
  • Erica_1230
  • 2014年07月06日 17:27
  • 529

Unity优化之ObjectPool-关于初始化

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

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

ObjectPool 对象池脚本这里面用继承单例框架脚本,此脚本在我的另一篇博客上单例框架脚本using UnityEngine; using System.Collections; using Sy...
  • frank901
  • frank901
  • 2017年06月01日 14:31
  • 191

ObjectPool

ObjectPool ObjectPool 定义了一个简单的池接口 BaseObjectPool以下三个类的父类     A simple base implementa...
  • wei_ya_wen
  • wei_ya_wen
  • 2013年04月15日 09:15
  • 1044

ObjectPool_v1.0

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

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

本章将重点介绍各个图表类型特定的设置。线状图,柱状图,散点图,烛状图 & 气泡图 : setAutoScaleMinMaxEnabled(boolean enabled) : 标志,指示自动缩放在y轴...
  • u014136472
  • u014136472
  • 2015年12月22日 17:12
  • 10202
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Specific Type ObjectPool
举报原因:
原因补充:

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