1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* http://www.i-scream.org.uk |
4 |
+ |
* Copyright (C) 2000-2002 i-scream |
5 |
+ |
* |
6 |
+ |
* This program is free software; you can redistribute it and/or |
7 |
+ |
* modify it under the terms of the GNU General Public License |
8 |
+ |
* as published by the Free Software Foundation; either version 2 |
9 |
+ |
* of the License, or (at your option) any later version. |
10 |
+ |
* |
11 |
+ |
* This program is distributed in the hope that it will be useful, |
12 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
+ |
* GNU General Public License for more details. |
15 |
+ |
* |
16 |
+ |
* You should have received a copy of the GNU General Public License |
17 |
+ |
* along with this program; if not, write to the Free Software |
18 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
+ |
*/ |
20 |
+ |
|
21 |
|
//---PACKAGE DECLARATION--- |
22 |
< |
package uk.org.iscream.util; |
22 |
> |
package uk.org.iscream.cms.util; |
23 |
|
|
24 |
|
//---IMPORTS--- |
25 |
|
import java.util.LinkedList; |
26 |
|
import java.util.NoSuchElementException; |
27 |
|
import java.util.Random; |
28 |
< |
import uk.org.iscream.util.*; |
28 |
> |
//import uk.org.iscream.cms.util.*; |
29 |
|
|
30 |
|
/** |
31 |
|
* A Queue class designed to operate in a multi-threaded environment, with |
114 |
|
for(int i=0; i < _lists.size(); i++) { |
115 |
|
// skip over any gaps left in the list |
116 |
|
if(_lists.get(i) != null) { |
117 |
< |
// get size before adding to the Queue |
118 |
< |
int s = ((LinkedList) _lists.get(i)).size(); |
119 |
< |
// check whether we need to remove an item from the current Queue |
120 |
< |
if(_maxSize!=-1 && s==_maxSize && _removeAlgorithm!=DROP) { |
121 |
< |
// we need to remove an item |
122 |
< |
removeQueueItem((LinkedList) _lists.get(i)); |
123 |
< |
} |
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); |
117 |
> |
synchronized(((LinkedList) _lists.get(i))) { |
118 |
> |
// get size before adding to the Queue |
119 |
> |
int s = ((LinkedList) _lists.get(i)).size(); |
120 |
> |
// check whether we need to remove an item from the current Queue |
121 |
> |
if(_maxSize!=-1 && s>=_maxSize && _removeAlgorithm!=DROP) { |
122 |
> |
// we need to remove an item |
123 |
> |
removeQueueItem((LinkedList) _lists.get(i)); |
124 |
|
} |
125 |
< |
} |
126 |
< |
// if the queue was empty before the add it is possible |
127 |
< |
// that a consumer is waiting... so we notify them |
128 |
< |
if (s == 0) { |
129 |
< |
synchronized(((LinkedList) _lists.get(i))) { |
130 |
< |
((LinkedList) _lists.get(i)).notifyAll(); |
125 |
> |
// check if we should add (not if Queue full, and using DROP algorithm) |
126 |
> |
if(!(s>=_maxSize && _removeAlgorithm==DROP)) { |
127 |
> |
// add the next item, ensuring we lock |
128 |
> |
synchronized(this) { |
129 |
> |
// LinkedList.add() does the same thing, but this ensures behaviour |
130 |
> |
((LinkedList) _lists.get(i)).addLast(o); |
131 |
> |
} |
132 |
> |
// if the queue was empty before the add it is possible |
133 |
> |
// that a consumer is waiting... so we notify them |
134 |
> |
//synchronized(((LinkedList) _lists.get(i))) { |
135 |
> |
((LinkedList) _lists.get(i)).notifyAll(); |
136 |
> |
//} |
137 |
|
} |
138 |
|
} |
139 |
|
} |
156 |
|
throw new InvalidQueueException("Requested queue "+queue+" does not exist"); |
157 |
|
} |
158 |
|
// block if the queue is empty |
159 |
< |
if (((LinkedList) _lists.get(queue)).size() == 0) { |
160 |
< |
synchronized(((LinkedList) _lists.get(queue))) { |
159 |
> |
synchronized(((LinkedList) _lists.get(queue))) { |
160 |
> |
if (((LinkedList) _lists.get(queue)).size() == 0) { |
161 |
|
try { ((LinkedList) _lists.get(queue)).wait(); } catch(Exception e) {} |
162 |
|
} |
163 |
|
} |
248 |
|
|
249 |
|
/** |
250 |
|
* Returns the total numer of elements to have passed |
251 |
< |
* through this queue (ie. a counter on the add method). |
251 |
> |
* through this queue. This is essentially a counter |
252 |
> |
* on the add method. |
253 |
|
* |
254 |
|
* @return the element-ometer. |
255 |
|
*/ |
268 |
|
* |
269 |
|
* @return An integer to be passed to the get() method. |
270 |
|
*/ |
271 |
< |
public int getQueue() { |
271 |
> |
public synchronized int getQueue() { |
272 |
|
int pos = -1; |
273 |
|
for(int i=0; i < _lists.size(); i++) { |
274 |
|
if(_lists.get(i) == null) { |
292 |
|
* |
293 |
|
* @param queue The integer identifier for the queue, given by getQueue(). |
294 |
|
*/ |
295 |
< |
public void removeQueue(int queue) { |
295 |
> |
public synchronized void removeQueue(int queue) { |
296 |
|
_lists.set(queue, null); |
297 |
|
} |
298 |
|
|
353 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
354 |
|
* method to provide clean logging (every class should have this). |
355 |
|
* |
356 |
< |
* This uses the uk.org.iscream.util.FormatName class |
356 |
> |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
357 |
|
* to format the toString() |
358 |
|
* |
359 |
|
* @return the name of this class and its CVS revision. |