Follow these steps to implement the example:
- Create a class named PricesInfo that stores information about the prices of two products:
public class PricesInfo {
- Declare two double attributes named price1 and price2:
private double price1; private double price2;
- Declare a ReadWriteLock object called lock:
private ReadWriteLock lock;
- 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(); }
- 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: ...