How to do it...

Follow these steps to implement the example:

  1. Create a class named PricesInfo that stores information about the prices of two products:
        public class PricesInfo {
  1. Declare two double attributes named price1 and price2:
        private double price1;         private double price2;
  1. Declare a ReadWriteLock object called lock:
        private ReadWriteLock lock;
  1. Implement the constructor of the class that initializes the three attributes. For the lock attribute, create a new ReentrantReadWriteLock object:
        public PricesInfo(){           price1=1.0;           price2=2.0;           lock=new ReentrantReadWriteLock();         }
  1. Implement the getPrice1() method that returns the value of the price1 attribute. It uses the read lock to control access to the value of this attribute: ...

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.