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();
}
  1. Implement the printJob() method that will simulate the printing of a document. It receives an object called document as a parameter:
        public void printJob (Object document){
  1. First of all, the printJob() method calls the acquire() method to acquire access to the semaphore. As this method can throw an InterruptedException exception, you must include the code to process it:
        try { 
semaphore.acquire();
  1. Then, get the number of the printers assigned to print this job, using the private method getPrinter():
        int assignedPrinter=getPrinter();
  1. Then, implement the lines that simulate the printing of a document waiting for a random period of time:
        long duration=(long)(Math.random()*10); 
System.out.printf("%s - %s: PrintQueue: Printing a Job in
Printer %d during %d seconds ",
new Date(), Thread.currentThread().getName(),
assignedPrinter,duration);
TimeUnit.SECONDS.sleep(duration);
  1. Finally, release the semaphore by calling the release() method and marking the printer used as free, and assign true to the corresponding index in the freePrinters array:
          freePrinters[assignedPrinter]=true; 
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
semaphore.release();
}
  1. Next, implement the getPrinter() method. It's a private method that returns an int value and has no parameters:
        private int getPrinter() {
  1. First, declare an int variable to store the index of the printer:
        int ret=-1;
  1. Then, get access to the lockPrinters object:
        try { 
lockPrinters.lock();
  1. Post this, find the first true value in the freePrinters array and save its index in a variable. Modify this value to false because this printer will be busy:
        for (int i=0; i<freePrinters.length; i++) { 
if (freePrinters[i]){
ret=i;
freePrinters[i]=false;
break;
}
}
  1. Finally, free the lockPrinters object and return the index of the true value:
        } catch (Exception e) { 
e.printStackTrace();
} finally {
lockPrinters.unlock();
}
return ret;
  1. Next, create a class called Job and specify that it implements the Runnable interface. This class implements the job that will send a document to the printer:
        public class Job implements Runnable {
  1. Declare a PrintQueue object. Call it printQueue:
        private PrintQueue printQueue;
  1. Implement the constructor of the class. It initializes the PrintQueue object declared in the class:
        public Job(PrintQueue printQueue){ 
this.printQueue=printQueue;
}
  1. Implement the run() method:
        @Override 
public void run() {
  1. First, this method writes a message to the console that shows that the job has started its execution:
        System.out.printf("%s: Going to print a job
",
Thread.currentThread().getName());
  1. Then, it calls the printJob() method of the PrintQueue object:
        printQueue.printJob(new Object());
  1. Finally, the method writes a message to the console that shows that it has finished its execution:
          System.out.printf("%s: The document has been printed
",
Thread.currentThread().getName());
}
  1. Next, implement the main class of the example by creating a class named Main and implementing the main() method:
        public class Main { 
public static void main (String args[]){
  1. Create a PrintQueue object named printQueue:
        PrintQueue printQueue=new PrintQueue();
  1. Create 12 threads. Each one of these threads will execute a Job object that will send a document to the print queue:
        Thread[] threads=new Thread[12]; 
for (int i=0; I < threads.length i++){
thread[i]=new Thread(new Job(printQueue),"Thread"+i);
}
  1. Finally, start the 12 threads:
        for (int i=0; I < threads.length; i++){ 
thread[i].start();
}
..................Content has been hidden....................

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