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 the following code to simulate the process of printing a document:
        try { 
Long duration=(long)(Math.random()*10000);
System.out.println(Thread.currentThread().getName()+ ":
PrintQueue: Printing a Job during "+
(duration/1000)+" seconds");
Thread.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
}
  1. Finally, free the control of the Lock object with the unlock() method:
        finally { 
queueLock.unlock();
}
  1. Then, repeat the same process again. The printJob() method will help you get access to the lock and then free it twice. This strange behavior will allow us to see the difference between fair and non-fair mode in a better way. We include this piece of code in the printJob() method:
          queueLock.lock(); 
try {
Long duration = (long) (Math.random() * 10000);
System.out.printf("%s: PrintQueue: Printing a Job during
%d seconds ", Thread.currentThread()
.getName(),(duration / 1000));
Thread.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
queueLock.unlock();
}
  1. Create a class named Job and specify that it implements the Runnable interface:
        public class Job implements Runnable {
  1. Declare an object of the PrintQueue class and implement the constructor of the class that initializes this object:
        private PrintQueue printQueue; 

public Job(PrintQueue printQueue){
this.printQueue=printQueue;
}
  1. Implement the run() method. It uses the PrintQueue object to send a job to print:
        @Override 
public void run() {
System.out.printf("%s: Going to print a document ",
Thread.currentThread().getName());
printQueue.printJob(new Object());
System.out.printf("%s: The document has been printed ",
Thread.currentThread().getName());
}
  1. Create the main class of the application by implementing a class named Main and adding the main() method to it:
        public class Main { 

public static void main (String args[]){
  1. We are going to test the PrintQueue class using a lock with the fair mode returning both true and false. We will use an auxiliary method to implement both the tests so the code of the main() method is simple:
          System.out.printf("Running example with fair-mode =
false ");
testPrintQueue(false);
System.out.printf("Running example with fair-mode = true ");
testPrintQueue(true);
}
  1. Create the testPrintQueue() method and create a shared PrintQueue object inside it:
        private static void testPrintQueue(Boolean fairMode) { 
PrintQueue printQueue=new PrintQueue(fairMode);
  1. Create 10 Job objects and 10 threads to run them:
        Thread thread[]=new Thread[10]; 
for (int i=0; i<10; i++){
thread[i]=new Thread(new Job(printQueue),"Thread "+ i);
}
  1. Start the 10 threads:
        for (int i=0; i<10; i++){ 
thread[i].start();
}
  1. Lastly, wait for the finalization of the 10 threads:
        for (int i=0; i<10; i++) { 
try {
thread[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.219.130