There's more...

The StampedLock class has other methods that you should know:

  • tryReadLock() and tryReadLock(long time, TimeUnit unit): These methods try to acquire the lock in read mode. If they can't, the first version is returned immediately and the second one waits for the amount of time specified in the parameter. These methods also return a stamp that must be checked (stamp != 0).
  • tryWriteLock() and tryWriteLock(long time, TimeUnit unit): These methods try to acquire the lock in write mode. If they can't, the first version is returned immediately and the second one waits for the amount of time specified in the parameter. These methods also return a stamp that must be checked (stamp != 0).
  • isReadLocked() and isWriteLocked(): These methods ...

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.