A reader-friendly RW lock

RW locks can give priority to readers (read-friendly) or writers (write-friendly). We will see how concurrency and starvation aspects come up during the design.

The following diagram shows the ReadWriteLock class:

The readers field is an int—it represents the number of readers at any given instance of time. On the other hand, the writer field is a boolean—there can be only one writer, or there can be multiple readers—so a boolean suffices. We synchronize on an external lock, represented by the lck field.

The following diagram shows the design of the reader and writer locks, RdLock and WrLock. These are the inner classes ...

Get Concurrent Patterns and Best Practices now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.