android.support.v4.util.Pool 類解析
阿新 • • 發佈:2019-01-27
今天閒著沒事看看 android v4包,結果發現一個類Pools.java,看了一下,從字面上的意思來看應該也是池,我自然而然的就會聯想到執行緒池,看了一下,Pools類其實是物件池
裡面維護了一個數組,廢話不多說,看程式碼:
public final class Pools { /** * Interface for managing a pool of objects. * * @param <T> The pooled type. */ public static interface Pool<T> { /** * @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 <T> The pooled type. */ public static class SimplePool<T> implements Pool<T> { 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 <T> The pooled type. */ public static class SynchronizedPool<T> extends SimplePool<T> { 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); } } } }
這個類首先定義了一個Pool介面,其中acquare 方法返回T型別物件,release方法,返回true/false
然後還定義了兩個內部類SimplePool,SynchronizedPool
先來說SynchronizedPool:
這個類繼承了SimplePool,只是對存取過程加鎖,可以這樣使用
SimplePool 實現了Pool介面public class MyPooledClass { private static final SynchronizedPool<MyPooledClass> sPool = new SynchronizedPool<MyPooledClass>(10); public static MyPooledClass obtain() { MyPooledClass instance = sPool.acquire(); return (instance != null) ? instance : new MyPooledClass(); } public void recycle() { // Clear state if needed. sPool.release(this); }
首先獲取陣列大小,然後取出一個物件mPoolSize-1
public T acquire() { if (mPoolSize > 0) { final int lastPooledIndex = mPoolSize - 1; T instance = (T) mPool[lastPooledIndex]; mPool[lastPooledIndex] = null; mPoolSize--; return instance; } return null; }
釋放的時候再把陣列的那個元素賦值,mPoolSize+1
@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;
}