憨厚生

----Java's Slave----
***Java's Host***

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  165 随笔 :: 17 文章 :: 90 评论 :: 0 Trackbacks

#

业余时间写了有关对象池方面的程序,不足之处请大家指出:
1)要放如到对象池中的对象类
package com.test;

import java.util.*;

public class TestObject {
  private String ID;
  
  private String name;
  
  private boolean isUsed = false;
  
  private long firstDate = System.currentTimeMillis();
  
  public void handle() {// 业务操作 �����
  }
  
  public long getFirstDate() {
    return firstDate;
  }
  
  public void setFirstDate(long firstDate) {
    this.firstDate = firstDate;
  }
  
  public boolean isUsed() {
    return isUsed;
  }
  
  public void setUsed(boolean isUsed) {
    this.isUsed = isUsed;
  }
  
  public String getName() {
    return name;
  }
  
  public void setName(String name) {
    this.name = name;
  }
  
  public String getID() {
    return ID;
  }
  
  public void setID(String id) {
    ID = id;
  }
  
  public boolean close() {
   this.setFirstDate(System.currentTimeMillis());
    this.setUsed(false);
    return true;
  }
}
2)对象池类
package com.test;

import java.util.*;

;

public class Factory {
  private static Factory factory;
  
  private static int minimum = 4;// 对象池中最少有4个对象
  //暂时没有写对象池初始化的时候自动生成4个相关对象
  private static int maximum = 10;// 对象池中最多有10个对象
  
  private static Collection data = new Vector();
  static {
    TestObject testObject = new TestObject();
    /*
* for (int i = 0; i < minimum; i++) { testObject.setID("0" + new IntegerLight Bulb.toString());
* testObject.setName("01" + new IntegerLight Bulb.toString()); data.add(testObject); }
*/
    testObject.setID("01");
    testObject.setName("011");
    data.add(testObject);
    testObject = new TestObject();
    testObject.setID("02");
    testObject.setName("021");
    data.add(testObject);
    testObject = new TestObject();
    testObject.setID("03");
    testObject.setName("031");
    data.add(testObject);
    testObject = new TestObject();
    testObject.setID("04");
    testObject.setName("041");
    data.add(testObject);
    testObject = new TestObject();
    testObject.setID("05");
    testObject.setName("051");
    data.add(testObject);
    testObject = new TestObject();
    testObject.setID("06");
    testObject.setName("061");
    data.add(testObject);
    testObject = new TestObject();
    testObject.setID("07");
    testObject.setName("071");
    data.add(testObject);
  }
  
  private Factory() {
    
  }
  
  public static synchronized Factory creatFactoryPool() {
    if (factory == null)
      factory = new Factory();
    return factory;
  }
  
  public TestObject getObject(String id) {
    /*
* 通过ID来查找对象
*/
    Iterator iterator = getIterator();
    TestObject object = null;
    int i = 0;
    while (iterator.hasNext()) {
      object = (TestObject) iterator.next();
      if (id.equals(object.getID())) {
        i = 1;
        /*
* 当返回池中对象时,对象的下面2个属性值做相映的变化
*/
        object.setFirstDate(System.currentTimeMillis());
        object.setUsed(true);
        break;
      }
    }
    if (i == 1) {
      return object;
    } else {
      System.out.println("ID=" + id + "对象不存在!");
      return null;
    }
  }
  
  public TestObject getObject() {
    /*
* 获取池中对象,如池中没有多余对象可供选择,即返回一新建对象.
* 如池中对象数目没有达到最大值,就把此对象添加池中.
*/
    Iterator iterator = getIterator();
    TestObject object = null;
    int i = 0;
    while (iterator.hasNext()) {
      object = (TestObject) iterator.next();
      if (object.isUsed() == false) {
        i = 1;
        object.setFirstDate(System.currentTimeMillis());
        object.setUsed(true);
        break;
      }
    }
    if (i == 1) {// 得到要查找的对象
      return object;
    }
    if (data.size() < maximum) {
      System.out.println("返回新建对象,添加池中!");
      object = new TestObject();
      object.setUsed(true);
      data.add(object);
      return object;
    } else {
      System.out.println("返回新建对象!");
      return new TestObject();
    }
  }
  
  public boolean deleteObject(String id) {
    /*
* 根据对象ID来删除对象.成功返回true,失败返回false
*/
    Iterator iterator = getIterator();
    TestObject object = null;
    int i = 0;
    while (iterator.hasNext()) {
      if (object.getID().equals(id)) {
        iterator.remove();
        i = 1;
        break;
      }
    }
    if (i == 0) {
      System.out.println("ID=" + id + "被删除对象不存在!");
      return false;
    } else {
      System.out.println("删除对象成功!");
      return true;
    }
    
  }
  
  public boolean addObject(TestObject Object) {
    return data.add(Object);
    
  }
  
  public void objectManage() {
    /*
     * 池对象在获取对象时候,都会执行此方法.如果有对象超过30分钟没有使用,
     * 就把此对象isUsed属性值改成false
     */
    long interval = 30 * 60 * 1000;// 30分钟
    Iterator iterator = data.iterator();
    TestObject object = null;
    long currentTime = System.currentTimeMillis();
    while (iterator.hasNext()) {
      object = (TestObject) iterator.next();
      if (object.isUsed() == false)
        continue;
      if (currentTime - object.getFirstDate() >= interval) {
        object.setUsed(false);
      }
    }
  }
  
  public Collection getData() {
    return data;
  }
  
  public Iterator getIterator() {
    objectManage();
    return data.iterator();
  }
  
}
Factory类提供了获取对象的相关方法,程序还有不少要改进的地方,贴出来让大家提出改进的意见!
posted @ 2007-05-10 23:03 二胡 阅读(130) | 评论 (0)编辑 收藏

仅列出标题
共165页: First 上一页 146 147 148 149 150 151 152 153 154 下一页 Last