对象池common-pool2源码分析
2016-03-28 00:00
756 查看
摘要: common-pool2源码分析
Apache common-pool2提供了一个通用的对象池技术的实现。
common-pool2主要围绕三个接口来实现,ObjectPool、PooledObject、PooledObjectFactory。由PooledObjectFactory创建的对象,经PooledObject包装后放入ObjectPool。
官方例子:http://commons.apache.org/proper/commons-pool/examples.html
先从GenericObjectPool开始分析
成员变量:
重要的成员变量为:allObjects和idleObjects.
allObjects:对象池中所有的对象.
idleObjects:空闲对象.
其中数据结构LinkedBlockingDeque:
结点的数据结构
从之前的分析中可以看出GenericObjectPool中allObjects和idleObjects存放均是池对象即经过包装的对象.
Apache common-pool2提供了一个通用的对象池技术的实现。
common-pool2主要围绕三个接口来实现,ObjectPool、PooledObject、PooledObjectFactory。由PooledObjectFactory创建的对象,经PooledObject包装后放入ObjectPool。
1.ObjectPool对象池
ObjectPool:对象池,负责存放管理对象.官方例子:http://commons.apache.org/proper/commons-pool/examples.html
ReaderUtil readerUtil = new ReaderUtil(new GenericObjectPool<StringBuffer>(new StringBufferFactory()));
先从GenericObjectPool开始分析
成员变量:
重要的成员变量为:allObjects和idleObjects.
/* * All of the objects currently associated with this pool in any state. It * excludes objects that have been destroyed. The size of * {@link #allObjects} will always be less than or equal to {@link * #_maxActive}. Map keys are pooled objects, values are the PooledObject * wrappers used internally by the pool. */ private final Map<T, PooledObject<T>> allObjects = new ConcurrentHashMap<T, PooledObject<T>>();
private final LinkedBlockingDeque<PooledObject<T>> idleObjects;
allObjects:对象池中所有的对象.
idleObjects:空闲对象.
其中数据结构LinkedBlockingDeque:
结点的数据结构
/** Doubly-linked list node class */ private static final class Node<E> { /** * The item, or null if this node has been removed. */ E item; /** * One of: * - the real predecessor Node * - this Node, meaning the predecessor is tail * - null, meaning there is no predecessor */ Node<E> prev; /** * One of: * - the real successor Node * - this Node, meaning the successor is head * - null, meaning there is no successor */ Node<E> next; /** * Create a new list node. * * @param x The list item * @param p Previous item * @param n Next item */ Node(E x, Node<E> p, Node<E> n) { item = x; prev = p; next = n; } }
2.PooledObject池对象
PooledObject:池对象,将需要放入对象池的对象包装添加一些附加信息.从之前的分析中可以看出GenericObjectPool中allObjects和idleObjects存放均是池对象即经过包装的对象.
private final Map<T, PooledObject<T>> allObjects = new ConcurrentHashMap<T, PooledObject<T>>(); private final LinkedBlockingDeque<PooledObject<T>> idleObjects;
3.PooledObjectFactory池对象工厂
PooledObjectFactory:池对象工厂,负责池对象的创建,销毁等.相关文章推荐
- 从源码安装Mysql/Percona 5.5
- 浅析Ruby的源代码布局及其编程风格
- asp.net 抓取网页源码三种实现方法
- JS小游戏之仙剑翻牌源码详解
- JS小游戏之宇宙战机源码详解
- jQuery源码分析之jQuery中的循环技巧详解
- 本人自用的global.js库源码分享
- java中原码、反码与补码的问题分析
- ASP.NET使用HttpWebRequest读取远程网页源代码
- PHP网页游戏学习之Xnova(ogame)源码解读(六)
- C#获取网页HTML源码实例
- PHP网页游戏学习之Xnova(ogame)源码解读(八)
- PHP网页游戏学习之Xnova(ogame)源码解读(四)
- JS小游戏之极速快跑源码详解
- JS小游戏之象棋暗棋源码详解
- android源码探索之定制android关机界面的方法
- 基于Android设计模式之--SDK源码之策略模式的详解
- Android游戏源码分享之2048
- C语言借助EasyX实现的生命游戏源码
- C实现的非阻塞方式命令行端口扫描器源码