O'Reilly logo

Java 9 Concurrency Cookbook - Second Edition by Javier Fernández González

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

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

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