【源代码】LruCache源代码剖析
上一篇分析了LinkedHashMap源代码,这个Map集合除了拥有HashMap的大部分特性之外。还拥有链表的特点,即能够保持遍历顺序与插入顺序一致。
另外。当我们将accessOrder设置为true时。能够使遍历顺序和訪问顺序一致,其内部双向链表将会依照最近最少訪问到最近最多訪问的顺序排列Entry对象,这能够用来做缓存。
private final LinkedHashMap<K, V> map;
/** Size of this cache in units. Not necessarily the number of elements. */
private int size;//当前大小
private int maxSize;//最大容量
private int putCount;//put次数
private int createCount;//create次数
private int evictionCount;//回收次数
private int hitCount;//命中次数
private int missCount;//丢失次数
LinkedHashMap的初始化放在构造器中:
public LruCache(int maxSize) {
if (maxSize <= 0) {
throw new IllegalArgumentException("maxSize <= 0");
}
this.maxSize = maxSize;
this.map = new LinkedHashMap<K, V>(0, 0.75f, true);
}
首先是put方法:
public final V put(K key, V value) {
if (key == null || value == null) {//键值不同意为空
throw new NullPointerException("key == null || value == null");
}
V previous;
synchronized (this) {//线程安全
putCount++;
size += safeSizeOf(key, value);
previous = map.put(key, value);
if (previous != null) {//之前已经插入过同样的key
size -= safeSizeOf(key, previous);//那么减去该entry的容量,由于发生覆盖
}
}
if (previous != null) {
entryRemoved(false, key, previous, value);//这种方法默认空实现
}
trimToSize(maxSize);//若容量超过maxsize,将会删除近期非常少訪问的entry
return previous;
}
除此之外,put操作被加锁了,所以是线程安全的!
public void trimToSize(int maxSize) {
while (true) {//不断删除linkedHashMap头部entry,也就是近期最少訪问的条目。直到size小于最大容量
K key;
V value;
synchronized (this) {//线程安全
if (size < 0 || (map.isEmpty() && size != 0)) {
throw new IllegalStateException(getClass().getName()
+ ".sizeOf() is reporting inconsistent results!");
}
if (size <= maxSize || map.isEmpty()) {//直到容量小于最大容量为止
break;
}
Map.Entry<K, V> toEvict = map.entrySet().iterator().next();//指向链表头
key = toEvict.getKey();
value = toEvict.getValue();
map.remove(key);//删除最少訪问的entry
size -= safeSizeOf(key, value);
evictionCount++;
}
entryRemoved(true, key, value, null);
}
}
private void trimToSize(int maxSize) {
while (true) {
K key;
V value;
synchronized (this) {
if (size < 0 || (map.isEmpty() && size != 0)) {
throw new IllegalStateException(getClass().getName()
+ ".sizeOf() is reporting inconsistent results!");
}
if (size <= maxSize) {
break;
}
Map.Entry<K, V> toEvict = null;
for (Map.Entry<K, V> entry : map.entrySet()) {
toEvict = entry;
}
if (toEvict == null) {
break;
}
key = toEvict.getKey();
value = toEvict.getValue();
map.remove(key);
size -= safeSizeOf(key, value);
evictionCount++;
}
entryRemoved(true, key, value, null);
}
}
public final V get(K key) {
if (key == null) {//不同意空键
throw new NullPointerException("key == null");
}
V mapValue;
synchronized (this) {//线程安全
mapValue = map.get(key);//调用LinkedHashMap的get方法
if (mapValue != null) {
hitCount++;//命中次数加1
return mapValue;//返回value
}
missCount++;//未命中
}
V createdValue = create(key);//默认返回为false
if (createdValue == null) {
return null;
}
synchronized (this) {
createCount++;//假设创建成功,那么create次数加1
mapValue = map.put(key, createdValue);//放到哈希表中
if (mapValue != null) {
// There was a conflict so undo that last put
map.put(key, mapValue);
} else {
size += safeSizeOf(key, createdValue);
}
}
if (mapValue != null) {
entryRemoved(false, key, createdValue, mapValue);
return mapValue;
} else {
trimToSize(maxSize);
return createdValue;
}
}
public final V remove(K key) {
if (key == null) {
throw new NullPointerException("key == null");
}
V previous;
synchronized (this) {
previous = map.remove(key);//调用LinkedHashMap的remove方法
if (previous != null) {
size -= safeSizeOf(key, previous);
}
}
if (previous != null) {
entryRemoved(false, key, previous, null);
}
return previous;//返回value
}
sizeof:这种方法用于计算每一个条目的大小,子类必须得复写这个类。
protected int sizeOf(K key, V value) {//用于计算每一个条目的大小
return 1;
}
snapshot方法,返回当前缓存中全部的条目集合
public synchronized final Map<K, V> snapshot() {
return new LinkedHashMap<K, V>(map);
}
【源代码】LruCache源代码剖析的更多相关文章
- android之LruCache源代码解析
移动设备开发中,因为移动设备(手机等)的内存有限,所以使用有效的缓存技术是必要的.android提供来一个缓存工具类LruCache,开发中我们会经经常使用到,以下来他是怎样实现的. 在package ...
- 图像库---Image Datasets---OpenSift源代码---openSurf源代码
1.Computer Vision Datasets on the web http://www.cvpapers.com/datasets.html 2.Dataset Reference http ...
- MINA2 源代码学习--源代码结构梳理
一.mina总体框架与案例: 1.总体结构图: 简述:以上是一张来自网上比較经典的图,总体上揭示了mina的结构,当中IoService包括clientIoConnector和服务端IoAccepto ...
- 【Java集合源代码剖析】TreeMap源代码剖析
转载请注明出处:http://blog.csdn.net/ns_code/article/details/36421085 前言 本文不打算延续前几篇的风格(对全部的源代码加入凝视),由于要理解透Tr ...
- Java To CSharp源代码转换
前言 开发环境 客户端:Unity3D开发(C#) 服务器:Java (基于Java7) 日 期:2016年09月 需求说明 部分服务器的部分逻辑功能在客户端实现一遍,可以简单的理解为服务器的部分 ...
- Apple II DOS 源代码发布
加州山景城的计算机历史博物馆不仅仅展示硬件,还展示软件.博物馆此前已发布了著名软件MacPaint .Photoshop和APL的源代码,现在它公开了1978年的Apple II DOS源代码.源代码 ...
- 在Android中调用C#写的WebService(附源代码)
由于项目中要使用Android调用C#写的WebService,于是便有了这篇文章.在学习的过程中,发现在C#中直接调用WebService方便得多,直接添加一个引用,便可以直接使用将WebServi ...
- 1、android源代码下载与跟踪
学习Android源代码的目的 理解Android API查找API(Activity.Content Provider等) 高级应用开发(ROM定制) 在不同平台下载Android源代码 W ...
- Ubuntu10.04下载并编译Android4.3源代码
注:转载或引用请标明出处 http://blog.csdn.net/luzhenrong45/article/details/9719433 去年用Ubuntu10.10成功下载并编译Andro ...
随机推荐
- PAT Basic 1049
1049 数列的片段和 给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段.例如,给定数列 { 0.1, 0.2, 0.3, 0.4 },我们有 (0.1) (0.1, 0.2) (0.1 ...
- 【02】markdown工具推荐
[02]信息 Windows 平台 MarkdownPad MarkPad Linux 平台 ReText Mac 平台 Mou 最新版Mac OS下Mou已经无法使用了.这里推荐一个跨平台的编辑器 ...
- [uiautomator篇] bluetooth---接口来做
package com.softwinner.performance.frameratetest; import android.Manifest; import android.bluetooth. ...
- 2011 Michigan Invitational Programming Contest
Crossings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463 Description ...
- 雅图CAD
今天培训了雅图CAD. 由辅助线确定下一步的位置,是个好思想.
- BZOJ 3143 [Hnoi2013]游走 ——概率DP
概率DP+高斯消元 与博物馆一题不同的是,最终的状态是有一定的概率到达的,但是由于不能从最终状态中出来,所以最后要把最终状态的概率置为0. 一条边$(x,y)$经过的概率是x点的概率$*x$到$y$的 ...
- 刷题总结——作诗(bzoj2821)
题目: Description 神犇SJY虐完HEOI之后给傻×LYD出了一题:SHY是T国的公主,平时的一大爱好是作诗.由于时间紧迫,SHY作完诗 之后还要虐OI,于是SHY找来一篇长度为N的文章, ...
- ftp链接、上传、下载、断开
开发环境:Jdk 1.8 引入第三方库:commons-net-2.2.jar(针对第一种方法) 一.基于第三方库FtpClient的FTP服务器数据传输 由于是基于第三方库,所以这里基本上没有太多要 ...
- 插头DP--URAL1519Formula 1
去年的老朋友.挺怀念的,回来看看. $n \leq 12,m \leq 12$,$n*m$的01矩形,问在0中走路的欧拉回路数.答案保证longlong范围. 先设计插头:左右括号和空插头:然后分3* ...
- 安装sqlServer2012失败补救
今天拿着新电脑win10,装数据库2012,装了第一次,没装上,有一半的工具都失败了,慌了.. 连management studio都没装上,我用navicat也连不上. 卸了,第二次安装,装一半卡住 ...