Skip to content

Commit

Permalink
Fix bug JakeWharton#81: Infinite loop in trimToSize with multi-threading
Browse files Browse the repository at this point in the history
  • Loading branch information
damienurruty committed May 18, 2015
1 parent 3aa6286 commit 60a213c
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/main/java/com/jakewharton/disklrucache/DiskLruCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -646,9 +646,20 @@ public synchronized void close() throws IOException {

private void trimToSize() throws IOException {
while (size > maxSize) {
Map.Entry<String, Entry> toEvict = lruEntries.entrySet().iterator().next();
remove(toEvict.getKey());
if (!tryRemoveEntryFromOldest()) {
// cannot remove any entry (all are being edited), skip the trim
break;
}
}
}

private boolean tryRemoveEntryFromOldest() throws IOException {
for (Map.Entry<String, Entry> toEvict : lruEntries.entrySet()) {
if (remove(toEvict.getKey())) {
return true;
}
}
return false;
}

/**
Expand Down

0 comments on commit 60a213c

Please sign in to comment.