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.1
Committed: Tue Jan 2 03:20:23 2001 UTC (23 years, 4 months ago) by tdb
Branch: MAIN
Log Message:
Moved Queue to main source.

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.3 2001/01/02 01:50:17 tdb1 Exp $
17 */
18 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.3 $";
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 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 public String status() {
102 String status = "";
103 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 }
114 status += "A total of "+_count+" elements have been added to the queues";
115 return status;
116 }
117
118 /**
119 * This method assigns a queue to a consumer. The idea behind
120 * this is to ensure that only 1 consumer can be associated with
121 * a given queue, otherwise the whole "blocking" thing fails
122 * miserably. Queues are created upon requested.
123 *
124 * It is IMPORTANT that removeQueue() is used when the queue is
125 * no longer required.
126 *
127 * @return An integer to be passed to the get() method.
128 */
129 public int getQueue() {
130 int pos = -1;
131 for(int i=0; i < _lists.size(); i++) {
132 if(_lists.get(i) == null) {
133 // found a gap, re-use it
134 pos = i;
135 _lists.set(i, new LinkedList());
136 }
137 }
138 if(pos == -1) {
139 //we didn't find a gap, add at end
140 pos = _lists.size();
141 _lists.add(pos, new LinkedList());
142 }
143 return pos;
144 }
145
146 /**
147 * This method sets a entry to null in the list. This ensures
148 * that it will no longer be added to after it is no longer
149 * required be a consumer.
150 *
151 * @param queue The integer identifier for the queue, given by getQueue().
152 */
153 public void removeQueue(int queue) {
154 _lists.set(queue, null);
155 }
156
157 /**
158 * Overrides the {@link java.lang.Object#toString() Object.toString()}
159 * method to provide clean logging (every class should have this).
160 *
161 * This uses the uk.ac.ukc.iscream.util.FormatName class
162 * to format the toString()
163 *
164 * @return the name of this class and its CVS revision
165 */
166 /*public String toString() {
167 return FormatName.getName(
168 _name,
169 getClass().getName(),
170 REVISION);
171 }*/
172
173 //---PRIVATE METHODS---
174
175 //---ACCESSOR/MUTATOR METHODS---
176
177 //---ATTRIBUTES---
178
179 /**
180 * The LinkedLists of queues.
181 */
182 private LinkedList _lists = new LinkedList();
183
184 /**
185 * A counter so we know how many data items have been
186 * passed through, for statistical purposes.
187 */
188 private int _count = 0;
189
190 /**
191 * This is the friendly identifier of the
192 * component this class is running in.
193 * eg, a Filter may be called "filter1",
194 * If this class does not have an owning
195 * component, a name from the configuration
196 * can be placed here. This name could also
197 * be changed to null for utility classes.
198 */
199 //private String _name = <!THIS SHOULD CALL A STATIC NAME IN THE COMPONENT CLASS FOR THIS OBJECT!>;
200
201 /**
202 * This holds a reference to the
203 * system logger that is being used.
204 */
205 //private Logger _logger = ReferenceManager.getInstance().getLogger();
206
207 //---STATIC ATTRIBUTES---
208
209 }