Advanced locking with the StampedLock class
The StampedLock
class provides a special kind of lock that is different from the ones provided by the Lock or ReadWriteLock
interfaces. In fact, this class doesn't implement these interfaces, but the functionality it provides is very similar.
The first point to note about this kind of lock is that its main purpose is to be a helper class to implement thread-safe components, so its use will not be very common in normal applications.
The most important features of StampedLock
locks are as follows:
- You can obtain control of the lock in three different modes:
- Write: In this mode, you get exclusive access to the lock. No other thread can have control of the lock in this mode.
- Read: In this mode, you have non-exclusive access to the lock. There can be other threads that have access to the lock in this mode or the Optimistic Read mode.
- Optimistic Read: Here, the thread doesn't have control over the block. Other threads can get control of the lock in write mode...