How to do it...

Follow these steps to implement the example:

  1. Create a class named PrintQueue that will implement the print queue:
        public class PrintQueue {
  1. This class will have three private attributes. A semaphore named semaphore, an array of Booleans named freePrinters, and a lock named lockPrinters, as shown in the following code snippet:
        private final Semaphore semaphore;         private final boolean freePrinters[];         private final Lock lockPrinters;
  1. Implement the constructor of the class. It initializes the three attributes of the class, as shown in the following code snippet:
        public PrintQueue(){           semaphore=new Semaphore(3);           freePrinters=new boolean[3];           for (int i=0; i<3; i++){             freePrinters[i]=true;           }  lockPrinters=new ReentrantLock(); ...

Get Java 9 Concurrency Cookbook - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.