Pools.java

来源:互联网 发布:带约束的最优化问题 编辑:程序博客网 时间:2024/06/06 05:36

/* * Copyright (C) 2009 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.util;/** * Helper class for crating pools of objects. An example use looks like this: * <pre> * 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); *     } * *     . . . * } * </pre> * * @hide */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);            }        }    }}


1、static的三种用途:
1)static局部变量存储与静态存储区,如果没有赋值则初始化为0,且只初始化一次;
2)不能被其他文件访问的全局变量和函数;
3)静态数据成员和成员函数,表示不属于某个类的对象,而只属于类,无this指针(C++中)。
2、final:
final类不能被继承,没有子类,final类中的方法默认是final的。
方法不能被子类的方法覆盖,但可以被继承。
成员变量表示常量,只能被赋值一次,赋值后值不再改变。
不能用于修饰构造方法。
 注意:父类的private成员方法是不能被子类方法覆盖的,因此private类型的方法默认是final类型的。
 3、interface
相对abstract class(抽象类)来讲,interface则造出了“完全抽象的class”,丝毫不带半点实现的内容。且interface中的所有methods都是虚的空的。
interface中的方法无需声明,都会自动设为了public。
当然,interface中的数据成员也变为了public,static,final。

要注意,当某个类(like class B)实现了一个接口时,一定要把它实现的methods(函数)标明为public否则编译器不会让你好过。在java中,只能继承一个一般的Class(non-interface即非接口类),但你却可以继承多个interface。

4、implements一般是实现接口。extends 是继承类。

5、synchronized 关键字,代表这个方法加锁,相当于不管哪一个线程(例如线程A),运行到这个方法时,都要检查有没有其它线程B(或者C、 D等)正在用这个方法,有的话要等正在使用synchronized方法的线程B(或者C 、D)运行完这个方法后再运行此线程A;没有的话,直接运行。

1)方法声明时使用,放在范围操作符(public等)之后,返回类型声明(void等)之前.这时,线程获得的是成员锁,即一次只能有一个线程进入该方法,其他线程要想在此时调用该方法,只能排队等候,当前线程(就是在synchronized方法内部的线程)执行完该方法后,别的线程才能进入.

  public synchronized void synMethod() {
        //方法体
      }

2)对某一代码块使用,synchronized后跟括号,括号里是变量,这样,一次只有一个线程进入该代码块.此时,线程获得的是成员锁

   public int synMethod(int a1){
        synchronized(a1) {
          //一次只能有一个线程进入
        }

      }

3)synchronized后面括号里是一对象,此时,线程获得的是对象锁.如果线程进入,则得到当前对象锁,那么别的线程在该类所有对象上的任何操作都不能进行.在对象级使用锁通常是一种比较粗糙的方法。为什么要将整个对象都上锁,而不允许其他线程短暂地使用对象中其他同步方法来访问共享资源?如果一个对象拥有多个资源,就不需要只为了让一个线程使用其中一部分资源,就将所有线程都锁在外面。

4) synchronized后面括号里是类,此时,线程获得的是对象锁.如果线程进入,则线程在该类中所有操作不能进行,包括静态变量和静态方法,实际上,对于含有静态方法和静态变量的代码块的同步,我们通常用4来加锁.

6、super:
1)表示调用父类的构造函数。也是一个特殊语法,不是变量,没有什么类型。 子类的构造函数如果要引用super的话,必须把super放在函数的首位.
2) 在Java中,有时还会遇到子类中的成员变量或方法与超类(有时也称父类)中的成员变量或方法同名。因为子类中的成员变量或方法名优先级高,所以子类中的同名成员变量或方法就隐藏了超类的成员变量或方法,但是我们如果想要使用超类中的这个成员变量或方法,就需要用到super.
3)super的另外一个作用是调用父类的protected函数。只有通过"super"这个魔咒,我们才能操作父类的protected成员,别无它法。
7.super和this的异同:
       1)super(参数):调用基类中的某一个构造函数(应该为构造函数中的第一条语句) 
         2)this(参数):调用本类中另一种形成的构造函数(应该为构造函数中的第一条语句)
   3)super: 它引用当前对象的直接父类中的成员(用来访问直接父类中被隐藏的父类中成员数据或函数,基类与派生类中有相同成员定义时如:super.变量名    super.成员函数据名(实参)
       4)this:它代表当前对象名(在程序中易产生二义性之处,应使用this来指明当前对象;如果函数的形参与类中的成员数据同名,这时需用this来指明成员变量名)
5)调用super()必须写在子类构造方法的第一行,否则编译不通过。每个子类构造方法的第一条语句,都是隐含地调用super(),如果父类没有这种形式的构造函数,那么在编译的时候就会报错。
     6)super()和this()类似,区别是,super()从子类中调用父类的构造方法,this()在同一类内调用其它方法。
     7)super()和this()均需放在构造方法内第一行。
     8)尽管可以用this调用一个构造器,但却不能调用两个。
     9)this和super不能同时出现在一个构造函数里面,因为this必然会调用其它的构造函数,其它的构造函数必然也会有super语句的存在,所以在同一个构造函数里面有相同的语句,就失去了语句的意义,编译器也不会通过。
     10)this()和super()都指的是对象,所以,均不可以在static环境中使用。包括:static变量,static方法,static语句块。
     11)从本质上讲,this是一个指向本对象的指针, 然而super是一个Java关键字。
@Override是伪代码,表示重写(当然不写也可以),不过写上有如下好处: 
1、可以当注释用,方便阅读;
2、编译器可以给你验证@Override下面的方法名是否是你父类中所有的,如果没有则报错。比如你如果没写@Override而你下面的方法名又写错了,这时你的编译器是可以通过的(它以为这个方法是你的子类中自己增加的方法)。
 
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 哭的时候踹不过气来应该怎么办 孩子在幼儿园被老师罚站怎么办 孩子屁股打肿了又红又紫怎么办 生完孩子两个月一直浑身疼怎么办 宝宝被蚊子叮咬后红肿硬怎么办 小孩屁股青一块紫一块打的怎么办 小孩的手被鞭子抽红了怎么办 孩子每次写作业都要挨打挨骂怎么办 儿子四岁脾气特别大怎么办呢 月子里屁股被开水烫了怎么办 学生打闹家长只找老师责任怎么办 两个学生打闹受伤的孩子家长怎么办 小孩学习不好做家长的该怎么办 对学习不入门的小孩家长该怎么办 孩子老做作业发神上课不专心怎么办 儿子成绩考得差不专心未来怎么办 五年级学生写字慢又丑怎么办 宝宝上课坐不住不听老师话怎么办 三岁宝宝特调皮打他还还手怎么办 怀孕40天不知道喝酒了怎么办 怀孕四十天的时候喝酒抽烟了怎么办 宝宝怀孕三十天左右喝酒了怎么办 两个人都喝酒了意外怀孕怎么办 不知道自己怀孕了喝了很多酒怎么办 不知道自己怀孕了喝了一次酒怎么办 我儿子11岁了有多动症怎么办 面对老师的冷暴力家长该怎么办? 面对无德的老师家长该怎么办 如果你家长屏蔽老师老师该怎么办 小孩出完水痘后身上出现疱疹怎么办 脑子里兴奋的头疼怎么办 吃什么药 一个月宝宝异常兴奋不睡觉怎么办 四个月宝宝晚上兴奋不睡觉怎么办 20个月宝宝半夜惊醒哭闹怎么办 小孩吃了氨茶碱兴奋不睡觉怎么办 孩子在学校被同学撞鼻骨折怎么办 孩子在学校无意致使同学受伤怎么办 9个月的宝宝吃坏东西腹泻怎么办 8个月发烧到38度怎么办 1岁半宝宝鼻塞发烧38度6怎么办 八个月的宝宝发热38度怎么办