chinarocky 发表于 2015-06-24 09:12

自动清除容器中的元素的List

import java.util.AbstractList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.locks.ReentrantLock;

/**
* This class provides a simple implementation of the AbstractList.
* It keeps a deamon thread to remove the element of LinkedHashMap in it,
* so all the method for removing is not provided.
* AutoDrownList is synchronized.
* @author whaon
* @date 2015年6月11日
* @version 0.1
*/
public class AutoDrownList<T> extends AbstractList<T> {
    //Map<T, Long> m = Collections.synchronizedMap(new LinkedHashMap());
   
    ReentrantLock lock = new ReentrantLock();
    Map<T, Long> m = new LinkedHashMap<T, Long>();
   
    private static final long SECONDS = 1000;
   
    private long interval;
   
    private long ttl;
   
    private Timer timer;
   
    /**
   *
   * @param interval:time in seconds for task executions.
   * @param ttl:time to live for element
   */
    public AutoDrownList(long interval, long ttl) {
      this.interval = interval * SECONDS;
      this.ttl = ttl * SECONDS;
      start();
    }
   
    @Override
    public boolean add(T t) {
      lock.lock();
      try {
            if(m.containsKey(t)) {
                return false;
            }
            m.put(t, System.currentTimeMillis());
      } finally {
            lock.unlock();
      }
         
      return true;
    }
   
    private void start() {
      timer = new Timer("MaintainList-" + new Date(), true);
      timer.schedule(new MaintainListTimerTask(m), 0, interval);
    }
   
    private class MaintainListTimerTask extends TimerTask {
         
      Map<T, Long> m;
         
      MaintainListTimerTask(Map<T, Long> m) {
            this.m = m;
      }

      @Override
      public void run() {
            long now = System.currentTimeMillis();
            lock.lock();
            try {
                Iterator<Map.Entry<T, Long>> i = m.entrySet().iterator();
                while(i.hasNext()) {
                  Map.Entry<T, Long> e = i.next();
                  //System.out.println(e.getKey() + "/" + e.getValue());
                  if((now - e.getValue()) > ttl) {
                        i.remove();
                  } else {
                        break;
                  }
                }
            } finally {
                lock.unlock();
            }
      }
         
    }
   
    @Override
    public String toString() {
      return "MaintainList ";
    }

    @Override
    public T get(int index) {
      T t = null;
      if(index < 0) {
            return null;
      }
      lock.lock();
      try {
            Iterator<T> it = m.keySet().iterator();
            while(it.hasNext()) {
                t = it.next();
                if(index-- == 0) {
                  break;
                }
            }
            
      } finally {
            lock.unlock();
      }
      return t;
    }

    @Override
    public int size() {
      int ret = 0;
      lock.lock();
      try {
            ret = m.size();
      } finally {
            lock.unlock();
      }
      return ret;
    }
   
   
}
页: [1]
查看完整版本: 自动清除容器中的元素的List