O'Reilly logo

Concurrent Patterns and Best Practices by Atul S. Khot

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required