Simple object pool (ObjectPool) implementation

a few points

  • Object creation
  • Object reuse
  • Object recycling
  • Object memory release

Solution 1: Bean object with object pool


package chenggong.test;

import java.util.Objects;

/**
 * 对象池。用了一个简单的链表来实现。参考Android的Message类。
 * ObjectPool 是一个bean类。
 *
 * @since 2021-07-25
 */
public class ObjectPool {
    
    
    private static final String TAG = "ObjectPool";

    private static final Object POOL_LOCK = new Object();
    private static final int MAX_POOL_SIZE = 20;

    private static ObjectPool sPool;
    private static int sPoolSize;

    private String name;
    private int number;
    private ObjectPool next;

    public ObjectPool() {
    
    
    }

    public ObjectPool(String name, int number) {
    
    
        this.name = name;
        this.number = number;
    }

    public static ObjectPool obtain() {
    
    
        obtain(null, 0);
    }

    public static ObjectPool obtain(String name, int number) {
    
    
        synchronized (POOL_LOCK) {
    
    
            if (sPool != null) {
    
    
                ObjectPool object = sPool;
                sPool = object.next;
                object.next = null;
                sPoolSize--;

                object.name = name;
                object.number = number;
                return object;
            }
        }
        return new ObjectPool(name, number);
    }

    public void recycle() {
    
    
        name = null;
        number = 0;
        synchronized (POOL_LOCK) {
    
    
            if (sPoolSize < MAX_POOL_SIZE) {
    
    
                next = sPool;
                sPool = this;
                sPoolSize++;
            }
        }
    }

    public static void release() {
    
    
        synchronized (POOL_LOCK) {
    
    
            sPool = null; // 链表第一个节点为空,释放链表
            sPoolSize = 0;
        }
    }

    @Override
    public boolean equals(Object o) {
    
    
        if (this == o)
            return true;
        if (!(o instanceof ObjectPool))
            return false;
        ObjectPool that = (ObjectPool) o;
        return number == that.number && Objects.equals(name, that.name);
    }

    @Override
    public int hashCode() {
    
    
        return Objects.hash(name, number);
    }

    @Override
    public String toString() {
    
    
        return "ObjectPool{" + "name=" + name + ", number=" + number + '}';
    }
}

Solution 2 object pool template


package chenggong.test;

import java.util.LinkedList;
import java.util.Objects;

import android.util.Log;

/**
 * 对象池
 *
 * @since 2021-07-25
 */
public class ObjectPool<T> {
    
    
    private static final String TAG = "ObjectPool";

    private static final int MAX_POOL_SIZE = 20;
    private final Object mPoolLock = new Object();

    private LinkedList<T> mPool;
    private Factory<T> mFactory;

    public ObjectPool(Factory<T> factory) {
    
    
        mFactory = factory;
        mPool = new LinkedList<>();
    }

    public T obtain() {
    
    
        synchronized (mPoolLock) {
    
    
            if (!mPool.isEmpty()) {
    
    
                return mPool.removeFirst();
            }
        }
        return mFactory.create();
    }

    public void recycle(T t) {
    
    
        mFactory.recycle(t);
        synchronized (mPoolLock) {
    
    
            if (mPool.size() < MAX_POOL_SIZE) {
    
    
                mPool.add(t);
            }
        }
    }

    public void release() {
    
    
        synchronized (mPoolLock) {
    
    
            Log.i(TAG, "mPool size is " + mPool);
            mPool.clear(); // 清空链表
        }
    }

    @Override
    public boolean equals(Object o) {
    
    
        if (this == o)
            return true;
        if (!(o instanceof ObjectPool))
            return false;
        ObjectPool<?> that = (ObjectPool<?>) o;
        return Objects.equals(mPool, that.mPool) && Objects.equals(mFactory, that.mFactory);
    }

    @Override
    public int hashCode() {
    
    
        return Objects.hash(mPool, mFactory);
    }

    @Override
    public String toString() {
    
    
        return "ObjectPool{" + "mPool=" + mPool + ", mFactory=" + mFactory + '}';
    }
}
package chenggong.test;

/**
 * 对象工厂, 用于创建和回收对象
 *
 * @since 2021-07-26
 */
public interface Factory<T> {
    
    
    T create();
    void recycle(T t);
}

Guess you like

Origin blog.csdn.net/followYouself/article/details/119084820