今天看别人代码,突然发现一个类pools,然后点进去看发现是在v4包下的一个工具类。官方文档对其介绍比较简单就一句话:
Helper class for crating pools of objects.
对象池存取的辅助类。OK,接下来我们就分析一下源码然后再来研究一下它的使用。
由于源码不多,所以直接贴出来便于分析:
public final class Pools {
/**
* Interface for managing a pool of objects.
*
* @param The pooled type.
*/
public static interface Pool {
/**
* @return An instance from the pool if such, null otherwise.
*/
public T acquire();
/**
* Release an instance to the pool.
*
* @param instance The instance to release.
* @return Whether the instance was put in the pool.
*
* @throws IllegalStateException If the instance is already in the pool.
*/
public boolean release(T instance);
}
private Pools() {
/* do nothing - hiding constructor */
}
/**
* Simple (non-synchronized) pool of objects.
*
* @param The pooled type.
*/
public static class SimplePool implements Pool {
private final Object[] mPool;
private int mPoolSize;
/**
* Creates a new instance.
*
* @param maxPoolSize The max pool size.
*
* @throws IllegalArgumentException If the max pool size is less than zero.
*/
public SimplePool(int maxPoolSize) {
if (maxPoolSize <= 0) {
throw new IllegalArgumentException("The max pool size must be > 0");
}
mPool = new Object[maxPoolSize];
}
@Override
@SuppressWarnings("unchecked")
public T acquire() {
if (mPoolSize > 0) {
final int lastPooledIndex = mPoolSize - 1;
T instance = (T) mPool[lastPooledIndex];
mPool[lastPooledIndex] = null;
mPoolSize--;
return instance;
}
return null;
}
@Override
public boolean release(T instance) {
if (isInPool(instance)) {
throw new IllegalStateException("Already in the pool!");
}
if (mPoolSize < mPool.length) {
mPool[mPoolSize] = instance;
mPoolSize++;
return true;
}
return false;
}
private boolean isInPool(T instance) {
for (int i = 0; i < mPoolSize; i++) {
if (mPool[i] == instance) {
return true;
}
}
return false;
}
}
/**
* Synchronized) pool of objects.
*
* @param The pooled type.
*/
public static class SynchronizedPool extends SimplePool {
private final Object mLock = new Object();
/**
* Creates a new instance.
*
* @param maxPoolSize The max pool size.
*
* @throws IllegalArgumentException If the max pool size is less than zero.
*/
public SynchronizedPool(int maxPoolSize) {
super(maxPoolSize);
}
@Override
public T acquire() {
synchronized (mLock) {
return super.acquire();
}
}
@Override
public boolean release(T element) {
synchronized (mLock) {
return super.release(element);
}
}
}
}
从上面的源码中可以看出:这个工具类中包含一个私有的构造方法、一个接口和两个类,下面详细解析一下:
1、Pools类中仅且仅有一个方法,而且该方法为一个私有的构造方法:于是Pools类不能够直接被使用,只能使用其子类(可以预测其一定而且必须有子类)。
2、包含一个接口Pool
T acquire():获取一个T对象。
boolean release(T element):向对象池中添加一个对象,如果该对象已存在于对象池中,则抛出异常IllegalStateException。返回true表示添加成功,否则失败。
3、包含两个类:SimplePool
SimplePool
SynchronizedPool
OK,分析就分析到这里,接下来我们来看其使用。
我们约定接下来的几个例子当中对象池中存储的对象DataSet定义如下:
import java.util.HashMap;
/**
* @author lizhenya
* @description 数据集合,支持传递复杂数据
* @since 12/11/2015
*/
public class DataSet {
private HashMap
1、使用SimplePool来维护一个对象池
/**
* @author:lizhenya
* @Time: 16/10/24
* @email: [email protected]
*/
public class DataPool extends Pools.SimplePool {
/**
* Creates a new instance.
*
* @param maxPoolSize The max pool size.
* @throws IllegalArgumentException If the max pool size is less than zero.
*/
public DataPool(int maxPoolSize) {
super(maxPoolSize);
}
/**
* 方法描述:获取DataSet对象
*
* @return
*/
public DataSet get() {
return acquire() != null ? acquire() : new com.lzy.poolsdemo.DataSet();
}
/**
* 方法描述:判断对象池中是否包含dataSet对象实例
*
* @param dataSet DataSet对象
* @return 如果包含含dataSet对象实例返回true, 如果不包含则将dataset对象实例添加到对象池并返回false
*/
public boolean obtain(DataSet dataSet) {
return release(dataSet);
}
}
在SimplePools中对象的维护使用的是数组,数组有个最大的缺点:存储满了后不能自增长。所以既要保证效率又能自增长可以采用ArrayList来维护该对象池,下面我们自定义一个ListPool:
/**
* @author:lizhenya
* @Time: 16/10/24
* @email: [email protected]
*/
public class ListPool implements Pools.Pool {
/**
* 同步ArrayList
**/
private final List ts;
public ListPool(int maxPoolSize) {
ts = Collections.synchronizedList(new ArrayList(maxPoolSize));
}
@Override
public T acquire() {
if (ts.isEmpty())
return null;
return ts.remove(0);
}
@Override
public boolean release(T instance) {
return ts.add(instance);
}
}
使用如下:
/**
* @author:lizhenya
* @Time: 16/10/24
* @email: [email protected]
*/
public class ListDataPools extends ListPool {
public ListDataPools(int maxPoolSize) {
super(maxPoolSize);
}
public DataSet get() {
return acquire() == null ? new DataSet() : acquire();
}
public boolean obtain(DataSet dataSet) {
dataSet.recycle();
return release(dataSet);
}
}
源码下载:android.support.v4.utils.Pools使用Demo