public class ObjectPoolDemo {
static PoolableObjectFactory factory = new PoolableObjectFactoryDemo()
static ObjectPool pool = new GenericObjectPool(factory)
private static AtomicInteger endcount = new AtomicInteger()
public static class PoolThread extends Thread{
public void run(){
Object obj = null;
try {
for (int i = ; i < ; i++) {
Systemoutprintln(== + i + ==)
obj = poolborrowObject() //从池中得到对象
Systemoutprintln(obj+ is get) //模拟使用对象
poolreturnObject(obj) //使用完成后将对象返回池中
}
} catch (Exception e) {
eprintStackTrace()
} finally {
endcountgetAndIncrement()
}
}
}
public static void main(String[] args) {
new PoolThread()start()
new PoolThread()start()
new PoolThread()start()
try{
while(true){
if(endcountget()==){ //等待个线程全部结束
poolclose()
break;
}
}
}catch(Exception e){
}
}
}
返回目录Java程序性能优化让你的Java程序更快更稳定
编辑推荐
Java程序设计培训视频教程
JEE高级框架实战培训视频教程
JME移动开发实战教学视频
Visual C++音频/视频技术开发与实战
Oracle索引技术
ORACLEG数据库开发优化指南
[] []