1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.org.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 java.util.Random; |
8 |
< |
import uk.org.iscream.util.*; |
8 |
> |
import uk.org.iscream.cms.server.util.*; |
9 |
|
|
10 |
|
/** |
11 |
|
* A Queue class designed to operate in a multi-threaded environment, with |
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 |
< |
// get size before adding to the Queue |
98 |
< |
int s = ((LinkedList) _lists.get(i)).size(); |
99 |
< |
// check whether we need to remove an item from the current Queue |
100 |
< |
if(_maxSize!=-1 && s>=_maxSize && _removeAlgorithm!=DROP) { |
101 |
< |
// we need to remove an item |
102 |
< |
removeQueueItem((LinkedList) _lists.get(i)); |
103 |
< |
} |
104 |
< |
// check if we should add (not if Queue full, and using DROP algorithm) |
105 |
< |
if(!(s>=_maxSize && _removeAlgorithm==DROP)) { |
106 |
< |
// add the next item, ensuring we lock |
107 |
< |
synchronized(this) { |
108 |
< |
// LinkedList.add() does the same thing, but this ensures behaviour |
109 |
< |
((LinkedList) _lists.get(i)).addLast(o); |
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 |
|
} |
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 |
– |
} |
119 |
|
} |
120 |
|
} |
121 |
|
// we keep track of the total additions for the status() method |
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 |
|
*/ |
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.org.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. |