美文网首页
一个设计精巧的Pool管理容器类

一个设计精巧的Pool管理容器类

作者: 王岩_shang | 来源:发表于2016-06-29 19:28 被阅读57次

源码(可以参考,直接使用)

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

例子

/**
 * Helper class for crating pools of objects. An example use looks like this:
 * */
  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);
       }
  
       //. . .
    } 

相关文章

  • 一个设计精巧的Pool管理容器类

    源码(可以参考,直接使用) 例子

  • JUC系列03-同步容器类

    1 同步容器类 同步容器类主要是指java.util.concurrent下的集合类,这些类的设计主要是用于提高多...

  • Laravel核心概念

    服务容器 Laravel 服务容器是一个用于管理类依赖和执行依赖注入的强大工具。 容器 简单来说,容器是一个装载对...

  • @Import 注解

    @Import 注解可以普通类导入到 IoC容器中。 想要让一个普通类接受 Spring 容器管理,有以下方法 使...

  • 迭代器模式

    迭代器模式 意图:将容器的存储结构和容器的展示分离开设计原则:单一职责原则,一个类只有一个变化的理由类图:

  • Spring快速入门

    Spring Spring是什么 Spring是一个开源的轻量级框架,是一个管理Bean的容器(普通Java类容器...

  • AutoreleasePool

    Autorelease Pool:是iOS内存管理机制中很重要的一个部分。 Autorelease Pool字面上...

  • golang通用连接池,支持GRPC,RPC,TCP

    pool 项目地址 https://github.com/flyaways/pool Pool 用于管理客户端到服...

  • c++template-基础篇(二)

    什么是类模板? 通常被用于管理某种特定类型的元素,容器类就是其中的一个典型例子。它可以用于实现容器类而不需要确定容...

  • nginx学习第四天

    ngx_pool_t ngx_pool_t是非常重要的而数据结构 1.对内存的管理。总是从一个ngx_pool_t...

网友评论

      本文标题:一个设计精巧的Pool管理容器类

      本文链接:https://www.haomeiwen.com/subject/elexjttx.html