com.absmartly.sdk.internal.Concurrency Maven / Gradle / Ivy
package com.absmartly.sdk.internal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java8.util.function.Function;
public class Concurrency {
static public V computeIfAbsentRW(ReentrantReadWriteLock lock, Map map, K key,
Function computer) {
final ReentrantReadWriteLock.ReadLock readLock = lock.readLock();
try {
readLock.lock();
final V value = map.get(key);
if (value != null) {
return value;
}
} finally {
readLock.unlock();
}
final ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
try {
writeLock.lock();
final V value = map.get(key); // double check
if (value != null) {
return value;
}
final V newValue = computer.apply(key);
map.put(key, newValue);
return newValue;
} finally {
writeLock.unlock();
}
}
static public V getRW(ReentrantReadWriteLock lock, Map map, K key) {
final ReentrantReadWriteLock.ReadLock readLock = lock.readLock();
try {
readLock.lock();
return map.get(key);
} finally {
readLock.unlock();
}
}
static public V putRW(ReentrantReadWriteLock lock, Map map, K key, V value) {
final ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
try {
writeLock.lock();
return map.put(key, value);
} finally {
writeLock.unlock();
}
}
static public void addRW(ReentrantReadWriteLock lock, List list, V value) {
final ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
try {
writeLock.lock();
list.add(value);
} finally {
writeLock.unlock();
}
}
}