来自AndroidX的对象池

package com.abc;

import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
  Helper class for creating pools of objects. An example use looks like this:
 */
public final class Pools {

    private static final String TAG = "Pools";

    /**
     * Interface for managing a pool of objects.
     *
     * @param  The pooled type.
     */
    public interface Pool {

        /**
         * @return An instance from the pool if such, null otherwise.
         */
        @Nullable
        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.
         */
        boolean release(@NonNull T instance);
    }

    private Pools() {
        /* do nothing - hiding constructor */
    }

    /**
     * Simple (non-synchronized) pool of objects.
     *
     * @param  The pooled type.
     */
    public static class SimplePool implements Pool {
        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(@NonNull T instance) {
            Log.e(TAG,"release ");
            if (isInPool(instance)) {
                throw new IllegalStateException("Already in the pool!");
            }
            if (mPoolSize < mPool.length) {
                mPool[mPoolSize] = instance;
                mPoolSize++;
                Log.e(TAG,"release  true ");
                return true;
            }
            Log.e(TAG,"release  false ");
            return false;
        }

        private boolean isInPool(@NonNull T instance) {
            for (int i = 0; i < mPoolSize; i++) {
                if (mPool[i] == instance) {
                    return true;
                }
            }
            return false;
        }
    }

    /**
     * Synchronized) pool of objects.
     *
     * @param  The pooled type.
     */
    public static class SynchronizedPool extends SimplePool {
        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(@NonNull T instance) {
            synchronized (mLock) {
                return super.release(instance);
            }
        }
    }
}

使用如下:

public class PersonTrackInfo {

    private static final String TAG = "Pools";

    private boolean isFace;

    public boolean isFace() {
        return isFace;
    }

    public void setFace(boolean face) {
        isFace = face;
    }

    private static  final Pools.SynchronizedPool sPool =
            new Pools.SynchronizedPool(100);

    public static PersonTrackInfo obtain() {
        PersonTrackInfo instance = sPool.acquire();
        if(instance != null){
            return  instance ;
        }
        else
        {
            return    new PersonTrackInfo();
        }
    }

    public  void recycle() {
        sPool.release(this);
    }

}
  



你可能感兴趣的:(android)