android android.support.v4.util.Pools 源码解析

文章目录

      • android android.support.v4.util.Pools 源码解析
          • `Pool`是一个接口,带泛型的接口。里面就两个方法,一个是取数据,一个是存数据。
          • `SimplePool`是具体实现,类如其名,实现的逻辑非常简单。
          • `SynchronizedPool`是对`SimplePool`的同步实现

路径是在 android.support.v4.util,该类属于工具类。代码比较简单,忽然翻到,就简单分析一下。

#####首先 Pools只是一个空壳子。然后里面的代码才是真正的代码。

Pool是一个接口,带泛型的接口。里面就两个方法,一个是取数据,一个是存数据。
SimplePool是具体实现,类如其名,实现的逻辑非常简单。
  • 首先通过构造方法创建一个指定大小的数组:Object[size];
  • T acquire()方法就是去获取Object[size-1]对应的数据,每次获取之后,size-- , 如果一直获取,超过了size个数了,就会获取null. (实际上,Object[index] 也是null ,看代码里面只是初始化了数组,并没有初始化数组中的元素。。。。)
  • boolean release(T obj)的操作是acquire()的反操作,也就是将数据放回之前的数组Object[size]中,然后放进去之后,size++。放之前先判断当前元素是不是已经存在于数组中了。
  • boolean isInPool(T t) 逻辑也很简单的,就是遍历当前数组,把数组的元素拿出来一个个与目标元素比较,如果有相等的,就说明已存在,否则不存在。
SynchronizedPool是对SimplePool的同步实现

其实SimplePool代码更简单,完全是利用了SimplePool,只是在 acquire() / release() 前加了同步关键字保护。


就目前而言,我非常怀疑这个类的存在意义。得到实际场景才能看到其存在的意义。

下面是该类的源码。如你所见,非常的简单。

/* * Copyright (C) 2013 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package android.support.v4.util;/** * Helper class for creating pools of objects. An example use looks like this: * 
 * 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); *     } * *     . . . * } * 
* */public final class Pools { /** * Interface for managing a pool of objects. * * @param The pooled type. */ public static interface Pool { /** * @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 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(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 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(T element) { synchronized (mLock) { return super.release(element); } } }}

更多相关文章

  1. Android使用Kotlin+Anko开发简化开发中的代码
  2. 使用Menu制作弹出菜单
  3. android 9.0 访问http
  4. 【Android学习笔记】Spinner
  5. android缓存框架ASimpleCache
  6. Android(安卓)不得不知 之 Serializable 与 Parcelable
  7. GreenDao的更新,不会使数据删除所导致重新创建新表的示例
  8. Android程序优化之异常终止数据恢复
  9. android 手机UDP 接受不到数据

随机推荐

  1. 修改android屏幕DPI
  2. Android 获取路径
  3. Android使用TextView实现无下划线超链接
  4. android修改framework固定屏幕
  5. Android 连接Wifi和创建Wifi热点
  6. [置顶] 我的Android进阶之旅------>Andro
  7. Notification 事件 使用
  8. 查看Android(安卓)各种API版本占据当前市
  9. 学习android无私的资源
  10. 敏捷软件测试的七个关键成功要素 Android