--- experimental/server/Queue/Queue.java 2000/12/28 00:58:43 1.1 +++ experimental/server/Queue/Queue.java 2001/01/18 22:08:07 1.4 @@ -1,46 +1,228 @@ +//---PACKAGE DECLARATION--- +package uk.ac.ukc.iscream.util; + +//---IMPORTS--- import java.util.LinkedList; import java.util.NoSuchElementException; +import uk.ac.ukc.iscream.util.*; -class Queue { +/** + * A Queue class designed to operate in a multi-threaded environment, with + * added support for multiple "consumer" threads. Also offers blocking on + * the get() methods, which ensures the consumer waits until the queue + * actually contains some elements. + * + * @author $Author: tdb $ + * @version $Id: Queue.java,v 1.4 2001/01/18 22:08:07 tdb Exp $ + */ +public class Queue { + +//---FINAL ATTRIBUTES--- + + /** + * The current CVS revision of this class + */ + public static final String REVISION = "$Revision: 1.4 $"; - public Queue() { - // Possible use this method instead ? - //_list = Collections.synchronizedList(new LinkedList(...)); - _list = new LinkedList(); - } +//---STATIC METHODS--- + +//---CONSTRUCTORS--- + +//---PUBLIC METHODS--- - public synchronized void add(Object o) { - int s = _list.size(); - // add() does the same thing, but this ensures behaviour - _list.addLast(o); - if (s == 0) { - notifyAll(); + /** + * This method adds a given object to every queue. It will notify + * any waiting consumers (on an empty queue) during this process. + * + * @param o An Object to be added to the queues. + */ + public void add(Object o) { + 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 _count++; } - public synchronized Object get() { - if (_list.size() == 0) { - try { wait(); } catch(Exception e) {} + /** + * This method returns an object from the front of a given 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) 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 (((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; - try { - o = _list.removeFirst(); + synchronized(this) { + try { + o = ((LinkedList) _lists.get(queue)).removeFirst(); + } + catch (NoSuchElementException e) { + // This should never happen ! + } } - catch (NoSuchElementException e) { - // no element... null already... so just leave - } return o; } - + + /** + * 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 + * includes the current size of each queue, and the total + * items passed through. + * + * @return A String message containing status information. + */ public String status() { String status = ""; - status += "Current queue size = "+_list.size(); - status += "\n"; - status += "Queue-ometer = "+_count; + 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; } - private LinkedList _list; - private int _count; -} \ No newline at end of file + /** + * Returns the size of a given queue. A consumer can use + * this to see how big their queue is at any given time. + * they should use their queue number as the parameter. + * + * @param queue The queue number to query. + * @return the current size of the queue. + */ + public int queueSize(int queue) throws InvalidQueueException { + if (queue >= _lists.size() || _lists.get(queue) == null) { + throw new InvalidQueueException("Requested queue "+queue+" does not exist"); + } + return ((LinkedList) _lists.get(queue)).size(); + } + + /** + * Returns the total numer of elements to have passed + * through this queue (ie. a counter on the add method). + * + * @return the element-ometer + */ + public int elementCount() { + return _count; + } + + /** + * 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. 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. + */ + 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()); + } + } + 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). + * + * This uses the uk.ac.ukc.iscream.util.FormatName class + * to format the toString() + * + * @return the name of this class and its CVS revision + */ + public String toString() { + return FormatName.getName( + _name, + getClass().getName(), + REVISION); + } + +//---PRIVATE METHODS--- + +//---ACCESSOR/MUTATOR METHODS--- + +//---ATTRIBUTES--- + + /** + * The LinkedLists of queues. + */ + private LinkedList _lists = new LinkedList(); + + /** + * A counter so we know how many data items have been + * passed through, for statistical purposes. + */ + private int _count = 0; + + /** + * This is the friendly identifier of the + * component this class is running in. + * eg, a Filter may be called "filter1", + * If this class does not have an owning + * component, a name from the configuration + * can be placed here. This name could also + * be changed to null for utility classes. + */ + private String _name = null; + +//---STATIC ATTRIBUTES--- + +}