import java.util.Enumeration;
import java.util.Vector;
public class ObjectPool {
private static final boolean PRINTABLE = true;
private Class<?> clazz = null;
private int numObjects = 10;
private int maxObjects = 50;
private Vector<PooledObject> objects = null;
public static void main(String[] args) throws Exception {
ObjectPool pool = new ObjectPool(XX.class);
pool.createPool();
XX xx = (XX) pool.getObject();
xx.reset();
xx.setAge(1);
xx.setSex("M");
pool.returnObject(xx);
pool.closeObjectPool();
}
public ObjectPool(Class<?> clazz) {
this.clazz = clazz;
}
public synchronized void createPool() throws Exception {
if (this.objects != null)
return;
this.objects = new Vector<PooledObject>();
for (int i = 0; i < this.numObjects; ++ i) {
createObjects();
}
}
public synchronized Object getObject() throws Exception {
if (this.objects == null)
return null;
Object conn = this.getFreeObject();
while (conn == null) {
wait(250);
conn = this.getFreeObject();
}
return conn;
}
private Object getFreeObject() throws Exception {
Object obj = this.findFreeObject();
if (obj == null) {
this.createObjects();
obj = this.findFreeObject();
if (obj == null) {
return null;
}
}
return obj;
}
private void createObjects() throws Exception {
if (this.objects.size() < this.maxObjects) {
this.message("created" + this.objects.size());
Object obj = this.clazz.newInstance();
this.objects.addElement(new PooledObject(this.objects.size(), obj));
}
}
private Object findFreeObject() {
this.message("before free:--------");
this.print();
Object obj = null;
PooledObject pooledObject = null;
Enumeration<PooledObject> enumerate = objects.elements();
while (enumerate.hasMoreElements()) {
pooledObject = (PooledObject) enumerate.nextElement();
if (!pooledObject.isBusy()) {
pooledObject.setBusy(true);
obj = pooledObject.getObject();
this.message("free object");
break;
}
}
this.message("after free:--------");
this.print();
return obj;
}
public void returnObject(Object obj) {
if (this.objects == null)
return;
this.message("before return:--------");
this.print();
PooledObject pooledObject = null;
Enumeration<PooledObject> enumerate = objects.elements();
while (enumerate.hasMoreElements()) {
pooledObject = (PooledObject) enumerate.nextElement();
if (obj == pooledObject.getObject()) {
pooledObject.setBusy(false);
this.message("return object");
break;
}
}
this.message("after return:--------");
this.print();
}
public synchronized void closeObjectPool() {
if (this.objects == null)
return;
PooledObject pooledObject = null;
Enumeration<PooledObject> enumerate = objects.elements();
while (enumerate.hasMoreElements()) {
pooledObject = (PooledObject) enumerate.nextElement();
this.message(pooledObject.toString());
if (pooledObject.isBusy()) {
this.message("wait 5000");
wait(5000);
}
}
this.objects.clear();
this.objects = null;
}
private void wait(int millionSeconds) {
try {
Thread.sleep(millionSeconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void print() {
Enumeration<PooledObject> enumerate = objects.elements();
while (enumerate.hasMoreElements()) {
PooledObject pooledObject = (PooledObject) enumerate.nextElement();
this.message(pooledObject.toString());
}
this.message("print end==============================");
}
public void message(String msg) {
if (PRINTABLE) {
System.out.println(msg);
}
}
class PooledObject {
private int index = 0;
Object objection = null;// 对象
boolean busy = false; // 此对象是否正在使用的标志,默认没有正在使用
// 构造函数,根据一个 Object 构告一个 PooledObject 对象
public PooledObject(int index, Object objection) {
this.index = index;
this.objection = objection;
this.busy = false;
}
// 返回此对象中的对象
public Object getObject() {
return objection;
}
// 设置此对象的,对象
public void setObject(Object objection) {
this.objection = objection;
}
// 获得对象对象是否忙
public boolean isBusy() {
return busy;
}
// 设置对象的对象正在忙
public void setBusy(boolean busy) {
this.busy = busy;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
@Override
public String toString() {
return String.format("[PooledObject] index:%d, busy:%b", this.index, this.busy);
}
}
}
class XX {
private int age = 0;
private String sex = "";
public XX() {
}
public void reset() {
this.age = 0;
this.sex = "";
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}