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 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(); ...

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