google的ConcurrentLinkedHashmap源代码解析
来源:互联网 发布:大数据公司如何盈利 编辑:程序博客网 时间:2024/05/20 05:24
转载自:http://janeky.iteye.com/blog/1534352
简述
ConcurrentLinkedHashMap 是google团队提供的一个容器。它有什么用呢?其实它本身是对
ConcurrentHashMap的封装,可以用来实现一个基于LRU策略的缓存。详细介绍可以参见
http://code.google.com/p/concurrentlinkedhashmap
使用范例
- public static void main(String[] args) {
- ConcurrentLinkedHashMap<Integer, Integer> map = new
- <span style="white-space: pre;"> </span>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
- google的ConcurrentLinkedHashmap源代码解析
- ConcurrentLinkedHashMap设计与代码解析
- concurrentlinkedhashmap
- Google开放HTML5解析库Gumbo的源代码
- ConcurrentLinkedHashMap 这货竟然没有链表的属性
- vsftpd的源代码 解析
- cvAdaptiveThreshold源代码的解析
- Google主页导航动画效果演示、下载及源代码解析
- Google主页导航动画效果演示、下载及源代码解析
- google开放了android的源代码
- Google的Go编程语言源代码(Windows)
- 怎么下载google code 上的源代码?
- google code 上源代码的下载方法:
- google code 上源代码的下载方法
- google code 上源代码的下载方法:
- 学习Google的C++开源代码项目
- 怎么下载google code上的源代码
- 怎么下载google code 上的源代码?
- C++ std::sort 函数的STL内部错误
- 翻译《有关编程、重构及其他的终极问题?》——23.自动获取字符串的长度
- PRML读书笔记——图模型
- Eclipse常用快捷键及其他快捷键
- 兼容性和浏览器hack
- google的ConcurrentLinkedHashmap源代码解析
- caffe-ssd训练kitti、lisa数据集
- c++中std::set自定义去重和排序函数
- 北斗用户机接收机基带信号处理
- FFmpeg codec HOWTO
- HADOOP+ZOOKEEPER+HBASE+HIVE
- C 标准库的 setlocale() 函数用法
- android developer tiny share-201704014
- js相关知识