/**
*
* VacantCell:代表空的cells,由多个cell组成,将其实现为一个cell池,减少对象的创建
*
*/
static final class VacantCell{
private static final int POOL_SIZE = 100; //池最多缓存100个VacantCell
private static final Object mLock = new Object(); //用作同步锁
private static VacantCell mRoot;
private static int count;
private VacantCell mNext;
//VacantCell的大小信息
private int cellX;
private int cellY;
private int cellHSpan;
private int cellVSpan;
public static VacantCell acquire(){
synchronized (mLock) {
if(mRoot == null){
return new VacantCell(); //一开始没有的时候,一直新创建再返回
}
//如果池存在,则从池中取
VacantCell info = mRoot;
mRoot = info.mNext;
count--; //记得将统计更新
return info;
}
}
//release这个对象自身
public void release(){
synchronized(mLock){
if(count < POOL_SIZE){
count++;
mNext = mRoot;
mRoot = this;
}
}
}
}
}
转载于:https://www.cnblogs.com/wzfyang/p/3878272.html