首页 > 代码库 > 仿照Android的池化技术

仿照Android的池化技术

/**
 * 仿照Android池化技术
 * @author fgtian
 *
 */
public class ObjectCacheTest {
	public static class ObjectItem {
		private static int sPoolSize = 0;
		private static final int MAX_CACHE = 10;
		private static final Object sPoolLock = new Object();
		private static ObjectItem sPool = null;
		
		private ObjectItem mNext = null;
		private int mValue;
		
		public static ObjectItem obtain() {
			synchronized (sPoolLock) {
				if (null != sPool) {
					ObjectItem item = sPool;
					sPool = item.mNext;
					item.mNext = null;
					--sPoolSize;
					return item;
				}
			}
			return new ObjectItem();
		}
		
		public static ObjectItem obtain(int value) {
			synchronized (sPoolLock) {
				if (null != sPool) {
					ObjectItem item = sPool;
					sPool = item.mNext;
					item.mNext = null;
					--sPoolSize;
					item.mValue = http://www.mamicode.com/value;>