說明:英語(yǔ)中的Pool除了“池”之外,還有“供多方共享的資源”意思。作者十分懷疑第二種才是“Object Pool”中的Pool的實(shí)際含義,但是“對(duì)象池”的說法已經(jīng)廣為流傳,而一時(shí)又沒有足以替代的貼切譯法,因此這里仍然沿用這種譯名。 Jakarta Commons Pool組件
Jakarta Commons Pool是一個(gè)用于在java程序中實(shí)現(xiàn)對(duì)象池化的組件。它的基本情況是:
最簡(jiǎn)單的一個(gè)是StackObjectPool(),一切采用默認(rèn)的設(shè)置,也不指明要用的PoolableObjectFactory實(shí)例。 最復(fù)雜的一個(gè)則是StackObjectPool(PoolableObjectFactory factory, int max, int init)。其中:
最簡(jiǎn)單的是SoftReferenceObjectPool(),不預(yù)先在池內(nèi)創(chuàng)建對(duì)象,也不指明要用的PoolableObjectFactory實(shí)例。 最復(fù)雜的一個(gè)則是SoftReferenceObjectPool(PoolableObjectFactory factory, int initSize)。其中:
最簡(jiǎn)單的一個(gè)是GenericObjectPool(PoolableObjectFactory factory)。僅僅指明要用的PoolableObjectFactory實(shí)例,其它參數(shù)則采用默認(rèn)值。 最復(fù)雜的一個(gè)是GenericObjectPool(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle)。其中:
GenericObjectPool(PoolableObjectFactory factory, int maxActive) GenericObjectPool(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait) GenericObjectPool(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, boolean testOnBorrow, boolean testOnReturn) GenericObjectPool(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle) GenericObjectPool(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn) 這種對(duì)象池不可以在沒有Jakarta Commmons Collections組件支持的情況下運(yùn)行。
int maxActive int maxIdle long maxWait long minEvictableIdleTimeMillis int numTestsPerEvictionRun boolean testOnBorrow boolean testOnReturn boolean testWhileIdle long timeBetweenEvictionRunsMillis byte whenExhaustedAction 這些字段的作用,與在GenericKeyedObjectPool最復(fù)雜的構(gòu)造方法中與它們同名的參數(shù)完全相同。
Martin Fowler在 《Refactoring -- Improving the Design of Existing Code》(中譯本名為《重構(gòu)??改善既有代碼的設(shè)計(jì)》,由侯捷、熊節(jié)合譯)一書的第三章《代碼的壞味道(Bad Smells in Code)》中討論了被稱為“Long Parameter List”和“Data Class”的“壞味道”。并指明了一些可以用于對(duì)付這些問題的重構(gòu)手法。
Brian Goetz在IBM developerWorks上發(fā)表的《Java 理論與實(shí)踐:描繪線程安全性》一文中,說明了為什么單純地使用同步方法還不能讓對(duì)象就此在多線程環(huán)境里高枕無(wú)憂的原因。
Dr. Cliff Click發(fā)表在JavaOne 2003上的《Performance Myths Exposed》(session #1522),給出了一組包括“對(duì)象池化”在內(nèi)的、對(duì)“能提高Java程序性能”的做法的實(shí)際效果的測(cè)試數(shù)據(jù),和一些恰當(dāng)使用這些做法的建議。