简述
ConcurrentLinkedHashMap 是google团队提供的一个容器。它有什么用呢?其实它本身是对
ConcurrentHashMap的封装,可以用来实现一个基于LRU策略的缓存。详细介绍可以参见
http://code.google.com/p/concurrentlinkedhashmap
使用范例
public static void main(String[] args) {
ConcurrentLinkedHashMap<Integer, Integer> map = new
ConcurrentLinkedHashMap.Builder<Integer,Integer>().maximumWeightedCapacity(2).
weigher(Weighers.singleton()).build();
map.put(1, 1);
map.put(2, 2);
map.put(3, 3);
System.out.println(map.get(1));//null 已经失效了
System.out.println(map.get(2));
}
ConcurrentLinkedHashMap 的构造函数比较特殊,它采用了Builder(构造器,GOF模式之一)。
它本身也是实现了ConcurrentMap接口的,所以使用起来跟ConcurrentHashMap一样。我们先put
进去三个元素,然后获取第一个元素,果然是null,因为基于LRU(最近使用)算法,key=1的节
点已经失效了。
源代码解析
先来看看它的整体框架
它本质是额外维护了一个双向链表,每次读和写都要改变相应节点的位置,将其移至队列头。
什么时候判断容易已经满了,是根据weight。每个元素都有一个weight,每增加一个元素,weight累计。当达到最大值的时候,就需要剔除最少操作的那个元素了,并且触发相关的事件。
我们先来看put函数
V put(K key, V value, boolean onlyIfAbsent) {
checkNotNull(value);
final int weight = weigher.weightOf(key, value);//计算weight
final WeightedValue<V> weightedValue = new WeightedValue<V>(value, weight);
final Node node = new Node(key, weightedValue);//对数据进行包装,准备存入
ConcurrentHashMap
for (;;) {
final Node prior = data.putIfAbsent(node.key, node);
if (prior == null) {//这个key之前没有值
afterCompletion(new AddTask(node, weight));//更新后续操作
return null;
} else if (onlyIfAbsent) {
afterCompletion(new ReadTask(prior));
return prior.getValue();
}
AddTask 是判断是否容量满了,需要剔除其他元素
final class AddTask extends AbstractTask {
final Node node;
final int weight;
@Override
@GuardedBy("evictionLock")
public void run() {
weightedSize += weight;
// ignore out-of-order write operations
if (node.get().isAlive()) {
evictionDeque.add(node);
evict();//是否移除失效的
}
}
}
void evict() {
while (hasOverflowed()) {
Node node = evictionDeque.poll();
// If weighted values are used, then the pending operations will adjust
// the size to reflect the correct weight
if (node == null) {
return;
}
// Notify the listener only if the entry was evicted
if (data.remove(node.key, node)) {//移除失效的
pendingNotifications.add(node);
}
node.makeDead();
}
}
get函数更简单一点,只是将这个key节点移至队列头
public V get(Object key) {
final Node node = data.get(key);
if (node == null) {
return null;
}
afterCompletion(new ReadTask(node));
return node.getValue();
}
性能比较 vs ConcurrentHashMap
不用说了,肯定是ConcurrentHashMap要好一点了,因为本文的主角还要维护一个操作队列嘛:)
不过性能上不是差很多,见下图。
总结:
利用ConcurrentLinkedHashMap来做基于LRU的缓存,还是值得推荐的。我们可以定义它的容器大小,基于LRU,就可以保证较高的命中率了。
参考资料:
http://code.google.com/p/concurrentlinkedhashmap
- 大小: 7.3 KB
- 大小: 46 KB
分享到:
相关推荐
google protobuf 最新源代码google protobuf 最新源代码google protobuf 最新源代码google protobuf 最新源代码google protobuf 最新源代码google protobuf 最新源代码google protobuf 最新源代码google protobuf ...
google搜搜 源代码 google搜搜 源代码 google搜搜 源代码
谷歌小恐龙HTML源代码
google搜索源代码
labview调用google地图源代码.zip源码Labview个人项目资料程序资源下载labview调用google地图源代码.zip源码Labview个人项目资料程序资源下载labview调用google地图源代码.zip源码Labview个人项目资料程序资源下载...
google protobuf对协议内容进行了编码和压缩,对协议安全很有意义
Google_Android_应用开发揭秘_源代码
谷歌小恐龙彩蛋源代码,很好玩的一个游戏,喜欢编程的同学看一下,下载下来学习一下,来学习一下。
GPS定位并调用谷歌地图源代码,本人原创绝对可用。
android google地图源代码 通过经纬度搜索地点
google防火墙源代码
深入浅出google android 源代码,初学者用,不错!
谷歌地图软件源代码谷歌地图软件源代码谷歌地图软件源代码
google日历前端源代码javascript-html-css整理 不包括后台程序和数据库,见谅 真正源代码,可以先试用再下载 演示地址:http://www.limagan.com/ 包含后台代码和数据库的压缩包已放出, 欢迎下载使用 ...
综合搜索、百度搜索、谷歌搜索、有道搜索、搜搜、源代码(搜索引擎调用代码大全)快速搜索、同步搜索代码!
Android谷歌网络地图定位演示源代码
在Win32平台下,假设源代码在E:/opensource/gtest-1.5.0目录。 2. Compile & install 2.1 Linux platform # cd /usr/src # wget http://googletest.googlecode.com/files/gtest-1.5.0.tar.gz # tar -...
费了半天才找到的源代码,很好用的,如果想学习google map api二次开发,他会提供很好帮助!
google 标准的早期版本的图像浏览器源代码。是和camera 放在一个应用里,现在已经放在了gallery 应用里