Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
沉迷打码的小凳子
glide
提交
31640f60
G
glide
项目概览
沉迷打码的小凳子
/
glide
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
glide
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
31640f60
编写于
4月 26, 2015
作者:
S
Sam Judd
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Ensure data is available when disk cache put completes.
Fixes #429.
上级
31fc07fd
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
95 addition
and
0 deletion
+95
-0
library/src/main/java/com/bumptech/glide/load/engine/cache/DiskCacheWriteLocker.java
...umptech/glide/load/engine/cache/DiskCacheWriteLocker.java
+91
-0
library/src/main/java/com/bumptech/glide/load/engine/cache/DiskLruCacheWrapper.java
...bumptech/glide/load/engine/cache/DiskLruCacheWrapper.java
+4
-0
未找到文件。
library/src/main/java/com/bumptech/glide/load/engine/cache/DiskCacheWriteLocker.java
0 → 100644
浏览文件 @
31640f60
package
com.bumptech.glide.load.engine.cache
;
import
com.bumptech.glide.load.Key
;
import
java.util.ArrayDeque
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Queue
;
import
java.util.concurrent.locks.Lock
;
import
java.util.concurrent.locks.ReentrantLock
;
/**
* Keeps a map of keys to locks that allows locks to be removed from the map when no longer in use
* so the size of the collection is bounded.
*
* <p> This class will be accessed by multiple threads in a thread pool and ensures that the
* number of threads interested in each lock is updated atomically so that when the count reaches
* 0, the lock can safely be removed from the map. </p>
*/
final
class
DiskCacheWriteLocker
{
private
final
Map
<
Key
,
WriteLock
>
locks
=
new
HashMap
<
Key
,
WriteLock
>();
private
final
WriteLockPool
writeLockPool
=
new
WriteLockPool
();
void
acquire
(
Key
key
)
{
WriteLock
writeLock
;
synchronized
(
this
)
{
writeLock
=
locks
.
get
(
key
);
if
(
writeLock
==
null
)
{
writeLock
=
writeLockPool
.
obtain
();
locks
.
put
(
key
,
writeLock
);
}
writeLock
.
interestedThreads
++;
}
writeLock
.
lock
.
lock
();
}
void
release
(
Key
key
)
{
WriteLock
writeLock
;
synchronized
(
this
)
{
writeLock
=
locks
.
get
(
key
);
if
(
writeLock
==
null
||
writeLock
.
interestedThreads
<=
0
)
{
throw
new
IllegalArgumentException
(
"Cannot release a lock that is not held"
+
", key: "
+
key
+
", interestedThreads: "
+
(
writeLock
==
null
?
0
:
writeLock
.
interestedThreads
));
}
if
(--
writeLock
.
interestedThreads
==
0
)
{
WriteLock
removed
=
locks
.
remove
(
key
);
if
(!
removed
.
equals
(
writeLock
))
{
throw
new
IllegalStateException
(
"Removed the wrong lock"
+
", expected to remove: "
+
writeLock
+
", but actually removed: "
+
removed
+
", key: "
+
key
);
}
writeLockPool
.
offer
(
removed
);
}
}
writeLock
.
lock
.
unlock
();
}
private
static
class
WriteLock
{
final
Lock
lock
=
new
ReentrantLock
();
int
interestedThreads
;
}
private
static
class
WriteLockPool
{
private
static
final
int
MAX_POOL_SIZE
=
10
;
private
final
Queue
<
WriteLock
>
pool
=
new
ArrayDeque
<
WriteLock
>();
WriteLock
obtain
()
{
WriteLock
result
;
synchronized
(
pool
)
{
result
=
pool
.
poll
();
}
if
(
result
==
null
)
{
result
=
new
WriteLock
();
}
return
result
;
}
void
offer
(
WriteLock
writeLock
)
{
synchronized
(
pool
)
{
if
(
pool
.
size
()
<
MAX_POOL_SIZE
)
{
pool
.
offer
(
writeLock
);
}
}
}
}
}
library/src/main/java/com/bumptech/glide/load/engine/cache/DiskLruCacheWrapper.java
浏览文件 @
31640f60
...
...
@@ -25,6 +25,7 @@ public class DiskLruCacheWrapper implements DiskCache {
private
static
final
int
VALUE_COUNT
=
1
;
private
static
DiskLruCacheWrapper
wrapper
=
null
;
private
final
DiskCacheWriteLocker
writeLocker
=
new
DiskCacheWriteLocker
();
private
final
SafeKeyGenerator
safeKeyGenerator
;
private
final
File
directory
;
private
final
int
maxSize
;
...
...
@@ -87,6 +88,7 @@ public class DiskLruCacheWrapper implements DiskCache {
@Override
public
void
put
(
Key
key
,
Writer
writer
)
{
String
safeKey
=
safeKeyGenerator
.
getSafeKey
(
key
);
writeLocker
.
acquire
(
key
);
try
{
DiskLruCache
.
Editor
editor
=
getDiskCache
().
edit
(
safeKey
);
// Editor will be null if there are two concurrent puts. In the worst case we will just silently fail.
...
...
@@ -104,6 +106,8 @@ public class DiskLruCacheWrapper implements DiskCache {
if
(
Log
.
isLoggable
(
TAG
,
Log
.
WARN
))
{
Log
.
w
(
TAG
,
"Unable to put to disk cache"
,
e
);
}
}
finally
{
writeLocker
.
release
(
key
);
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录