根据某种条件定时删除Map中的key
2006-06-25 22:18
337 查看
根据某种条件定时删除Map中的key,主要用于一些定时删除key的操作
应该是在模仿httpsession的环境中应用比较多
package pack;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author Administrator
*
*/
public abstract class ExecuteTimeTask extends Thread {
private long time=60*100;//定时时间
private Map map=null;
/**
* condition is true, execute the clean operation
* @param key
* @param value
* @return
*/
public abstract boolean condition(Object key,Object value);
/* (non-Javadoc)
* @see java.lang.Thread#run()
*/
public void run() {
for(;;){
//
try {
synchronized (this) {
this.wait(time);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
if(map==null||map.size()<1){
continue;
}
for(java.util.Iterator it=map.entrySet().iterator();it.hasNext();){
Map.Entry entry=(Map.Entry)it.next();
if(condition(entry.getKey(),entry.getValue())){
it.remove();
};
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
ExecuteTimeTask t=new ExecuteTimeTask(){
public boolean condition(Object key, Object value) {
return key.toString().equals(value.toString())?true:false;
//return true;
}
};
Map m=new HashMap();
m.put("a", "2");
//m.put("2", "2");
t.setMap(m);
t.setTime(1000);
t.start();
long l=0;
for(;;){
try {
synchronized (Thread.currentThread()) {
Thread.currentThread().wait(2000);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.err.println("before--"+m+"---"+java.lang.Runtime.getRuntime().totalMemory());
m.put(l+"", l+"");
l++;
System.err.println("after"+m+"---nanotime--"+System.nanoTime()+"-----"+System.currentTimeMillis());
}
}
/**
* @return the map
*/
public Map getMap() {
return map;
}
/**
* @param map the map to set
*/
public void setMap(Map map) {
this.map = map;
}
/**
* @return the time
*/
public long getTime() {
return time;
}
/**
* @param time the time to set
*/
public void setTime(long time) {
this.time = time;
}
}
应该是在模仿httpsession的环境中应用比较多
package pack;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author Administrator
*
*/
public abstract class ExecuteTimeTask extends Thread {
private long time=60*100;//定时时间
private Map map=null;
/**
* condition is true, execute the clean operation
* @param key
* @param value
* @return
*/
public abstract boolean condition(Object key,Object value);
/* (non-Javadoc)
* @see java.lang.Thread#run()
*/
public void run() {
for(;;){
//
try {
synchronized (this) {
this.wait(time);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
if(map==null||map.size()<1){
continue;
}
for(java.util.Iterator it=map.entrySet().iterator();it.hasNext();){
Map.Entry entry=(Map.Entry)it.next();
if(condition(entry.getKey(),entry.getValue())){
it.remove();
};
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
ExecuteTimeTask t=new ExecuteTimeTask(){
public boolean condition(Object key, Object value) {
return key.toString().equals(value.toString())?true:false;
//return true;
}
};
Map m=new HashMap();
m.put("a", "2");
//m.put("2", "2");
t.setMap(m);
t.setTime(1000);
t.start();
long l=0;
for(;;){
try {
synchronized (Thread.currentThread()) {
Thread.currentThread().wait(2000);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.err.println("before--"+m+"---"+java.lang.Runtime.getRuntime().totalMemory());
m.put(l+"", l+"");
l++;
System.err.println("after"+m+"---nanotime--"+System.nanoTime()+"-----"+System.currentTimeMillis());
}
}
/**
* @return the map
*/
public Map getMap() {
return map;
}
/**
* @param map the map to set
*/
public void setMap(Map map) {
this.map = map;
}
/**
* @return the time
*/
public long getTime() {
return time;
}
/**
* @param time the time to set
*/
public void setTime(long time) {
this.time = time;
}
}
相关文章推荐
- 从标准输入中读取两个字符串 name:表示姓名,hobby:爱好;name作为key;hobby作为Value存入Map 然后根据姓名取得Value并打印,再根据name值删除map中的数据再打印ma
- Java根据条件删除Map中元素
- Java根据条件删除Map中元素
- 根据key删除Map集合中的key-value映射
- Map根据Valuse 输出对应的key(一)
- C++ map 根据value找key
- 如何在遍历list,vector,map时删除符合条件的元素
- 根据表单的name(名字)数组,返回对应的Map对象。Map对象的key是表单的name,Map对象的value是表单的value
- JAVA中对List<map<String,Object>>根据map某个key值进行排序
- MyBatis多查询条件-根据Map查询
- Propel 中根据条件删除记录的方法
- Map 根据value 获取key
- List<map<String,String>>根据选择的key去重
- map中根据value获取key
- PHP根据array_map、array_reduce和array_column获取二维数组中某个key的集合
- Java中Map根据键值(key)或者值(value)进行排序实现
- Java中Map根据键(Key)或者值(Value)进行排序
- Map中删除数据 或是key
- Map中根据value排序---输出value最大时所对应的Key
- map排序,根据key给map排序,根据value给map排序