How to do it...

Follow these steps to implement the example:

  1. Create a class named ParkingCounter and specify that it extends the AtomicInteger class:
        public class ParkingCounter extends AtomicInteger {
  1. Declare a private int attribute named maxNumber to store the maximum number of cars admitted into the parking lot:
        private final int maxNumber;
  1. Implement the constructor of the class to initialize its attributes:
        public ParkingCounter(int maxNumber){           set(0);           this.maxNumber=maxNumber;         }
  1. Implement the carIn() method. This method increments the counter of cars if it has a value smaller than the established maximum value. Construct an infinite loop and get the value of the internal counter using the get() method:
 public boolean ...

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.