How it works...

In the following screenshot, you can see a part of the output of one execution of this example:

While the writer has acquired the write lock, none of the reader tasks can read the data. You can see some messages of the reader tasks after the Write Lock Acquired message, but they are instructions that were executed before and not shown yet in the console. Once the writer task has released the lock, reader tasks gain access to the prices information again and show the new prices.

As mentioned previously, the ReentrantReadWriteLock class has two locks: one for read operations and one for write operations. The lock used in read ...

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.