home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 139 / dpcs0999.iso / Web / CFserver / data1.cab / Java / CFJava.cab / CFJavaRuntime.cab / netscape / application / ObjectPool.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-10-01  |  2.3 KB  |  94 lines

  1. package netscape.application;
  2.  
  3. class ObjectPool {
  4.    Object[] freePool;
  5.    int freePoolMaxLength;
  6.    int freePoolNextSlot;
  7.    Class objectClass;
  8.    int allocSaved;
  9.    int allocDone;
  10.    int maxCapacity;
  11.  
  12.    public ObjectPool(String var1) {
  13.       this(var1, 32);
  14.    }
  15.  
  16.    public ObjectPool(String var1, int var2) {
  17.       this.freePool = new Object[1];
  18.       this.freePoolMaxLength = 1;
  19.       this.freePoolNextSlot = 0;
  20.  
  21.       try {
  22.          this.objectClass = Class.forName(var1);
  23.       } catch (ClassNotFoundException var3) {
  24.          System.out.println("ObjectPool cannot find class " + var1);
  25.       }
  26.  
  27.       this.allocSaved = 0;
  28.       this.allocDone = 0;
  29.       this.maxCapacity = var2;
  30.    }
  31.  
  32.    public Object allocateObject() {
  33.       Object var1 = null;
  34.       synchronized(this){}
  35.  
  36.       try {
  37.          if (this.freePoolNextSlot > 0) {
  38.             --this.freePoolNextSlot;
  39.             var1 = this.freePool[this.freePoolNextSlot];
  40.          }
  41.       } catch (Throwable var8) {
  42.          throw var8;
  43.       }
  44.  
  45.       if (var1 == null) {
  46.          ++this.allocDone;
  47.  
  48.          try {
  49.             var1 = this.objectClass.newInstance();
  50.          } catch (InstantiationException var6) {
  51.             System.out.println("Cannot instantiate instance of class " + this.objectClass);
  52.          } catch (IllegalAccessException var7) {
  53.             System.out.println("Cannot instantiate instance of class. Illegal." + this.objectClass);
  54.          }
  55.       } else {
  56.          ++this.allocSaved;
  57.       }
  58.  
  59.       return var1;
  60.    }
  61.  
  62.    public void recycleObject(Object var1) {
  63.       synchronized(this){}
  64.  
  65.       try {
  66.          if (this.freePoolMaxLength < this.maxCapacity) {
  67.             if (this.freePoolNextSlot == this.freePoolMaxLength) {
  68.                Object[] var4 = new Object[this.freePoolMaxLength * 2];
  69.                System.arraycopy(this.freePool, 0, var4, 0, this.freePoolMaxLength);
  70.                this.freePool = var4;
  71.                this.freePoolMaxLength *= 2;
  72.             }
  73.  
  74.             this.freePool[this.freePoolNextSlot++] = var1;
  75.          }
  76.       } catch (Throwable var6) {
  77.          throw var6;
  78.       }
  79.  
  80.    }
  81.  
  82.    protected void finalize() {
  83.       for(int var1 = 0; var1 < this.freePoolNextSlot; ++var1) {
  84.          this.freePool[var1] = null;
  85.       }
  86.  
  87.       this.freePool = null;
  88.    }
  89.  
  90.    public String toString() {
  91.       return "Object pool for class " + this.objectClass + " has " + this.freePoolNextSlot + " instances." + " " + this.allocSaved + " allocations avoided allocation performed:" + this.allocDone;
  92.    }
  93. }
  94.