--- experimental/server/Queue/Queue.java 2000/12/28 03:49:15 1.2 +++ experimental/server/Queue/Queue.java 2001/01/02 01:50:17 1.3 @@ -12,7 +12,7 @@ import java.util.NoSuchElementException; * actually contains some elements. * * @author $Author: tdb $ - * @version $Id: Queue.java,v 1.2 2000/12/28 03:49:15 tdb Exp $ + * @version $Id: Queue.java,v 1.3 2001/01/02 01:50:17 tdb Exp $ */ class Queue { @@ -21,38 +21,12 @@ class Queue { /** * The current CVS revision of this class */ - public static final String REVISION = "$Revision: 1.2 $"; + public static final String REVISION = "$Revision: 1.3 $"; //---STATIC METHODS--- -//---CONSTRUCTORS--- - - /** - * This constructor sets up a given number of queues, all of which - * will be populated with data using the add() method. It is very - * important that the correct number is given, otherwise redundant - * queues will build up with large amounts of data in them. - * - * @param consumers The number of queues to be created. - */ - public Queue(int consumers) { - // constuct and initialise the queues - _lists = new LinkedList[consumers]; - for(int i=0; i < _lists.length; i++) { - _lists[i] = new LinkedList(); - } - } - - /** - * This constructor is intended for an environment with a single - * consumer. This should be used in conjunction with the no-args - * get() method. - */ - public Queue() { - // call the proper constructor - this(1); - } - +//---CONSTRUCTORS--- + //---PUBLIC METHODS--- /** @@ -62,18 +36,21 @@ class Queue { * @param o An Object to be added to the queues. */ public void add(Object o) { - for(int i=0; i < _lists.length; i++) { - int s = _lists[i].size(); - synchronized(this) { - // add() does the same thing, but this ensures behaviour - _lists[i].addLast(o); - } - // if the queue was empty before the add it is possible - // that a consumer is waiting... so we notify them - if (s == 0) { - synchronized(_lists[i]) { - _lists[i].notifyAll(); + for(int i=0; i < _lists.size(); i++) { + // skip over any gaps left in the list + if(_lists.get(i) != null) { + int s = ((LinkedList) _lists.get(i)).size(); + synchronized(this) { + // add() does the same thing, but this ensures behaviour + ((LinkedList) _lists.get(i)).addLast(o); } + // if the queue was empty before the add it is possible + // that a consumer is waiting... so we notify them + if (s == 0) { + synchronized(((LinkedList) _lists.get(i))) { + ((LinkedList) _lists.get(i)).notifyAll(); + } + } } } // we keep track of the total additions for the status() method @@ -85,19 +62,24 @@ class Queue { * It will block until data exists in the queue if required. * * @return The object from the front of the queue. + * @throws InvalidQueueException if the queue does not exist. */ - public Object get(int queue) { + public Object get(int queue) throws InvalidQueueException { + // make sure queue exists + if (queue >= _lists.size() || _lists.get(queue) == null) { + throw new InvalidQueueException("Requested queue "+queue+" does not exist"); + } // block if the queue is empty - if (_lists[queue].size() == 0) { - synchronized(_lists[queue]) { - try { _lists[queue].wait(); } catch(Exception e) {} + if (((LinkedList) _lists.get(queue)).size() == 0) { + synchronized(((LinkedList) _lists.get(queue))) { + try { ((LinkedList) _lists.get(queue)).wait(); } catch(Exception e) {} } } // get an item, it should never be null due to the blocking above Object o = null; synchronized(this) { try { - o = _lists[queue].removeFirst(); + o = ((LinkedList) _lists.get(queue)).removeFirst(); } catch (NoSuchElementException e) { // This should never happen ! @@ -105,19 +87,8 @@ class Queue { } return o; } - + /** - * This method is intended for an environment where there is - * only a single consumer. It simply gets the item from the - * first (and presumably only) queue. - * - * @return The object from the front of the queue. - */ - public Object get() { - return get(0); - } - - /** * This method returns a textual status of the queues. It * is merely for observation, and would most likely be used * by a larger "monitoring" component. Information returned @@ -128,9 +99,16 @@ class Queue { */ public String status() { String status = ""; - for(int i=0; i < _lists.length; i++) { - status += "Queue number "+i+" contains "+_lists[i].size()+" elements"; - status += "\n"; + for(int i=0; i < _lists.size(); i++) { + // check for null entries + if(_lists.get(i) != null) { + status += "Queue number "+i+" contains "+((LinkedList) _lists.get(i)).size()+" elements"; + status += "\n"; + } + else { + status += "Slot number "+i+" is currently empty"; + status += "\n"; + } } status += "A total of "+_count+" elements have been added to the queues"; return status; @@ -140,21 +118,42 @@ class Queue { * This method assigns a queue to a consumer. The idea behind * this is to ensure that only 1 consumer can be associated with * a given queue, otherwise the whole "blocking" thing fails - * miserably. + * miserably. Queues are created upon requested. * + * It is IMPORTANT that removeQueue() is used when the queue is + * no longer required. + * * @return An integer to be passed to the get() method. - * @throws NoQueueException if there are no un-assigned queue's. */ - public int getQueue() throws NoQueueException { - if(_index < _lists.length) { - return _index++; + public int getQueue() { + int pos = -1; + for(int i=0; i < _lists.size(); i++) { + if(_lists.get(i) == null) { + // found a gap, re-use it + pos = i; + _lists.set(i, new LinkedList()); + } } - else { - throw new NoQueueException("Too many consumers, there are already "+_lists.length+" running"); + if(pos == -1) { + //we didn't find a gap, add at end + pos = _lists.size(); + _lists.add(pos, new LinkedList()); } + return pos; } - + /** + * This method sets a entry to null in the list. This ensures + * that it will no longer be added to after it is no longer + * required be a consumer. + * + * @param queue The integer identifier for the queue, given by getQueue(). + */ + public void removeQueue(int queue) { + _lists.set(queue, null); + } + + /** * Overrides the {@link java.lang.Object#toString() Object.toString()} * method to provide clean logging (every class should have this). * @@ -177,10 +176,9 @@ class Queue { //---ATTRIBUTES--- /** - * The array of lists, which the underlying queue data - * is stored in. + * The LinkedLists of queues. */ - private LinkedList[] _lists; + private LinkedList _lists = new LinkedList(); /** * A counter so we know how many data items have been @@ -188,12 +186,6 @@ class Queue { */ private int _count = 0; - /** - * An index of the next available queue. Used by the - * getQueue() method. - */ - private int _index = 0; - /** * This is the friendly identifier of the * component this class is running in.