StampedLock
This lesson introduces a new kind of Lock class introduced in Java 8.
The ReentrantReadWriteLoc
k and its drawbacks
Before Java 8 we had a ReentrantReadWriteLock
class that was used for reading and writing data in a thread-safe manner.
Here are a few of the important points about ReentrantReadWriteLock
:
- Multiple threads can acquire a read lock simultaneously.
- Only one thread can acquire a write lock.
- If a thread wants to acquire a write lock and there are some threads that have read lock, the thread will wait until all the threads release the read lock.
There are a few problems with using the ReentrantReadWriteLock
class:
- It can lead to starvation.
- Sometimes it can be significantly slower than other synchronizers.
The improvements provided by StampedLock
To overcome these disadvantages, StampedLock
is added. Apart from providing separate read and write locks, also has a feature for optimistic locking for ...