1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.ac.ukc.iscream.util; |
2 |
> |
package uk.org.iscream.cms.server.util; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
|
import java.util.LinkedList; |
6 |
|
import java.util.NoSuchElementException; |
7 |
< |
import uk.ac.ukc.iscream.util.*; |
7 |
> |
import java.util.Random; |
8 |
> |
import uk.org.iscream.cms.server.util.*; |
9 |
|
|
10 |
|
/** |
11 |
|
* A Queue class designed to operate in a multi-threaded environment, with |
25 |
|
*/ |
26 |
|
public static final String REVISION = "$Revision$"; |
27 |
|
|
28 |
+ |
/** |
29 |
+ |
* Pass to constructor to remove a RANDOM item from |
30 |
+ |
* the Queue upon reaching the maximum limit. |
31 |
+ |
*/ |
32 |
+ |
public static final int RANDOM = 0; |
33 |
+ |
|
34 |
+ |
/** |
35 |
+ |
* Pass to constructor to remove the FIRST item from |
36 |
+ |
* the Queue upon reaching the maximum limit. |
37 |
+ |
*/ |
38 |
+ |
public static final int FIRST = 1; |
39 |
+ |
|
40 |
+ |
/** |
41 |
+ |
* Pass to constructor to remove the LAST item from |
42 |
+ |
* the Queue upon reaching the maximum limit. |
43 |
+ |
*/ |
44 |
+ |
public static final int LAST = 2; |
45 |
+ |
|
46 |
+ |
/** |
47 |
+ |
* Pass to constructor to drop the new item upon reaching |
48 |
+ |
* the maximum Queue limit. |
49 |
+ |
*/ |
50 |
+ |
public static final int DROP = 3; |
51 |
+ |
|
52 |
+ |
/** |
53 |
+ |
* To allow opposite lookups. |
54 |
+ |
*/ |
55 |
+ |
public static final String[] algorithms = {"RANDOM", "FIRST", "LAST", "DROP"}; |
56 |
+ |
|
57 |
|
//---STATIC METHODS--- |
58 |
|
|
59 |
|
//---CONSTRUCTORS--- |
60 |
+ |
|
61 |
+ |
/** |
62 |
+ |
* Constructs a new Queue with a maximum size limit on |
63 |
+ |
* any individual queue. This should be used to stop |
64 |
+ |
* conditions where the Queue cannot be guaranteed to |
65 |
+ |
* be emptied as quick as it's filled. |
66 |
+ |
* |
67 |
+ |
* An algorithm will be used to remove data when new data |
68 |
+ |
* arrives. There may be choices of algorithms later on. |
69 |
+ |
* |
70 |
+ |
* @param maxSize the upper limit for a queue |
71 |
+ |
* @param removeAlgorithm the remove algorithm to use upon reaching the maxSize |
72 |
+ |
*/ |
73 |
+ |
public Queue(int maxSize, int removeAlgorithm) { |
74 |
+ |
_maxSize = maxSize; |
75 |
+ |
_removeAlgorithm = removeAlgorithm; |
76 |
+ |
} |
77 |
+ |
|
78 |
+ |
/** |
79 |
+ |
* Constructs a Queue with no maximum size. |
80 |
+ |
*/ |
81 |
+ |
public Queue() { |
82 |
+ |
_maxSize = -1; |
83 |
+ |
} |
84 |
|
|
85 |
|
//---PUBLIC METHODS--- |
86 |
|
|
94 |
|
for(int i=0; i < _lists.size(); i++) { |
95 |
|
// skip over any gaps left in the list |
96 |
|
if(_lists.get(i) != null) { |
97 |
< |
int s = ((LinkedList) _lists.get(i)).size(); |
98 |
< |
synchronized(this) { |
99 |
< |
// add() does the same thing, but this ensures behaviour |
100 |
< |
((LinkedList) _lists.get(i)).addLast(o); |
101 |
< |
} |
102 |
< |
// if the queue was empty before the add it is possible |
103 |
< |
// 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(); |
97 |
> |
synchronized(((LinkedList) _lists.get(i))) { |
98 |
> |
// get size before adding to the Queue |
99 |
> |
int s = ((LinkedList) _lists.get(i)).size(); |
100 |
> |
// check whether we need to remove an item from the current Queue |
101 |
> |
if(_maxSize!=-1 && s>=_maxSize && _removeAlgorithm!=DROP) { |
102 |
> |
// we need to remove an item |
103 |
> |
removeQueueItem((LinkedList) _lists.get(i)); |
104 |
|
} |
105 |
+ |
// check if we should add (not if Queue full, and using DROP algorithm) |
106 |
+ |
if(!(s>=_maxSize && _removeAlgorithm==DROP)) { |
107 |
+ |
// add the next item, ensuring we lock |
108 |
+ |
synchronized(this) { |
109 |
+ |
// LinkedList.add() does the same thing, but this ensures behaviour |
110 |
+ |
((LinkedList) _lists.get(i)).addLast(o); |
111 |
+ |
} |
112 |
+ |
// if the queue was empty before the add it is possible |
113 |
+ |
// that a consumer is waiting... so we notify them |
114 |
+ |
//synchronized(((LinkedList) _lists.get(i))) { |
115 |
+ |
((LinkedList) _lists.get(i)).notifyAll(); |
116 |
+ |
//} |
117 |
+ |
} |
118 |
|
} |
119 |
|
} |
120 |
|
} |
136 |
|
throw new InvalidQueueException("Requested queue "+queue+" does not exist"); |
137 |
|
} |
138 |
|
// block if the queue is empty |
139 |
< |
if (((LinkedList) _lists.get(queue)).size() == 0) { |
140 |
< |
synchronized(((LinkedList) _lists.get(queue))) { |
139 |
> |
synchronized(((LinkedList) _lists.get(queue))) { |
140 |
> |
if (((LinkedList) _lists.get(queue)).size() == 0) { |
141 |
|
try { ((LinkedList) _lists.get(queue)).wait(); } catch(Exception e) {} |
142 |
|
} |
143 |
|
} |
199 |
|
status += "queue"+i+"=\""+((LinkedList) _lists.get(i)).size()+"\" "; |
200 |
|
} |
201 |
|
else { |
202 |
< |
status += "queue"+i+"=\"null\" "; |
202 |
> |
status += "queue"+i+"=\"[deleted]\" "; |
203 |
|
} |
204 |
|
} |
205 |
< |
status += "total=\""+_count+"\"></queue>"; |
205 |
> |
status += "total=\""+_count+"\""; |
206 |
> |
if(_maxSize != -1) { |
207 |
> |
status += " maxSize=\""+_maxSize+"\""; |
208 |
> |
} |
209 |
> |
status += "></queue>"; |
210 |
|
return status; |
211 |
|
} |
212 |
|
|
228 |
|
|
229 |
|
/** |
230 |
|
* Returns the total numer of elements to have passed |
231 |
< |
* through this queue (ie. a counter on the add method). |
231 |
> |
* through this queue. This is essentially a counter |
232 |
> |
* on the add method. |
233 |
|
* |
234 |
|
* @return the element-ometer. |
235 |
|
*/ |
248 |
|
* |
249 |
|
* @return An integer to be passed to the get() method. |
250 |
|
*/ |
251 |
< |
public int getQueue() { |
251 |
> |
public synchronized int getQueue() { |
252 |
|
int pos = -1; |
253 |
|
for(int i=0; i < _lists.size(); i++) { |
254 |
|
if(_lists.get(i) == null) { |
272 |
|
* |
273 |
|
* @param queue The integer identifier for the queue, given by getQueue(). |
274 |
|
*/ |
275 |
< |
public void removeQueue(int queue) { |
275 |
> |
public synchronized void removeQueue(int queue) { |
276 |
|
_lists.set(queue, null); |
277 |
|
} |
278 |
|
|
333 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
334 |
|
* method to provide clean logging (every class should have this). |
335 |
|
* |
336 |
< |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
336 |
> |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
337 |
|
* to format the toString() |
338 |
|
* |
339 |
|
* @return the name of this class and its CVS revision. |
346 |
|
} |
347 |
|
|
348 |
|
//---PRIVATE METHODS--- |
349 |
< |
|
349 |
> |
|
350 |
> |
/** |
351 |
> |
* This method removes an item from a Queue, using a method |
352 |
> |
* requested at construction. |
353 |
> |
* |
354 |
> |
* @param list The LinkedList from which to remove an item. |
355 |
> |
*/ |
356 |
> |
private void removeQueueItem(LinkedList list) { |
357 |
> |
// look at our algorithm |
358 |
> |
// remove a random item from the list |
359 |
> |
if(_removeAlgorithm==RANDOM) { |
360 |
> |
// new Random, with a good seed |
361 |
> |
Random rand = new Random(System.currentTimeMillis()); |
362 |
> |
int i = rand.nextInt(_maxSize); |
363 |
> |
synchronized(this) { |
364 |
> |
list.remove(i); |
365 |
> |
} |
366 |
> |
} |
367 |
> |
// remove the first item from the list |
368 |
> |
else if(_removeAlgorithm==FIRST) { |
369 |
> |
synchronized(this) { |
370 |
> |
list.removeFirst(); |
371 |
> |
} |
372 |
> |
} |
373 |
> |
// remove the last item from the list |
374 |
> |
else if(_removeAlgorithm==LAST) { |
375 |
> |
synchronized(this) { |
376 |
> |
list.removeLast(); |
377 |
> |
} |
378 |
> |
} |
379 |
> |
} |
380 |
> |
|
381 |
|
//---ACCESSOR/MUTATOR METHODS--- |
382 |
|
|
383 |
|
//---ATTRIBUTES--- |
397 |
|
* A reference to our QueueMonitor, if we have one. |
398 |
|
*/ |
399 |
|
private QueueMonitor _queueMon = null; |
400 |
+ |
|
401 |
+ |
/** |
402 |
+ |
* The maximum size of any Queue. |
403 |
+ |
*/ |
404 |
+ |
private int _maxSize = -1; |
405 |
+ |
|
406 |
+ |
/** |
407 |
+ |
* The remove algorithm to use upon a Queue reaching |
408 |
+ |
* it's maximum size. |
409 |
+ |
*/ |
410 |
+ |
private int _removeAlgorithm = -1; |
411 |
|
|
412 |
|
/** |
413 |
|
* This is the friendly identifier of the |