Reader or writer locks
A readers–writer(RW) or shared-exclusivelock is a primitive synchronization that allows concurrent access for read-only operations, as well as  exclusive write operations. Multiple threads can read the data concurrently, but for writing or modifying the data, an exclusive lock is needed.
A writer has exclusive access for writing data. Till the current writer is done, other writers and readers will be blocked. There are many cases where data is read more often than it is written.
The following code shows how we use the locks to provide concurrent access to a Java Map<K, V>
. The code synchronizes the internal map, using an RW lock:
public class RWMap<K, V> { private final Map<K, V> map; private final ReadWriteLock lock = new ReadWriteLock(); private final RWLock r = lock.getRdLock(); private final RWLock w = lock.getWrLock(); public RWMap(Map<K, V> map) { this.map = map; } public V put(K key, V value) throws InterruptedException { w.lock(); try...