android 对象池

android.support.v4.util 包下,有个类 Pools.java ,它是用来干什么的呢?我们看下这个类的功能描述:

Helper class for creating pools of objects.

超级简单的一个介绍,就是一个创建对象池的帮助类。

接下来我们去看下这个类的具体实现代码:

public final class Pools {

    // 管理对象池的接口
    public static interface Pool {

        // 如果对象池中有这样的实例就返回这样的实例否则返回null
        public T acquire();

        // 释放一个实例到对象池
        public boolean release(T instance);
    }

    private Pools() {
        /* 私有化构造函数 */
    }

    /**
     * 简单的对象池
     *
     * @param  对象池里保存的对象类型
     */
    public static class SimplePool implements Pool {
        private final Object[] mPool;

        private int mPoolSize;

        /**
         * 创建一个新实例
         *
         * @param maxPoolSize 对象池的最大容量
         *
         * @throws IllegalArgumentException 如果对象池的容量小于0
         */
        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;
        }
    }

    /**
     * 同步的对象池
     *
     * @param  对象池的类型
     */
    public static class SynchronizedPool extends SimplePool {
        private final Object mLock = new Object();

        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);
            }
        }
    }
}

其实从代码的实现来看是非常简单的,不过知道这个工具包的帮助类,可以为我们以后使用对象池提供更加方便的实现。接下来,我们看一下关于对象池的简单使用方法:

 public class MyPooledClass {

     private static final SynchronizedPool sPool = 
             new SynchronizedPool(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);
     }

     . . .
 }

你可能感兴趣的:(android 对象池)