n*******1 发帖数: 145 | 5 public class ThreadSafeHashMap extends HashMap {
private final ReentrantReadWriteLock readWriteLock = new
ReentrantReadWriteLock();
private final Lock read = readWriteLock.readLock();
private final Lock write = readWriteLock.writeLock();
public ThreadSafeHashMap(){
super();
}
@Override
public V put(K key, V value) {
write.lock();
try {
super.put(key,value);
} finally {
write.unlock();
}
return value;
}
@Override
public V get(Object key) {
read.lock();
try {
return super.get(key);
} finally {
read.unlock();
}
}
} |