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