Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Thread handling

  1. #1
    Member
    Join Date
    Dec 2010
    Posts
    69
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Thread handling

    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 class
    public 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 class
    import 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 class
    public 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 class
    public 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


  2. #2
    Member
    Join Date
    Dec 2010
    Posts
    69
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Thread handling

    anyone know?

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Thread handling

    hi...were you able to do this program? do you have a solution of this program? It would be greatly appreciated !!
    Thanks!!

  4. #4
    Member
    Join Date
    Dec 2010
    Posts
    69
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Thread handling

    Nope, haven't received any help yet on it and nothing I seem to do with it works so idk...

  5. #5
    Member
    Join Date
    Dec 2010
    Posts
    69
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Thread handling

    Running out of time, can anyone help?

  6. #6
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Thread handling

    That's quite a bit of code for someone to piece together and try to run/debug...you might receive a quicker response by breaking it down to the elements you are having a problem with (eg an SSCCE).

  7. #7
    Member
    Join Date
    Dec 2010
    Posts
    69
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Thread handling

    Well, If I can, I can try to just break it down in words first.

    I need to find a way for my OrderQueue or OrderHandler (unsure on which would work the best) to add the "order" to the "printerQueue" linkedlist, then have the PrinterHandler class pull the order and display the message saying it was pulled & printed.


    Ugh... I can't seem to find a way to break it down without having to paste the code again =/

  8. #8
    Member
    Join Date
    Dec 2010
    Posts
    69
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Thread handling

    Does anyone know how get the PrinterHandler class to pull orders and process them? I can't get anything to work =/

  9. #9
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Thread handling

    hey...I know how to do it..finally I have solved it myself...you still need my help?

Similar Threads

  1. ACTION LISTENER HANDLING
    By fari in forum What's Wrong With My Code?
    Replies: 19
    Last Post: June 12th, 2010, 08:31 PM
  2. Handling two button events?
    By BelowTheHeavens in forum AWT / Java Swing
    Replies: 2
    Last Post: May 15th, 2010, 01:35 AM
  3. Regarding File Handling
    By ravjot28 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 06:45 PM
  4. Event handling
    By subhvi in forum AWT / Java Swing
    Replies: 3
    Last Post: August 26th, 2009, 11:20 AM
  5. Exception handling
    By AnithaBabu1 in forum Exceptions
    Replies: 6
    Last Post: August 27th, 2008, 09:37 AM