CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Threads

Threads
Application
OrderQueue class

The code for the OrderQueue class

import java.util.LinkedList;

public class OrderQueue
{
    private LinkedList orderQueue = new LinkedList();

    public synchronized void pushOrder(Order order)
    {
        orderQueue.addLast(order);
        notifyAll();                            // notify any waiting threads
                                                // that an order has been added
    }

    public synchronized Order pullOrder()
    {
        while (orderQueue.size() == 0)          // if there are no orders in
                                                // the queue, wait
        {
            try
            {
                wait();
            }
            catch (InterruptedException e)
            {}
        }
        return orderQueue.removeFirst();
    }
}
Previous | Order Queue application | Classes | OrderQueueApp class | Order class | OrderTaker class | Orderhhandler class | OrderQueue class