ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/util/uk/org/iscream/cms/util/Queue.java
Revision: 1.7
Committed: Tue Jan 30 01:56:28 2001 UTC (23 years, 3 months ago) by tdb
Branch: MAIN
Changes since 1.6: +16 -2 lines
Log Message:
Added a method to erase the contents of a queue.

File Contents

# Content
1 //---PACKAGE DECLARATION---
2 package uk.ac.ukc.iscream.util;
3
4 //---IMPORTS---
5 import java.util.LinkedList;
6 import java.util.NoSuchElementException;
7 import uk.ac.ukc.iscream.util.*;
8
9 /**
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 * @author $Author: tdb1 $
16 * @version $Id: Queue.java,v 1.6 2001/01/23 18:22:28 tdb1 Exp $
17 */
18 public class Queue {
19
20 //---FINAL ATTRIBUTES---
21
22 /**
23 * The current CVS revision of this class
24 */
25 public static final String REVISION = "$Revision: 1.6 $";
26
27 //---STATIC METHODS---
28
29 //---CONSTRUCTORS---
30
31 //---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 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 }
55 }
56 }
57 // we keep track of the total additions for the status() method
58 _count++;
59 }
60
61 /**
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 * @throws InvalidQueueException if the queue does not exist.
67 */
68 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 // block if the queue is empty
74 if (((LinkedList) _lists.get(queue)).size() == 0) {
75 synchronized(((LinkedList) _lists.get(queue))) {
76 try { ((LinkedList) _lists.get(queue)).wait(); } catch(Exception e) {}
77 }
78 }
79 // get an item, it should never be null due to the blocking above
80 Object o = null;
81 synchronized(this) {
82 try {
83 o = ((LinkedList) _lists.get(queue)).removeFirst();
84 }
85 catch (NoSuchElementException e) {
86 // This should never happen !
87 }
88 }
89 return o;
90 }
91
92 /**
93 * This method releases a get() method that's currently
94 * waiting on an empty queue. This was designed for
95 * shutdown() type methods that may have problems closing
96 * if the thread of control is waiting on a queue.
97 *
98 * @param queue the queue to release
99 */
100 public void releaseQueue(int queue) {
101 synchronized(((LinkedList) _lists.get(queue))) {
102 ((LinkedList) _lists.get(queue)).notifyAll();
103 }
104 }
105
106 /**
107 * This method erases the contents of a given queue. This
108 * method should be used with care. It can only empty one
109 * internal queue, not all of them. This must be called
110 * multiple times to empty all queues.
111 *
112 * @param queue the queue to empty.
113 */
114 public void clearQueue(int queue) {
115 synchronized(this) {
116 ((LinkedList) _lists.get(queue)).clear();
117 }
118 }
119
120 /**
121 * This method returns a textual status of the queues. It
122 * is merely for observation, and would most likely be used
123 * by a larger "monitoring" component. Information returned
124 * includes the current size of each queue, and the total
125 * items passed through.
126 *
127 * @return A String message containing status information.
128 */
129 public String status() {
130 String status = "";
131 for(int i=0; i < _lists.size(); i++) {
132 // check for null entries
133 if(_lists.get(i) != null) {
134 status += "Queue number "+i+" contains "+((LinkedList) _lists.get(i)).size()+" elements";
135 status += "\n";
136 }
137 else {
138 status += "Slot number "+i+" is currently empty";
139 status += "\n";
140 }
141 }
142 status += "A total of "+_count+" elements have been added to the queues";
143 return status;
144 }
145
146 /**
147 * Returns the size of a given queue. A consumer can use
148 * this to see how big their queue is at any given time.
149 * they should use their queue number as the parameter.
150 *
151 * @param queue The queue number to query.
152 * @return the current size of the queue.
153 */
154 public int queueSize(int queue) throws InvalidQueueException {
155 if (queue >= _lists.size() || _lists.get(queue) == null) {
156 throw new InvalidQueueException("Requested queue "+queue+" does not exist");
157 }
158 return ((LinkedList) _lists.get(queue)).size();
159 }
160
161 /**
162 * Returns the total numer of elements to have passed
163 * through this queue (ie. a counter on the add method).
164 *
165 * @return the element-ometer
166 */
167 public int elementCount() {
168 return _count;
169 }
170
171 /**
172 * This method assigns a queue to a consumer. The idea behind
173 * this is to ensure that only 1 consumer can be associated with
174 * a given queue, otherwise the whole "blocking" thing fails
175 * miserably. Queues are created upon requested.
176 *
177 * It is IMPORTANT that removeQueue() is used when the queue is
178 * no longer required.
179 *
180 * @return An integer to be passed to the get() method.
181 */
182 public int getQueue() {
183 int pos = -1;
184 for(int i=0; i < _lists.size(); i++) {
185 if(_lists.get(i) == null) {
186 // found a gap, re-use it
187 pos = i;
188 _lists.set(i, new LinkedList());
189 }
190 }
191 if(pos == -1) {
192 //we didn't find a gap, add at end
193 pos = _lists.size();
194 _lists.add(pos, new LinkedList());
195 }
196 return pos;
197 }
198
199 /**
200 * This method sets a entry to null in the list. This ensures
201 * that it will no longer be added to after it is no longer
202 * required be a consumer.
203 *
204 * @param queue The integer identifier for the queue, given by getQueue().
205 */
206 public void removeQueue(int queue) {
207 _lists.set(queue, null);
208 }
209
210 /**
211 * Overrides the {@link java.lang.Object#toString() Object.toString()}
212 * method to provide clean logging (every class should have this).
213 *
214 * This uses the uk.ac.ukc.iscream.util.FormatName class
215 * to format the toString()
216 *
217 * @return the name of this class and its CVS revision
218 */
219 public String toString() {
220 return FormatName.getName(
221 _name,
222 getClass().getName(),
223 REVISION);
224 }
225
226 //---PRIVATE METHODS---
227
228 //---ACCESSOR/MUTATOR METHODS---
229
230 //---ATTRIBUTES---
231
232 /**
233 * The LinkedLists of queues.
234 */
235 private LinkedList _lists = new LinkedList();
236
237 /**
238 * A counter so we know how many data items have been
239 * passed through, for statistical purposes.
240 */
241 private int _count = 0;
242
243 /**
244 * This is the friendly identifier of the
245 * component this class is running in.
246 * eg, a Filter may be called "filter1",
247 * If this class does not have an owning
248 * component, a name from the configuration
249 * can be placed here. This name could also
250 * be changed to null for utility classes.
251 */
252 private String _name = null;
253
254 //---STATIC ATTRIBUTES---
255
256 }