編輯:關於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"); } } }
最近有人問我如何實現倒計時的按鈕功能,例如發送驗證碼,我記得有個CountDownTimer,因為好久沒用過了,自己就寫了一個,代碼如下 new CountDownTim
前言最近一段時間在研究EventBus和Retrofit 的過程中,都遇到了注解這個概念。由於在學習Java的時候對這方面沒有深入了解過,所以看起相關的代碼來,總會有點不
在項目中開發自定義控件時,或多或少都會用到 obtainStyledAttributes(AttributeSet, int[], int, int) 或者 obtain
1.Volley結構圖從上圖可以看到Volley分為三個線程,分別是主線程、緩存調度線程、和網絡調度線程,首先請求會加入緩存隊列,如果發現可以找到相應的緩存結果就直接讀取