根据某种条件定时删除Map中的key

根据某种条件定时删除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;
 }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java的Timer类和TimerTask类来实现定时删除Map的超时数据。具体实现步骤如下: 1. 定义一个包含Map的类,并在该类定义一个Timer对象和一个TimerTask对象。 2. 在TimerTask对象重写其run()方法,将需要删除的超时数据从Map删除。 3. 在Timer对象调用schedule()方法,设置定时执行的时间和间隔时间。 4. 在使用Map时,每次插入数据时,同时记录该数据的插入时间。 5. 在TimerTask的run()方法,遍历Map的所有数据,将超时的数据删除。 下面是一个示例代码: ```java import java.util.HashMap; import java.util.Map; import java.util.Timer; import java.util.TimerTask; public class MapDemo { private Map<String, Long> map = new HashMap<>(); private Timer timer = new Timer(); private TimerTask task = new TimerTask() { @Override public void run() { long now = System.currentTimeMillis(); for (String key : map.keySet()) { if (now - map.get(key) > 60 * 1000) { // 超时时间为1分钟 map.remove(key); } } } }; public void put(String key, Object value) { map.put(key, System.currentTimeMillis()); // 插入数据时,同时记录插入时间 } public Object get(String key) { return map.get(key); } public Map<String, Long> getAll() { return map; } public void startTimer() { timer.schedule(task, 60 * 1000, 60 * 1000); // 定时执行的时间和间隔时间均为1分钟 } public static void main(String[] args) { MapDemo demo = new MapDemo(); demo.startTimer(); // 启动定时删除超时数据的任务 // 测试插入数据 demo.put("key1", "value1"); demo.put("key2", "value2"); demo.put("key3", "value3"); System.out.println(demo.getAll()); // 等待一段时间后再次测试 try { Thread.sleep(2 * 60 * 1000); // 等待2分钟 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(demo.getAll()); } } ``` 在上面的代码定时删除超时数据的任务是在startTimer()方法启动的,间隔时间为1分钟。在测试时,先插入3条数据,然后等待2分钟后再次输出Map的所有数据,可以看到超时的数据已经被成功删除了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值