package wdzpool;
import java.util.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: netsky</p>
* @author wdz( hotmail =wdz123@hotmail.com)
* @version 1.0
*/
public final class WdzObjectPool implements WdzPoolIntf {
private WdzFactory factory1=new WdzFactory1();
private WdzFactory factory2=new WdzFactory2();
/**
* 维护可以被借出的对象列表
* **/
private ArrayList obSet = new ArrayList();
/**
* 维护已经被借出的对象列表,通过这个列表,可以进行对已经被借出的对象一些管理
* **/
private ArrayList borrowedSet = new ArrayList();
/***
* 维护每种可以被借出的 对象的最大借出数量
* */
private int MAX_OBJECT_COUNT = 2;
/***
* 初始化pool,预加载一些对象到pool
* **/
private void initClass(String sClassName) {
HashSet values = new HashSet();
for (int i = 0;
i < MAX_OBJECT_COUNT;
i++) {
values.add(createAInstance(sClassName));
}
obSet.add(values);
borrowedSet.add(new HashSet());
}
/***
* 初始化pool,预加载一些对象到pool
* **/
public void init() {
initClass("WdzObject1");
initClass("WdzObject2");
}
public WdzObjectPool() {
init();
}
/**
* 创建指定类型的对象
* @param sClassName 对象的类型
* ***/
private WdzObject createAInstance(String sClassName) {
if (sClassName.equals("WdzObject1")) {
return factory1.createInstance();
}
else
if (sClassName.equals("WdzObject2")) {
return factory2.createInstance();
}
else
{
return null;
}
}
/**
* 创建指定类型的对象,并且把创建的对象放入到pool中
* @param sClassName 对象的类型
* ***/
private void createAInstanceForClass(String sClassName) {
HashSet set;
if (sClassName.equals("WdzObject1")) {
set = (HashSet) obSet.get(0);
}
else
{
set = (HashSet) obSet.get(1);
}
set.add(createAInstance(sClassName));
}
/**
* 这里可以初始化状态
* ***/
public void beforeBrrow(WdzObject ob) {
}
/***
* 恢复到默认状态
* */
public void afterReturn(WdzObject ob) {
ob.resetStatus();
}
/**
* 必须定义成为同步方法,这样才支持多thread
* **/
public synchronized WdzObject brrowAInstance(String sClassName) {
int i;
if (sClassName.equals("WdzObject1")) {
i = 0;
}
else
{
i = 1;
}
HashSet set = (HashSet) obSet.get(i);
HashSet set2 = (HashSet) borrowedSet.get(i);
if (!set.iterator().hasNext()) {
if (set2.size() >= MAX_OBJECT_COUNT) {
try {
System.out.println(" notice ,没有的对象可以使用,进入等待状态");
this.wait();
}
catch (Exception ex) {
}
}
else
{
createAInstanceForClass(sClassName);
}
}
WdzObject ob = (WdzObject) set.iterator().next();
set.remove(ob);
set2.add(ob);
beforeBrrow(ob);
return ob;
}
/**
* 必须定义成为同步方法,这样才支持多thread
* **/
public synchronized void returnAInstance(WdzObject ob) {
int i;
if (ob instanceof WdzObject1) {
i = 0;
}
else
{
i = 1;
}
HashSet set = (HashSet) obSet.get(i);
HashSet set2 = (HashSet) borrowedSet.get(i);
afterReturn(ob);
set.add(ob);
set2.remove(ob);
try {
this.notifyAll();
System.out.println(" notice ,有新的对象可以使用了");
}
catch (Exception ex) {
}
System.out.println("ok return," + ob);
}
public static void main(String[] args) {
WdzObjectPool pool = new WdzObjectPool();
WdzObject ob1 = pool.brrowAInstance("WdzObject1");
WdzObject ob2 = pool.brrowAInstance("WdzObject2");
WdzObject ob11 = pool.brrowAInstance("WdzObject1");
ob1.execute();
WdzObject ob111 = pool.brrowAInstance("WdzObject1");
pool.returnAInstance(ob1);
}
}