ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/server/Queue/Queue.java
Revision: 1.4
Committed: Thu Jan 18 22:08:07 2001 UTC (23 years, 11 months ago) by tdb
Branch: MAIN
Changes since 1.3: +33 -13 lines
Log Message:
Updating, bringing in sync with main version.

File Contents

# User Rev Content
1 tdb 1.2 //---PACKAGE DECLARATION---
2 tdb 1.4 package uk.ac.ukc.iscream.util;
3 tdb 1.2
4     //---IMPORTS---
5 tdb 1.1 import java.util.LinkedList;
6     import java.util.NoSuchElementException;
7 tdb 1.4 import uk.ac.ukc.iscream.util.*;
8 tdb 1.1
9 tdb 1.2 /**
10     * A Queue class designed to operate in a multi-threaded environment, with
11     * added support for multiple "consumer" threads. Also offers blocking on
12     * the get() methods, which ensures the consumer waits until the queue
13     * actually contains some elements.
14     *
15 tdb 1.3 * @author $Author: tdb1 $
16 tdb 1.4 * @version $Id: Queue.java,v 1.4 2001/01/18 19:13:57 tdb1 Exp $
17 tdb 1.2 */
18 tdb 1.4 public class Queue {
19 tdb 1.2
20     //---FINAL ATTRIBUTES---
21    
22     /**
23     * The current CVS revision of this class
24     */
25 tdb 1.4 public static final String REVISION = "$Revision: 1.4 $";
26 tdb 1.2
27     //---STATIC METHODS---
28    
29 tdb 1.3 //---CONSTRUCTORS---
30    
31 tdb 1.2 //---PUBLIC METHODS---
32    
33     /**
34     * This method adds a given object to every queue. It will notify
35     * any waiting consumers (on an empty queue) during this process.
36     *
37     * @param o An Object to be added to the queues.
38     */
39     public void add(Object o) {
40 tdb 1.3 for(int i=0; i < _lists.size(); i++) {
41     // skip over any gaps left in the list
42     if(_lists.get(i) != null) {
43     int s = ((LinkedList) _lists.get(i)).size();
44     synchronized(this) {
45     // add() does the same thing, but this ensures behaviour
46     ((LinkedList) _lists.get(i)).addLast(o);
47     }
48     // if the queue was empty before the add it is possible
49     // that a consumer is waiting... so we notify them
50     if (s == 0) {
51     synchronized(((LinkedList) _lists.get(i))) {
52     ((LinkedList) _lists.get(i)).notifyAll();
53     }
54 tdb 1.2 }
55     }
56 tdb 1.1 }
57 tdb 1.2 // we keep track of the total additions for the status() method
58 tdb 1.1 _count++;
59     }
60    
61 tdb 1.2 /**
62     * This method returns an object from the front of a given queue.
63     * It will block until data exists in the queue if required.
64     *
65     * @return The object from the front of the queue.
66 tdb 1.3 * @throws InvalidQueueException if the queue does not exist.
67 tdb 1.2 */
68 tdb 1.3 public Object get(int queue) throws InvalidQueueException {
69     // make sure queue exists
70     if (queue >= _lists.size() || _lists.get(queue) == null) {
71     throw new InvalidQueueException("Requested queue "+queue+" does not exist");
72     }
73 tdb 1.2 // block if the queue is empty
74 tdb 1.3 if (((LinkedList) _lists.get(queue)).size() == 0) {
75     synchronized(((LinkedList) _lists.get(queue))) {
76     try { ((LinkedList) _lists.get(queue)).wait(); } catch(Exception e) {}
77 tdb 1.2 }
78 tdb 1.1 }
79 tdb 1.2 // get an item, it should never be null due to the blocking above
80 tdb 1.1 Object o = null;
81 tdb 1.2 synchronized(this) {
82     try {
83 tdb 1.3 o = ((LinkedList) _lists.get(queue)).removeFirst();
84 tdb 1.2 }
85     catch (NoSuchElementException e) {
86     // This should never happen !
87     }
88 tdb 1.1 }
89     return o;
90     }
91 tdb 1.3
92 tdb 1.2 /**
93     * This method returns a textual status of the queues. It
94     * is merely for observation, and would most likely be used
95     * by a larger "monitoring" component. Information returned
96     * includes the current size of each queue, and the total
97     * items passed through.
98     *
99     * @return A String message containing status information.
100     */
101 tdb 1.1 public String status() {
102     String status = "";
103 tdb 1.3 for(int i=0; i < _lists.size(); i++) {
104     // check for null entries
105     if(_lists.get(i) != null) {
106     status += "Queue number "+i+" contains "+((LinkedList) _lists.get(i)).size()+" elements";
107     status += "\n";
108     }
109     else {
110     status += "Slot number "+i+" is currently empty";
111     status += "\n";
112     }
113 tdb 1.2 }
114     status += "A total of "+_count+" elements have been added to the queues";
115 tdb 1.1 return status;
116     }
117    
118 tdb 1.2 /**
119 tdb 1.4 * Returns the size of a given queue. A consumer can use
120     * this to see how big their queue is at any given time.
121     * they should use their queue number as the parameter.
122     *
123     * @param queue The queue number to query.
124     * @return the current size of the queue.
125     */
126     public int queueSize(int queue) throws InvalidQueueException {
127     if (queue >= _lists.size() || _lists.get(queue) == null) {
128     throw new InvalidQueueException("Requested queue "+queue+" does not exist");
129     }
130     return ((LinkedList) _lists.get(queue)).size();
131     }
132    
133     /**
134     * Returns the total numer of elements to have passed
135     * through this queue (ie. a counter on the add method).
136     *
137     * @return the element-ometer
138     */
139     public int elementCount() {
140     return _count;
141     }
142    
143     /**
144 tdb 1.2 * This method assigns a queue to a consumer. The idea behind
145     * this is to ensure that only 1 consumer can be associated with
146     * a given queue, otherwise the whole "blocking" thing fails
147 tdb 1.3 * miserably. Queues are created upon requested.
148     *
149     * It is IMPORTANT that removeQueue() is used when the queue is
150     * no longer required.
151 tdb 1.2 *
152     * @return An integer to be passed to the get() method.
153     */
154 tdb 1.3 public int getQueue() {
155     int pos = -1;
156     for(int i=0; i < _lists.size(); i++) {
157     if(_lists.get(i) == null) {
158     // found a gap, re-use it
159     pos = i;
160     _lists.set(i, new LinkedList());
161     }
162 tdb 1.2 }
163 tdb 1.3 if(pos == -1) {
164     //we didn't find a gap, add at end
165     pos = _lists.size();
166     _lists.add(pos, new LinkedList());
167 tdb 1.2 }
168 tdb 1.3 return pos;
169 tdb 1.2 }
170 tdb 1.3
171     /**
172     * This method sets a entry to null in the list. This ensures
173     * that it will no longer be added to after it is no longer
174     * required be a consumer.
175     *
176     * @param queue The integer identifier for the queue, given by getQueue().
177     */
178     public void removeQueue(int queue) {
179     _lists.set(queue, null);
180     }
181    
182 tdb 1.2 /**
183     * Overrides the {@link java.lang.Object#toString() Object.toString()}
184     * method to provide clean logging (every class should have this).
185     *
186     * This uses the uk.ac.ukc.iscream.util.FormatName class
187     * to format the toString()
188     *
189     * @return the name of this class and its CVS revision
190     */
191 tdb 1.4 public String toString() {
192 tdb 1.2 return FormatName.getName(
193     _name,
194     getClass().getName(),
195     REVISION);
196 tdb 1.4 }
197 tdb 1.2
198     //---PRIVATE METHODS---
199    
200     //---ACCESSOR/MUTATOR METHODS---
201    
202     //---ATTRIBUTES---
203    
204     /**
205 tdb 1.3 * The LinkedLists of queues.
206 tdb 1.2 */
207 tdb 1.3 private LinkedList _lists = new LinkedList();
208 tdb 1.2
209     /**
210     * A counter so we know how many data items have been
211     * passed through, for statistical purposes.
212     */
213     private int _count = 0;
214    
215     /**
216     * This is the friendly identifier of the
217     * component this class is running in.
218     * eg, a Filter may be called "filter1",
219     * If this class does not have an owning
220     * component, a name from the configuration
221     * can be placed here. This name could also
222     * be changed to null for utility classes.
223     */
224 tdb 1.4 private String _name = null;
225 tdb 1.2
226     //---STATIC ATTRIBUTES---
227    
228     }