apache commons pool,官方稱之爲 Generic object pooling component,即通用的對象池組件。
The Apache Commons Pool open source software library provides an object-pooling API and a number of object pool implementations. 既然稱爲Generic通用的,那麼它的源碼抽象層次應該比較高,推測主要爲方法的定義、模式的設計等,具體實現較少。源碼應該較爲簡單,看懂不是什麼難事。
commons pool2的源碼在這裏:
https://commons.apache.org/proper/commons-pool/download_pool.cgi
javadoc在這裏:
https://commons.apache.org/proper/commons-pool/api-2.4.2/index.html
The org.apache.commons.pool2 package defines a handful of pooling interfaces and some base classes that may be useful when creating new pool implementations. 該包定義了一系列對象池接口,還有一些在創建具體對象池時會用到的抽象基類。
commons pool2 一共有三個最基本的接口:
1. ObjectPool<T>
2. PooledObjectFactory<T>
3. PooledObject<T> extends Comparable<PooledObject<T>>
ObjectPool代表一個對象池,通常在一個具體的對象池實現中,將此接口暴露給調用者。對象池對其持有的對象的生命週期負責,提供的方法有創建對象、獲取對象、歸還對象、銷燬對象等。
PooledObjectFactory定義了一系列有關對象的生命週期的方法,供ObjectPool調用。PooledObjectFactory應爲線程安全的。
PooledObject 是一個包裹類,用來包裹對象池中的對象,目的是跟蹤池中對象的一些附加信息,比如對象狀態。需要線程安全。
Defines the wrapper that is used to track the additional information, such as state, for the pooled objects.
Implementations of this class are required to be thread-safe.
上面三個超類的三角關係應該很明確了,ObjectPool持有PooledObjectFactory,對外暴露使用接口,PooledObjectFactory管理PooledObject的生命週期,PooledObject包裹池中對象。