ReadWriteLock

This lesson examines the ReadWriteLock interface and the implementing class ReentrantReadWriteLock. The lock is intended to allow multiple readers to read at a time but only allow a single writer to write.

If you are interviewing, consider buying our number#1 course for Java Multithreading Interviews.

The ReadWriteLock interface is part of Java’s java.util.concurrent.locks package. The only implementing class for the interface is ReentrantReadWriteLock. The ReentrantReadWriteLock can be locked by multiple readers at the same time while writer threads have to wait. Conversely, the ReentrantReadWriteLock can be locked by a single writer thread at a time and other writer or reader threads have to wait for the lock to be free.

ReentrantReadWriteLock

The ReentrantReadWriteLock as the name implies allows threads to recursively acquire the lock. Internally, there are two locks to guard for read and write accesses. ReentrantReadWriteLock can help improve concurrency over using a mutual exclusion lock as it allows multiple reader threads to read concurrently. However, whether an application will truly realize ...