How to do it...

Follow these steps to implement the example:

  1. First, create the application without using any synchronization mechanism. Create a class named ParkingCash with an internal constant and an attribute to store the total amount of money earned by providing this parking service:
          public class ParkingCash {           private static final int cost=2;           private long cash;            public ParkingCash() {             cash=0;           }
  1. Implement a method named vehiclePay() that will be called when a vehicle (a car or motorcycle) leaves the parking area. It will increase the cash attribute:
        public void vehiclePay() {           cash+=cost;         }
  1. Finally, implement a method named close() that will write the value of the cash attribute in the console and reinitialize it to zero: ...

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.