免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1251 | 回复: 0
打印 上一主题 下一主题

自动清除容器中的元素的List [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-06-24 09:12 |只看该作者 |倒序浏览
  1. import java.util.AbstractList;
  2. import java.util.Date;
  3. import java.util.Iterator;
  4. import java.util.LinkedHashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Timer;
  8. import java.util.TimerTask;
  9. import java.util.concurrent.locks.ReentrantLock;

  10. /**
  11. * This class provides a simple implementation of the AbstractList.
  12. * It keeps a deamon thread to remove the element of LinkedHashMap in it,
  13. * so all the method for removing is not provided.
  14. * AutoDrownList is synchronized.
  15. * @author whaon
  16. * @date 2015年6月11日
  17. * @version 0.1
  18. */
  19. public class AutoDrownList<T> extends AbstractList<T> {
  20.     //Map<T, Long> m = Collections.synchronizedMap(new LinkedHashMap());
  21.      
  22.     ReentrantLock lock = new ReentrantLock();
  23.     Map<T, Long> m = new LinkedHashMap<T, Long>();
  24.      
  25.     private static final long SECONDS = 1000;
  26.      
  27.     private long interval;
  28.      
  29.     private long ttl;
  30.      
  31.     private Timer timer;
  32.      
  33.     /**
  34.      *
  35.      * @param interval:time in seconds for task executions.
  36.      * @param ttl:time to live for element
  37.      */
  38.     public AutoDrownList(long interval, long ttl) {
  39.         this.interval = interval * SECONDS;
  40.         this.ttl = ttl * SECONDS;
  41.         start();
  42.     }
  43.      
  44.     @Override
  45.     public boolean add(T t) {
  46.         lock.lock();
  47.         try {
  48.             if(m.containsKey(t)) {
  49.                 return false;
  50.             }
  51.             m.put(t, System.currentTimeMillis());
  52.         } finally {
  53.             lock.unlock();
  54.         }
  55.          
  56.         return true;
  57.     }
  58.      
  59.     private void start() {
  60.         timer = new Timer("MaintainList-" + new Date(), true);
  61.         timer.schedule(new MaintainListTimerTask(m), 0, interval);
  62.     }
  63.      
  64.     private class MaintainListTimerTask extends TimerTask {
  65.          
  66.         Map<T, Long> m;
  67.          
  68.         MaintainListTimerTask(Map<T, Long> m) {
  69.             this.m = m;
  70.         }

  71.         @Override
  72.         public void run() {
  73.             long now = System.currentTimeMillis();
  74.             lock.lock();
  75.             try {
  76.                 Iterator<Map.Entry<T, Long>> i = m.entrySet().iterator();
  77.                 while(i.hasNext()) {
  78.                     Map.Entry<T, Long> e = i.next();
  79.                     //System.out.println(e.getKey() + "/" + e.getValue());
  80.                     if((now - e.getValue()) > ttl) {
  81.                         i.remove();
  82.                     } else {
  83.                         break;
  84.                     }
  85.                 }
  86.             } finally {
  87.                 lock.unlock();
  88.             }
  89.         }
  90.          
  91.     }
  92.      
  93.     @Override
  94.     public String toString() {
  95.         return "MaintainList [m=" + m + "]";
  96.     }

  97.     @Override
  98.     public T get(int index) {
  99.         T t = null;
  100.         if(index < 0) {
  101.             return null;
  102.         }
  103.         lock.lock();
  104.         try {
  105.             Iterator<T> it = m.keySet().iterator();
  106.             while(it.hasNext()) {
  107.                 t = it.next();
  108.                 if(index-- == 0) {
  109.                     break;
  110.                 }
  111.             }
  112.             
  113.         } finally {
  114.             lock.unlock();
  115.         }
  116.         return t;
  117.     }

  118.     @Override
  119.     public int size() {
  120.         int ret = 0;
  121.         lock.lock();
  122.         try {
  123.             ret = m.size();
  124.         } finally {
  125.             lock.unlock();
  126.         }
  127.         return ret;
  128.     }
  129.      
  130.      
  131. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP