Okay, I've been trying to work on this for a while now and am finally going to shout out for help. I'm sorry for having to post so much coding but I can't seem to figure out how to add a "Print handling" within the code. Basically the code is supposed to:
4. The fifth step is the action of your adding a Printer Handler that utilizes three (3) threads with a sleep interval of 1,000 milliseconds.
5. The write-up of the application depicted on Page 461, simulates order generation using the OrderTaker class; orders are passed to the OrderQueue class for processing and to the OrderHandler class for display. We will add a final simulation of a printing process, as shown.
a. In the OrderQueue class, add another LinkedList to save orders for the printing.
b. Create a PrinterHandler class to simulate and display the printing process, as shown in Exhibit C.
But I just can't get it to pull the orders correctly (I know I am doing it wrong within my code). It shows in Exhibit C that in the column under "Printer" all it will show is "Printing # " then the thread number it pulled and is fake "printing"
public class PrinterHandler extends Thread { private OrderQueue orderQueue; public PrinterHandler(OrderQueue orderQueue) { this.orderQueue = orderQueue; } public void run() { Order order; while (true) { order = orderQueue.pullOrder(); System.out.println( " HERE! " + this.getName()); <--I did this to see where my "HERE!" wouldprint try { Thread.sleep(1000); } // delay one second catch (InterruptedException e) { } // ignore interruptions } // end while } // end run method }public class Order { private int number; public Order(int number) { this.number = number; } public String toString() { return " Order # " + number; } } // end Order classpublic class OrderHandler extends Thread { private OrderQueue orderQueue; public OrderHandler(OrderQueue orderQueue) { this.orderQueue = orderQueue; } public void run() { Order order; while (true) { order = orderQueue.pullOrder(); // get next available order System.out.println("\t\t\t " + order.toString() + " " + this.getName()); try { Thread.sleep(2000); } // delay two seconds catch (InterruptedException e) {} // ignore interruptions } // end while } // end run method } // end OrderHandler classimport java.util.LinkedList; public class OrderQueue { private LinkedList<Order> orderQueue = new LinkedList<Order>(); private LinkedList<Order> printerQueue = new LinkedList<Order>(); public OrderQueue() { System.out.println(" 1. \"" + Thread.currentThread().getName() + "\" is instantiating the order queue"); } // end constructor public synchronized void pushOrder(Order order) { orderQueue.addLast(order); notifyAll(); // notify any waiting threads that an order has been added } // end pushOrder() method public synchronized Order pullOrder() { while (orderQueue.size() == 0) { // if there are no orders in the queue, wait try { System.out.println(Thread.currentThread().getName() + " is waiting"); wait(); } // end try catch (InterruptedException e) {} } // end while return orderQueue.removeFirst(); } // end pullOrder( ) method } // end OrderQueue classpublic class OrderQueueApp { public static void main(String[] args) { System.out.println(); System.out.println(" Thread Activities/Overview\n"); final int TAKER_COUNT = 3; // number of OrderTaker threads final int ORDER_COUNT = 3; // number of orders per OrderTaker thread final int HANDLER_COUNT = 3; // number of OrderHandler threads final int PRINTER_COUNT = 3; OrderQueue queue = new OrderQueue(); // create the order queue System.out.println(" 2. Starting the order queue."); System.out.println(" 3. Starting " + TAKER_COUNT + " order taker threads, " + "each producing " + ORDER_COUNT + " orders."); for (int i = 0; i < TAKER_COUNT; i++) // create the OrderTaker threads { OrderTaker t = new OrderTaker(ORDER_COUNT, queue); t.start(); } System.out.println(" 4. Starting " + HANDLER_COUNT + " order handler threads."); System.out.println(" 5. Starting " + PRINTER_COUNT + " printer handler threads.\n"); String s = " Order Taker\tThread\t Handler Thread Printer Thread\n" + " ===========\t========= =========== ========== =========== ========"; System.out.println(s); for (int i = 0; i < HANDLER_COUNT; i++) { // create the OrderHandler threads OrderHandler h = new OrderHandler(queue); h.start(); } // end for for (int i = 0; i < PRINTER_COUNT; i++) { PrinterHandler p = new PrinterHandler(queue); p.start(); } } // end main } // end OrderQueueApp classpublic class OrderTaker extends Thread { private static int orderNumber = 1; private int count = 0; private int maxOrders; private OrderQueue orderQueue; private String name; public OrderTaker(int orderCount, OrderQueue orderQueue) { this.maxOrders = orderCount; // number of orders this.orderQueue = orderQueue; // order queue } // end constructor public void run() { int orderNumber; Order order; while (count < maxOrders) { order = new Order(getOrderNumber()); orderQueue.pushOrder(order); // add order to the queue System.out.println(order.toString() + " " + this.getName()); count++; try { Thread.sleep(1000); } // delay one second catch (InterruptedException e) { } // ignore interruptions } // end while } // end run method private synchronized int getOrderNumber() { return orderNumber++; } } // end OrderTaker class