1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.ac.ukc.iscream.util; |
2 |
> |
package uk.org.iscream.util; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
|
import java.util.LinkedList; |
6 |
|
import java.util.NoSuchElementException; |
7 |
|
import java.util.Random; |
8 |
< |
import uk.ac.ukc.iscream.util.*; |
8 |
> |
import uk.org.iscream.util.*; |
9 |
|
|
10 |
|
/** |
11 |
|
* A Queue class designed to operate in a multi-threaded environment, with |
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--- |
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) { |
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)) { |
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 |
111 |
|
} |
112 |
|
// if the queue was empty before the add it is possible |
113 |
|
// that a consumer is waiting... so we notify them |
114 |
< |
if (s == 0) { |
115 |
< |
synchronized(((LinkedList) _lists.get(i))) { |
111 |
< |
((LinkedList) _lists.get(i)).notifyAll(); |
112 |
< |
} |
114 |
> |
synchronized(((LinkedList) _lists.get(i))) { |
115 |
> |
((LinkedList) _lists.get(i)).notifyAll(); |
116 |
|
} |
117 |
|
} |
118 |
|
} |
134 |
|
throw new InvalidQueueException("Requested queue "+queue+" does not exist"); |
135 |
|
} |
136 |
|
// block if the queue is empty |
137 |
< |
if (((LinkedList) _lists.get(queue)).size() == 0) { |
138 |
< |
synchronized(((LinkedList) _lists.get(queue))) { |
137 |
> |
synchronized(((LinkedList) _lists.get(queue))) { |
138 |
> |
if (((LinkedList) _lists.get(queue)).size() == 0) { |
139 |
|
try { ((LinkedList) _lists.get(queue)).wait(); } catch(Exception e) {} |
140 |
|
} |
141 |
|
} |
245 |
|
* |
246 |
|
* @return An integer to be passed to the get() method. |
247 |
|
*/ |
248 |
< |
public int getQueue() { |
248 |
> |
public synchronized int getQueue() { |
249 |
|
int pos = -1; |
250 |
|
for(int i=0; i < _lists.size(); i++) { |
251 |
|
if(_lists.get(i) == null) { |
269 |
|
* |
270 |
|
* @param queue The integer identifier for the queue, given by getQueue(). |
271 |
|
*/ |
272 |
< |
public void removeQueue(int queue) { |
272 |
> |
public synchronized void removeQueue(int queue) { |
273 |
|
_lists.set(queue, null); |
274 |
|
} |
275 |
|
|
330 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
331 |
|
* method to provide clean logging (every class should have this). |
332 |
|
* |
333 |
< |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
333 |
> |
* This uses the uk.org.iscream.util.FormatName class |
334 |
|
* to format the toString() |
335 |
|
* |
336 |
|
* @return the name of this class and its CVS revision. |