| | |
| | | * All Rights Saved! Chongqing AnYun Tech co. LTD |
| | | */ |
| | | public abstract class ObjectPool<T extends PooledObject> { |
| | | /** |
| | | * 对象容器 |
| | | */ |
| | | private T[] mContainer; |
| | | |
| | | private T[] mContainer;//对象容器 |
| | | /** |
| | | * 对象锁 |
| | | */ |
| | | private final Object LOCK = new Object(); |
| | | |
| | | private final Object LOCK = new Object();//对象锁 |
| | | |
| | | private int length;//每次返回对象都放到数据末端,length表示前面可用对象数 |
| | | /*** |
| | | * 每次返回对象都放到数据末端,length表示前面可用对象数 |
| | | */ |
| | | private int length; |
| | | |
| | | public ObjectPool(int capacity) { |
| | | mContainer = createObjPool(capacity); |
| | |
| | | } |
| | | return obj; |
| | | } |
| | | |
| | | /** |
| | | * 把对象放回池里面 |
| | | * @param obj |
| | | */ |
| | | public final void returnObj(T obj){ |
| | | synchronized (LOCK){ |
| | | int size = mContainer.length; |
| | | if (length < size){ |
| | | mContainer[length] = obj; |
| | | length++; |
| | | } |
| | | } |
| | | } |
| | | } |