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

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