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来加锁.
2) 在Java中,有时还会遇到子类中的成员变量或方法与超类(有时也称父类)中的成员变量或方法同名。因为子类中的成员变量或方法名优先级高,所以子类中的同名成员变量或方法就隐藏了超类的成员变量或方法,但是我们如果想要使用超类中的这个成员变量或方法,就需要用到super.
3)super的另外一个作用是调用父类的protected函数。只有通过"super"这个魔咒,我们才能操作父类的protected成员,别无它法。
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而你下面的方法名又写错了,这时你的编译器是可以通过的(它以为这个方法是你的子类中自己增加的方法)。
- Pools.java
- Java theory and practice: Thread pools and work queues
- 《Java Concurrency in Practice》ch8 Applying Thread Pools
- TYPE-POOLS
- Object Pools
- Autorelease Pools
- Thread Pools
- Worker Pools
- Thread Pools
- TYPE POOLS 的用法...
- TYPE-POOLS SLIS
- Three useful code pools
- TYPE-POOLS SLIS
- 线程池 Thread Pools
- Thread Pools 线程池
- 解读commons-pools
- TYPE-POOLS SLIS
- DMA pools的使用
- JAVA基础_构造函数与函数重载
- 在Linux中安装FTP Server 配置FTP Server,JDK安装,Tomcat 安装,简易教程
- Java入门到精通——工具篇之Maven概述
- OpenGL函数-glClear
- 大学那些事(5)
- Pools.java
- Bret Victor: Learnable Programming
- Github clone submodule Permission denied
- pringmvc整合redis架构搭建实例
- Cannot create a server using the selected type
- Thread的run()与start()的差别
- 代码实现UIPickerView
- iOS小项目---UIColor采用rgb
- Acdream 1081 梦