Locks In Java — Part 6.3 [ Stamped Lock Methods , Read View and Write View ]

Avinashsoni
2 min readMay 26, 2024

--

In This Article we are going to learn about the different methods under stamped lock in java and then will look into the views concepted provided by it.

lets goo!!

the methods are pretty straightforward to understand and they keep doing some checks on the state values to determine if true or false.

lets move to read view , write view and also see the asReadLock() and asWriteLock() method.

Read View :

it is a internal class which implements the lock interface

and works as a wrapper for the methods existing in the Stamped lock

so it has methods like .

  1. lock() -> internally it calls the readLock() method
  2. lockInterruptibly() -> internally it calls the readLockInterruptiblyMethod()
  3. tryLock() -> it checks if tryReadLock() method output != 0
  4. tryLock () -> timed lock which internally checks if tryReadLock timed lock !=O
  5. unlock() -> calls the unstampedUnlockRead() method [ unstamped methods are created for view classes and doesnt need stamp as view classes lock methods throw away the stamps ]
  6. newCondition () -> throw an exception

[ please check the lock interface to get better insights about the above 6 methods ]

asReadLock()

it creates the Read Lock view of the stamped lock

similarly we have the writeLock View which again implements the Lock interfaces and uses the corresponding write lock methods internally to call the lock interface

these two views help us to use the stamped lock under the lock interface

there is ReadWriteView as well which uses the asReadLock () and asWriteLock() methods and implements the ReadWriteLock Interface.

so this completes our java lock series in which we tried to cover most of the locks in java.

here is the complete series :

  1. Lock Interface : https://medium.com/@avinashsoni9829/locks-in-java-part-1-lock-interface-38222d6048d3
  2. Read Write Lock : https://medium.com/@avinashsoni9829/locks-in-java-part-2-read-write-locks-0a2a7c803b44
  3. Lock Support : https://medium.com/@avinashsoni9829/locks-in-java-part-3-locksupport-607d8766ed1a
  4. Abstract Ownable Synchronizers : https://medium.com/@avinashsoni9829/locks-in-java-part-4-abstract-ownable-synchronizers-1e4033f66d17
  5. Reentrant Lock part — 1: https://medium.com/@avinashsoni9829/locks-in-java-part-5-1-reentrant-locks-in-java-sync-class-bf11e8765507
  6. Reentrant Lock part — 2 : https://medium.com/@avinashsoni9829/locks-in-java-part-5-2-reentrant-locks-051e31e19a48
  7. Stamped Lock Part 1 : https://medium.com/@avinashsoni9829/locks-in-java-part-6-1-stamped-lock-optimistic-reads-and-pessimistic-reads-8cb8f9858674
  8. Stamped Lock Part 2 : https://medium.com/@avinashsoni9829/locks-in-java-part-6-2-stamped-lock-conversions-b2ec88e78ad1
  9. Stamped Lock Part 3 and Series End : https://medium.com/@avinashsoni9829/locks-in-java-part-6-3-stamped-lock-methods-read-view-and-write-view-333579a644f4

code : https://github.com/avinashsoni9829/Threading/tree/main/locks

thanks for reading ! do share ur feedback

--

--

Avinashsoni

SDE@BNY MELLON | Spring Boot | Learning and Growing EveryDay 😁 Linkedln 👇: https://www.linkedin.com/in/asoni93/