Apache的Common-pool中的Object pool的疑問!多謝!
我剛才下載了Common-pool的source code做了一個測試,共有三個程式,
程式碼如下:
//SampleObject.java
package test;
public class SampleObject {
public SampleObject() {
}
public void dosomething(){
//do nothing actually
}
}
//ObjectFactory.java
package test;
import org.apache.commons.pool.BasePoolableObjectFactory;
public class ObjectFactory extends BasePoolableObjectFactory{
public ObjectFactory() {
}
public Object makeObject() {
return new SampleObject();
}
// when an object is returned to the pool,
// we'll clear it out
public void passivateObject(Object obj) {
}
}
//TestObject.java
package test;
import org.apache.commons.pool.*;
import org.apache.commons.pool.impl.*;
import java.io.Reader;
import java.io.IOException;
import java.sql.*;
public class TestObject {
private ObjectPool pool;
public TestObject() {
}
public static void main(String[] args) throws Exception{
TestObject test = new TestObject();
int count = 0;
test.pool = new StackObjectPool(new ObjectFactory());
System.out.println("-------------------------------------------------------");
SampleObject so= null;
long bbefore = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
so = (SampleObject)test.pool.borrowObject();
so.dosomething();
}
long bend = System.currentTimeMillis();
System.out.println("time1="+(bend-bbefore));
System.out.println("-------------------------------------------------------");
SampleObject so1= null;
bbefore = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
so1 = new SampleObject();
so1.dosomething();
}
bend = System.currentTimeMillis();
System.out.println("time2="+(bend-bbefore));
System.out.println("-------------------------------------------------------");
}
}
執行結果如下:
-------------------------------------------------------
time1=9964
-------------------------------------------------------
time2=50
-------------------------------------------------------
沒有用ObjectPool比用了ObjectPool還要快。。。為什麼呢?多謝
程式碼如下:
//SampleObject.java
package test;
public class SampleObject {
public SampleObject() {
}
public void dosomething(){
//do nothing actually
}
}
//ObjectFactory.java
package test;
import org.apache.commons.pool.BasePoolableObjectFactory;
public class ObjectFactory extends BasePoolableObjectFactory{
public ObjectFactory() {
}
public Object makeObject() {
return new SampleObject();
}
// when an object is returned to the pool,
// we'll clear it out
public void passivateObject(Object obj) {
}
}
//TestObject.java
package test;
import org.apache.commons.pool.*;
import org.apache.commons.pool.impl.*;
import java.io.Reader;
import java.io.IOException;
import java.sql.*;
public class TestObject {
private ObjectPool pool;
public TestObject() {
}
public static void main(String[] args) throws Exception{
TestObject test = new TestObject();
int count = 0;
test.pool = new StackObjectPool(new ObjectFactory());
System.out.println("-------------------------------------------------------");
SampleObject so= null;
long bbefore = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
so = (SampleObject)test.pool.borrowObject();
so.dosomething();
}
long bend = System.currentTimeMillis();
System.out.println("time1="+(bend-bbefore));
System.out.println("-------------------------------------------------------");
SampleObject so1= null;
bbefore = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
so1 = new SampleObject();
so1.dosomething();
}
bend = System.currentTimeMillis();
System.out.println("time2="+(bend-bbefore));
System.out.println("-------------------------------------------------------");
}
}
執行結果如下:
-------------------------------------------------------
time1=9964
-------------------------------------------------------
time2=50
-------------------------------------------------------
沒有用ObjectPool比用了ObjectPool還要快。。。為什麼呢?多謝
相關文章
- 關於servlet多執行緒序的疑問,請各位仁兄作答,謝謝。Servlet執行緒
- object-c中疑問Object
- disruptor中多執行緒消費者的疑問執行緒
- Hibernate處理多個連續事務的疑問。詳情請見,請高人多多賜教,謝謝!
- activemq的問題,謝謝MQ
- jive配置中的database問題?!請幫忙阿謝謝!Database
- jboss 下 部署 多個應用程式的資源問題,急,謝謝!!
- 我的疑問
- HashMap中的hash演算法中的幾個疑問HashMap演算法
- RESTful設計中的常見疑問REST
- 在學習中的一點疑問。
- 關於多執行緒協作的疑問執行緒
- 多執行緒程式中對資料庫連線使用的疑問執行緒資料庫
- java——ArrayList中contains()方法中的疑問JavaAI
- 關於SimpleJdonFrameworkTest的問題,謝謝!Framework
- Ibatis的疑問BAT
- 快取的疑問快取
- 橋模式的疑問模式
- Macaca使用過程中的疑問Mac
- 關於petstore的執行問題,謝謝~
- 有關工作流程的概念,用法的問題!謝謝
- Struts +Spring + Hibernate 的配置問題,謝謝!Spring
- 請幫我看看呼叫webservice的問題謝謝!Web
- QI4J中關於entity的疑問
- SQL在shared pool中的解析過程問題SQL
- 疑問:使用了Jdon6.4的JPetStore的模型中的場景模型
- 關於jsp拒絕訪問的問題,謝謝大家JS
- JPetStore架構的疑問架構
- 工廠模式的疑問模式
- Composite模式的疑問模式
- Jive原始碼的疑問原始碼
- 初學者的小疑問
- 最近看JdonFramework的疑問Framework
- 初學JAVA的疑問。。Java
- 關於mysql和jsp的中文問題~謝謝MySqlJS
- DDD中Factory和Repository的一些疑問
- 對ERP系統中管理思想的疑問薦
- 關於JDONJIVE中DbFilterManager中程式碼的疑問Filter