用读写锁包装Map

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 get(Object key){
  r.lock();
  try{
   return map.get(key);
  }finally{
   r.unlock();
  }
 }
}

你可能感兴趣的:(map)