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 ...

Get Java 9 Concurrency Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.