标签:
设计初衷:
网站在提供服务的过程中,会创建很多对象,bean,dao层的对象尤为频繁,然而这些对象是可以重复利用的.
设计思路:
对象连接池ObjectPool才用单态的设计模式,自带线程,每隔一段时间调用该类的
clearObject方法,
为了保证同步,涉及线程安全的方法全都使用了synchronized关键字
每个方法的流程控制根据存储值会出现的状态,以及会影响存储值状态的数据决定
要存储进ObjectPool的类需要实现AllowEnterObjectPool接口
主要属性介绍:
HashMap<String,ArrayList> hashMapForStorageObject,key是包名+类名,value存储改类的实例,每次取出放回都会从0的位置
HashMap<String,Integer> hashMapForObjectUsedTime key是包名+类名+@+对象hashcode Integer记录了对象被取出次数
主要方法介绍:
takeOut的方法利用泛型,只需传给类的Class,便可返回对应类型,利用反射批量添加对象
put 是对象恢复到初始状态(由程序员指定),放入到对应ArrayList的0位置
clearObject,由内部线程调用,遍历hashMapForStorageObject每个ArrayList,如果ArrayList总数大于最小存储数,再去
ashMapForObjectUsedTime查找使用记录,将符合条件的对象删除,最后清零使用记录
代码及使用方式:
package myFrame;
public interface AllowEnterObjectPool
{
public ObjectPool objectPool = ObjectPool.getInstance();
//将对象放入改类时,需调用改方法,将对象恢复带默认状态
public void initialMember();
//当调用takeOut方法,发现没有可用的对象使,会调用改方法批量加入
public void batchAddSelf();
}
package myFrame;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import com.sun.security.auth.NTDomainPrincipal;
import Dao.TestDao;
public class ObjectPool implements Debug,Assistant
{
//当只剩下30个对象的时候,就不进行删除行为了
private int minNumber = 30;
//每隔多少时间进行检查
private int checkTime = 60*60*1000;
//每个类对应一个ArrayList
// key值已包名+类名的方式
// value值是存储该对象的Arraylist
// value影响程序流程的值 null(没有找到key) 0(size)
// 影响value的状态的值 minNumber
private HashMap<String,ArrayList> hashMapForStorageObject = new HashMap<String, ArrayList>();
//每个对象对应一个Integer,记录一段时间内被调用得次数
/*
* key值已包名+类名+@+对象hashcode
value 记录了该对象被取出的次数
value影响程序流程的值 null(没有找到key) >0(被使用过)
*/
private HashMap<String,Integer> hashMapForObjectUsedTime = new HashMap<String, Integer>();
private static ObjectPool objectPool = new ObjectPool();
private ObjectPool()
{
new clock().start();
}
//得到一个类的实例,每次都从0获取,并从ArrayList中删除
public<T> T takeOut(Class<T> object)
{
synchronized (this)
{
String className = object.getName();
ArrayList<T> objectList = hashMapForStorageObject.get(className);
if(objectList == null)
{
objectList = new ArrayList<T>();
hashMapForStorageObject.put(className,objectList);
T createObject = null;
try
{
createObject = object.newInstance();
Method batchAddSelf = object.getMethod("batchAddSelf");
batchAddSelf.invoke(createObject);
objectList.add(createObject);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(objectList.size()==0)
{
T createObject = null;
try
{
createObject = object.newInstance();
Method batchAddSelf = object.getMethod("batchAddSelf");
batchAddSelf.invoke(createObject);
objectList.add(createObject);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
T returnObject = objectList.get(0);
String flag = className+"@"+Integer.toHexString(returnObject.hashCode());
Integer i = hashMapForObjectUsedTime.get(flag);
if(i == null)
{
i = 0;
}
hashMapForObjectUsedTime.put(flag,++i);
objectList.remove(returnObject);
return returnObject;
}
}
/*
* 将对象恢复到初始状态后,放入0位置
*/
public synchronized void put(AllowEnterObjectPool allowEnterObjectPool)
{
allowEnterObjectPool.initialMember();
String className = allowEnterObjectPool.getClass().getName();
ArrayList objectList = hashMapForStorageObject.get(className);
if(objectList == null)
{
objectList = new ArrayList();
hashMapForStorageObject.put(className,objectList);
}
objectList.add(0,allowEnterObjectPool);
}
/*
* 已hashMapForStorageObject为准,取出一个ArrayList A,遍历里面的对象,例如对a ,组成hashMapForObjectUsedTime的key值
* 当A的容量大于minnumber时,则通过key查找hashMapForObjectUsedTime,得到value为null的话,则删除A里面的对应对象
* 将hashMapForObjectUsedTime数据清除
*/
public synchronized void clearObject()
{
Iterator<String> iterator = hashMapForStorageObject.keySet().iterator();
String key,flag;
ArrayList list;
while (iterator.hasNext())
{
key = iterator.next();
list = hashMapForStorageObject.get(key);
Object object;
Integer usedTime;
if (list.size()<minNumber)
{
continue;
}
for(int i=0;i<list.size();i++)
{
object = list.get(i);
flag = key+"@"+Integer.toHexString(object.hashCode());
usedTime = hashMapForObjectUsedTime.get(flag);
if(usedTime == null)
{
list.remove(i);
hashMapForObjectUsedTime.remove(key);
i--;
if(list.size() == minNumber) break;
}
}
}
hashMapForObjectUsedTime.clear();
}
public static ObjectPool getInstance()
{
return objectPool;
}
private class clock extends Thread
{
@Override
public void run()
{
System.out.println("时钟启动");
while(true)
{
try
{
Thread.sleep(checkTime);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
clearObject();
}
}
}
public static void main(String[] args)
{
Message message = objectPool.takeOut(Message.class);
objectPool.put(message);
System.out.println(message);
}
}
package myFrame;
public class Message implements AllowEnterObjectPool
{
private String name;
private int age;
public Message()
{
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public void initialMember() {
// TODO Auto-generated method stub
name = "";
age = 0;
}
@Override
public void batchAddSelf()
{
Message message;
for(int i=0;i<10;i++)
{
message = new Message();
objectPool.put(message);
}
}
}
标签:
原文地址:http://www.cnblogs.com/resolvent/p/5746901.html