java 并发学习(一)
来源:互联网 发布:映客直播网站源码 编辑:程序博客网 时间:2024/06/06 09:13
很多情况下 我们对缓存数据需要有时效性 的要求,可以借助 delayQueue 完成这一工作
import java.util.Map;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.DelayQueue;import java.util.concurrent.Delayed;import java.util.concurrent.TimeUnit;public class AutoCleanMap<K, V> { private final static Integer DEFAULT_TIME=5; private final static TimeUnit DEFAULT_TIMEUNIT=TimeUnit.SECONDS; private long effectiveTime; private TimeUnit timeUnit ; private Map<K, V> map = new ConcurrentHashMap<K, V>(); private DelayQueue<Data<K>> cleanQueue = new DelayQueue<Data<K>>(); public AutoCleanMap(long effectiveTime, TimeUnit timeUnit) { this.effectiveTime = effectiveTime; this.timeUnit = timeUnit; Thread thread = new Thread() { public void run() { for (;;) try { map.remove(cleanQueue.take().getKey()); } catch (InterruptedException e) { e.printStackTrace(); } } }; thread.setDaemon(true); thread.start(); } public AutoCleanMap() { this(DEFAULT_TIME, DEFAULT_TIMEUNIT); } public void put(K key, V value) { Data<K> data = new Data<K>(key, timeUnit.toNanos(effectiveTime)); synchronized (map) { if (map.get(key) != null) { cleanQueue.remove(data); } map.put(key, value); } cleanQueue.add(data); } public V get(K k) { return map.get(k); } private static class Data<K> implements Delayed { private K key; private long endTime; public Data(K key, long effectiveTime) { super(); this.key = key; this.endTime = effectiveTime + System.nanoTime(); } public K getKey() { return key; } @Override public int compareTo(Delayed o) { if (!(o instanceof Data)) return 1; if (this == o) return 0; @SuppressWarnings("unchecked") Data<K> data = (Data<K>) o; if (this.endTime == data.endTime) return 0; return this.endTime > data.endTime ? 1 : -1; } @Override public long getDelay(TimeUnit unit) { return endTime - System.nanoTime(); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((key == null) ? 0 : key.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; @SuppressWarnings("unchecked") Data<K> other = (Data<K>) obj; if (key == null) { if (other.key != null) return false; } else if (!key.equals(other.key)) return false; return true; } }}
0 0
- java 并发学习(一)
- 【JAVA并发学习一】并发和多线程
- Java 并发 学习 (一)
- java 并发深入学习一
- Java并发学习笔记一
- Java并发学习(一)-Executor
- 学习笔记-Java并发(一)
- Java并发学习(一)
- java 并发机制学习(一)
- java并发包学习笔记(一)
- JAVA高并发学习笔记(一)
- Java并发编程学习笔记(一)
- Java并发编程学习(一)
- java并发编程学习笔记(一)
- Java并发编程学习笔记(一)
- 系统学习java高并发系列一
- java学习(十二)—并发编程(一)
- 学习java并发编程实战(一)
- Oracle数据库高级查询案例
- 个人界面的禁止tableview的上拉效果
- Android——ViewPager和底部导航的配合
- 序列化 和 反序列化
- 在iPython Notebook下运行 pycaffe
- java 并发学习(一)
- 一些小题目
- Java基础知识—多态
- 工作中常用的Linux命令
- python基本语法
- 细节看出基本功
- SourceTree配置diff工具Beyond Compare
- NavController模块之Nav Guards
- leetcode 109. Convert Sorted List to Binary Search Tree