ReadWriteMap.java
import java.util.*;
import java.util.concurrent.locks.*;
/**
* ReadWriteMap
* <p/>
* Wrapping a Map with a read-write lock
*
* @author Brian Goetz and Tim Peierls
*/
public class ReadWriteMap <K,V> {
private final Map<K, V> map;
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private final Lock r = lock.readLock();
private final Lock w = lock.writeLock();
public ReadWriteMap(Map<K, V> map) {
this.map = map;
}
public V put(K key, V value) {
w.lock();
try {
return map.put(key, value);
} finally {
w.unlock();
}
}
public V remove(Object key) {
w.lock();
try {
return map.remove(key);
} finally {
w.unlock();
}
}
public void putAll(Map<? extends K, ? extends V> m) {
w.lock();
try {
map.putAll(m);
} finally {
w.unlock();
}
}
public void clear() {
w.lock();
try {
map.clear();
} finally {
w.unlock();
}
}
public V get(Object key) {
r.lock();
try {
return map.get(key);
} finally {
r.unlock();
}
}
public int size() {
r.lock();
try {
return map.size();
} finally {
r.unlock();
}
}
public boolean isEmpty() {
r.lock();
try {
return map.isEmpty();
} finally {
r.unlock();
}
}
public boolean containsKey(Object key) {
r.lock();
try {
return map.containsKey(key);
} finally {
r.unlock();
}
}
public boolean containsValue(Object value) {
r.lock();
try {
return map.containsValue(value);
} finally {
r.unlock();
}
}
}
This program showcases the use of a read-write lock to enhance concurrent access to a Map
, as discussed in Chapter 13 of Java Concurrency in Practice. The ReadWriteMap
wrapper class employs a ReentrantReadWriteLock
to distinguish between read operations, which can safely run concurrently, and write operations, which require exclusive access. Methods like get()
and containsKey()
acquire the shared read lock, while mutating operations like put()
and clear()
acquire the exclusive write lock. This design significantly improves performance in read-heavy scenarios by reducing contention compared to using a single mutual exclusion lock, while still preserving thread safety.
No comments:
Post a Comment