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. Declare a Lock object and initialize it with a new object of the ReentrantLock class in the constructor. The constructor will receive a Boolean parameter we will use to specify the fair mode of the Lock:
        private Lock queueLock;         public PrintQueue(booleanfairMode) {           queueLock = new ReentrantLock(fairMode);         }
  1. Implement the printJob() method. It will receive Object as a parameter and it will not return any value:
        public void printJob(Object document){
  1. Inside the printJob() method, get control of the Lock object by calling the lock() method:
        queueLock.lock();
  1. Then, include ...

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.