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