Java object pooling and management of own
Advertisements
No dependencies, JDK 1.5 or later can be used directly, huh, huh
/**
* @ObjectPool.java
* @author Zhang Lei
* @since 2010-3-31 Afternoon 01:41:35
*/
package cn.zgl.utils.pool;
import java.util.ArrayList;
import java.util.concurrent.Semaphore;
/**
* @author Zhang Lei
* @version 2010.03 Object pool management class
*/
public abstract class ObjectPool<T> {
private ArrayList<T> pool = null;
private int size=0;
private int step=0;
private int currentPoolSize=0;
private Semaphore pass = null;
/**
* Initializes a maxSize ObjectPool
* @param maxSize
*/
public ObjectPool(int maxSize){
// Initialize resource pool
this.size =maxSize;
pool = new ArrayList<T>();
for(int i=0; i<maxSize; i++){
pool.add(createT());
}
currentPoolSize=maxSize;
pass = new Semaphore(size);
}
/**
* Up to initialize a maxSize ObjectPool , The default initialization initsize than , After taking the step increment
* @param maxSize The maximum number
* @param initsize Initializes the number
* @param step Increment the number
*/
public ObjectPool(int maxSize,int initsize,int step){
// Initialize resource pool
this.size =maxSize;
this.step=step;
pool = new ArrayList<T>();
for(int i=0; i<maxSize; i++){
pool.add(createT());
}
currentPoolSize=maxSize;
pass = new Semaphore(size);
}
/**
* Read one from the pool
* @author Zhang Lei
* @since 2010-3-31 Afternoon 04:21:19
* @return T
* @return
* @throws InterruptedException
*/
public T get() throws InterruptedException{
// Gets a pass, only get passes to get resource
pass.acquire();
return getSourceFromPool();
}
/**
* Return a to pool
* @author Zhang Lei
* @since 2010-3-31 Afternoon 04:21:30
* @return void
* @param resource
*/
public void put(T resource){
// The return of the pass, and the return of resources
pass.release();
releaseSourceToPool(resource);
}
/**
* All releases of the pool
* @author Zhang Lei
* @since 2010-3-31 Afternoon 04:21:41
* @return void
*/
public void relasePool(){
for(T t:pool){
destroyT(t);
}
}
/**
* Read a private
* @author Zhang Lei
* @since 2010-3-31 Afternoon 04:22:17
* @return T
* @return
*/
private synchronized T getSourceFromPool() {
if(pool.size()==0){
if(currentPoolSize<size){
int newT= size-currentPoolSize>step ? step: size-currentPoolSize;
for(int i=0; i<newT; i++){
pool.add(createT());
}
currentPoolSize+=newT;
}
}
return pool.remove(0);
}
/**
* Release one to Private pool
* @author Zhang Lei
* @since 2010-3-31 Afternoon 04:22:34
* @return void
* @param resource
*/
private synchronized void releaseSourceToPool(T resource) {
// System.out.println("return "+resource);
pool.add(resource);
}
// Destroy the object was created.
abstract T createT();
abstract void destroyT(T t);
// Sample :public class IntegerPool extends ObjectPool<Integer>
}
Here is a simple example, Oh
/**
* @IntegerPoolExample.java
* @author Zhang Lei
* @since 2010-3-31 Afternoon 03:18:05
*/
package cn.zgl.utils.pool;
/**
* @author Zhang Lei
*/
public class IntegerPoolExample extends ObjectPool<Integer>{
/**
* @param maxSize
*/
public IntegerPoolExample(int maxSize) {
super(maxSize);
// TODO Auto-generated constructor stub
}
public IntegerPoolExample() {
super(10);
}
/* (non-Javadoc)
* @see cn.zgl.utils.pool.ObjectPool#createT()
*/
@Override
Integer createT() {
return 0;
}
/* (non-Javadoc)
* @see cn.zgl.utils.pool.ObjectPool#destroyT(java.lang.Object)
*/
@Override
void destroyT(Integer t) {
t=null;
}
}
Of course, generally do not need to use Integer to manage the pool, and this is just an example, huh, huh.
Use the sample
public static void main(String[] args){
ObjectPool<Integer> pool= new IntegerPoolExample();
try{
for(int i=0;i<10;i++){
// Drawn from the pool a
Integer tmp=pool.get();
System.out.println(" The first "+ I +" time output " +tmp);
// Modify the value of this
tmp=i;
// The modified value access to this place is just a test test , In fact the pool object General requirements before using no change after use.
pool.put(tmp);
}
for(int i=0;i<10;i++){
System.out.println(" The first "+ I +" time output " +pool.get());
}
}catch(InterruptedException e){
e.printStackTrace();
}
finally{
pool.relasePool();
}
}
So, stop here. Oh, you lot of pointing, personal information Please do not reproduced
Related Posts of Java object pooling and management of own
-
hibernate generic generic DAO
package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *
-
Servlet brief introduction
Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...
-
First Hibernate Example
Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co
-
Struts2 + hibernate + spring problem user log in
dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q
-
Hibernate secondary cache
Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...
-
Hibernate's lazy strategy
hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...












