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

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