• 仿照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 = value;
    					return item;
    				}
    			}
    			return new ObjectItem(value);
    		}
    		
    		public void recycle() {
    			synchronized (sPoolLock) {
    				if (sPoolSize < MAX_CACHE) {
    					mValue = 0;
    					this.mNext = sPool;
    					sPool = this;
    					
    					sPoolSize++;
    				}
    			}
    		}
    		
    		public ObjectItem() {
    			
    		}
    		
    		public ObjectItem(int value) {
    			mValue = value;
    		}
    		
    		@Override
    		public String toString() {
    			return String.valueOf(mValue);
    		}
    	}
    	
    	public static final void main(String[] args) {
    		ObjectItem item1 = ObjectItem.obtain(1);
    		item1.recycle();
    		ObjectItem item2 = ObjectItem.obtain(3);
    		if (item1 == item2) {
    			System.out.println("YES, USE THE SAME OBJECT");
    		} else {
    			System.out.println("ERROR");
    		}
    	}
    }

  • 相关阅读:
    操作系统__kali(1)基本操作指令,以及常用工具
    Log4net入门(回滚日志文件篇)
    Log4net入门(日志文件篇)
    Log4net入门(控制台篇)
    openwrt控制GPIO
    openwrt DTS介绍
    openwrt bin文件解析
    STM32(三十)蓝牙通信
    openwrt的led configuration
    uci文件生成脚本函数说明
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4344159.html
Copyright © 2020-2023  润新知