Skip to content

Commit 84ee4b0

Browse files
authored
Merge pull request #143 from jobschen/LazyCache
LazyCache - 锁机制调整
2 parents bcfb778 + 875a121 commit 84ee4b0

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

scheduler/src/main/java/com/zfoo/scheduler/util/LazyCache.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.util.concurrent.ConcurrentHashMap;
99
import java.util.concurrent.ConcurrentMap;
1010
import java.util.concurrent.atomic.AtomicLong;
11-
import java.util.concurrent.locks.ReentrantReadWriteLock;
11+
import java.util.concurrent.locks.ReentrantLock;
1212
import java.util.function.BiConsumer;
1313

1414
/**
@@ -62,7 +62,7 @@ public static enum RemovalCause {
6262
private ConcurrentMap<K, Cache<K, V>> cacheMap;
6363
private BiConsumer<List<Cache<K, V>>, RemovalCause> removeListener = (removes, removalCause) -> {
6464
};
65-
private final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
65+
private final ReentrantLock lock = new ReentrantLock();
6666

6767
public LazyCache(int maximumSize, long expireAfterAccessMillis, long expireCheckIntervalMillis, BiConsumer<List<Cache<K, V>>, RemovalCause> removeListener) {
6868
AssertionUtils.ge1(maximumSize);
@@ -157,7 +157,7 @@ private void removeForCause(List<Cache<K, V>> list, RemovalCause removalCause) {
157157
}
158158

159159
private void checkMaximumSize() {
160-
if (rwLock.writeLock().tryLock()) { // 获取写锁
160+
if (lock.tryLock()) { // 获取写锁
161161
try {
162162
if (cacheMap.size() > backPressureSize) {
163163
var removeList = cacheMap.values()
@@ -168,7 +168,7 @@ private void checkMaximumSize() {
168168
removeForCause(removeList, RemovalCause.SIZE);
169169
}
170170
} finally {
171-
rwLock.writeLock().unlock();
171+
lock.unlock();
172172
}
173173
}
174174
}

0 commit comments

Comments
 (0)