Changed locking positions.

This commit is contained in:
Harrison Deng 2020-04-29 13:05:04 -05:00
parent d62bc696a5
commit a102f53365

View File

@ -30,11 +30,11 @@ public class Cache<K, V> {
* @param value The value to store. * @param value The value to store.
*/ */
public void set(K key, V value) { public void set(K key, V value) {
writeLock.lock(); if (data.containsKey(key)) {
try { data.get(key).value = value;
if (data.containsKey(key)) { } else {
data.get(key).value = value; writeLock.lock();
} else { try {
CacheValue<K, V> val = new CacheValue<>(); CacheValue<K, V> val = new CacheValue<>();
val.key = key; val.key = key;
val.value = value; val.value = value;
@ -43,9 +43,9 @@ public class Cache<K, V> {
if (data.size() > maxSize) { if (data.size() > maxSize) {
data.remove(usage.pop().key); data.remove(usage.pop().key);
} }
} finally {
writeLock.unlock();
} }
} finally {
writeLock.unlock();
} }
} }