Javaオブジェクトプールの例
15609 ワード
オブジェクトプールの基本的な考え方は、使用したオブジェクトを保存し、次回このオブジェクトが必要になったら、繰り返し使用することで、頻繁にオブジェクトを作成するコストをある程度削減することです.すべてのオブジェクトがプール化に適しているわけではありません.オブジェクトプールのメンテナンスにもコストがかかります.生成時のオーバーヘッドが少ないオブジェクトをプール化すると、逆に「オブジェクトプールのメンテナンスオーバーヘッド」が「新しいオブジェクトを生成するオーバーヘッド」よりも大きくなり、パフォーマンスが低下する可能性があります.しかし、生成時にオーバーヘッドが大きいオブジェクトでは、プール化技術がパフォーマンスを向上させる有効な戦略です.次に、オブジェクトプールを構築する例を示します.
public class ObjectPool {
private int numObjects = 10; //
private int maxObjects = 50; //
private Vector objects = null; // ( PooledObject )
public ObjectPool() {
}
/*** ***/
public synchronized void createPool(){
// 。 , objects
if (objects != null) {
return; // ,
}
// , 0
objects = new Vector();
// numObjects ,
for (int x = 0; x < numObjects; x++) {
if ((objects.size() == 0)&&this.objects.size() <this.maxObjects) {
Object obj = new Obj();
objects.addElement(new PooledObject(obj));
}
}
}
public synchronized Object getObject(){
//
if (objects == null) {
return null; // , null
}
Object conn = getFreeObject(); //
// ,
while (conn == null) {
wait(250);
conn = getFreeObject(); // , ,
// getFreeObject() null,
}
return conn;//
}
/**
* objects ,
* , , 。
* , , null
*/
private Object getFreeObject(){
//
Object obj = findFreeObject();
if (obj == null) {
createObjects(incrementalObjects); // ,
//
obj = findFreeObject();
// , null
if (obj == null) {
return null;
}
}
return obj;
}
/**
* , ,
* , null
*/
private Object findFreeObject(){
Object obj = null;
PooledObject pObj = null;
//
Enumeration enumerate = objects.elements();
// ,
while (enumerate.hasMoreElements()) {
pObj = (PooledObject) enumerate.nextElement();
// ,
if (!pObj.isBusy()) {
obj = pObj.getObject();
pObj.setBusy(true);
}
return obj;//
}
/**
* , 。
* 。
*/
public void returnObject(Object obj) {
// , ( ),
if (objects == null) {
return;
}
PooledObject pObj = null;
Enumeration enumerate = objects.elements();
// ,
while (enumerate.hasMoreElements()) {
pObj = (PooledObject) enumerate.nextElement();
//
if (obj == pObj.getObject()) {
// ,
pObj.setBusy(false);
break;
}
}
}
/**
* , 。
*/
public synchronized void closeObjectPool() {
// , ,
if (objects == null) {
return;
}
PooledObject pObj = null;
Enumeration enumerate = objects.elements();
while (enumerate.hasMoreElements()) {
pObj = (PooledObject) enumerate.nextElement();
// , 5
if (pObj.isBusy()) {
wait(5000); // 5
}
//
objects.removeElement(pObj);
}
//
objects = null;
}
/**
*
*/
private void wait(int mSeconds) {
try {
Thread.sleep(mSeconds);
}
catch (InterruptedException e) {
}
}
/**
* 。
* , , 。
*/
class PooledObject {
Object objection = null;//
boolean busy = false; // ,
// , Object PooledObject
public PooledObject(Object objection) {
this.objection = objection;
}
//
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 class ObjectPoolTest {
public static void main(String[] args) throws Exception {
ObjectPool objPool = new ObjectPool();
objPool.createPool();
Object obj = objPool.getObject();
returnObject(obj);
objPool.closeObjectPool();
}
}