25 |
|
|
26 |
|
//---STATIC METHODS--- |
27 |
|
|
28 |
< |
//---CONSTRUCTORS--- |
29 |
< |
|
30 |
< |
/** |
31 |
< |
* This constructor sets up a given number of queues, all of which |
32 |
< |
* will be populated with data using the add() method. It is very |
33 |
< |
* important that the correct number is given, otherwise redundant |
34 |
< |
* queues will build up with large amounts of data in them. |
35 |
< |
* |
36 |
< |
* @param consumers The number of queues to be created. |
37 |
< |
*/ |
38 |
< |
public Queue(int consumers) { |
39 |
< |
// constuct and initialise the queues |
40 |
< |
_lists = new LinkedList[consumers]; |
41 |
< |
for(int i=0; i < _lists.length; i++) { |
42 |
< |
_lists[i] = new LinkedList(); |
43 |
< |
} |
44 |
< |
} |
45 |
< |
|
46 |
< |
/** |
47 |
< |
* This constructor is intended for an environment with a single |
48 |
< |
* consumer. This should be used in conjunction with the no-args |
49 |
< |
* get() method. |
50 |
< |
*/ |
51 |
< |
public Queue() { |
52 |
< |
// call the proper constructor |
53 |
< |
this(1); |
54 |
< |
} |
55 |
< |
|
28 |
> |
//---CONSTRUCTORS--- |
29 |
> |
|
30 |
|
//---PUBLIC METHODS--- |
31 |
|
|
32 |
|
/** |
36 |
|
* @param o An Object to be added to the queues. |
37 |
|
*/ |
38 |
|
public void add(Object o) { |
39 |
< |
for(int i=0; i < _lists.length; i++) { |
40 |
< |
int s = _lists[i].size(); |
41 |
< |
synchronized(this) { |
42 |
< |
// add() does the same thing, but this ensures behaviour |
43 |
< |
_lists[i].addLast(o); |
44 |
< |
} |
45 |
< |
// if the queue was empty before the add it is possible |
72 |
< |
// that a consumer is waiting... so we notify them |
73 |
< |
if (s == 0) { |
74 |
< |
synchronized(_lists[i]) { |
75 |
< |
_lists[i].notifyAll(); |
39 |
> |
for(int i=0; i < _lists.size(); i++) { |
40 |
> |
// skip over any gaps left in the list |
41 |
> |
if(_lists.get(i) != null) { |
42 |
> |
int s = ((LinkedList) _lists.get(i)).size(); |
43 |
> |
synchronized(this) { |
44 |
> |
// add() does the same thing, but this ensures behaviour |
45 |
> |
((LinkedList) _lists.get(i)).addLast(o); |
46 |
|
} |
47 |
+ |
// if the queue was empty before the add it is possible |
48 |
+ |
// that a consumer is waiting... so we notify them |
49 |
+ |
if (s == 0) { |
50 |
+ |
synchronized(((LinkedList) _lists.get(i))) { |
51 |
+ |
((LinkedList) _lists.get(i)).notifyAll(); |
52 |
+ |
} |
53 |
+ |
} |
54 |
|
} |
55 |
|
} |
56 |
|
// we keep track of the total additions for the status() method |
62 |
|
* It will block until data exists in the queue if required. |
63 |
|
* |
64 |
|
* @return The object from the front of the queue. |
65 |
+ |
* @throws InvalidQueueException if the queue does not exist. |
66 |
|
*/ |
67 |
< |
public Object get(int queue) { |
67 |
> |
public Object get(int queue) throws InvalidQueueException { |
68 |
> |
// make sure queue exists |
69 |
> |
if (queue >= _lists.size() || _lists.get(queue) == null) { |
70 |
> |
throw new InvalidQueueException("Requested queue "+queue+" does not exist"); |
71 |
> |
} |
72 |
|
// block if the queue is empty |
73 |
< |
if (_lists[queue].size() == 0) { |
74 |
< |
synchronized(_lists[queue]) { |
75 |
< |
try { _lists[queue].wait(); } catch(Exception e) {} |
73 |
> |
if (((LinkedList) _lists.get(queue)).size() == 0) { |
74 |
> |
synchronized(((LinkedList) _lists.get(queue))) { |
75 |
> |
try { ((LinkedList) _lists.get(queue)).wait(); } catch(Exception e) {} |
76 |
|
} |
77 |
|
} |
78 |
|
// get an item, it should never be null due to the blocking above |
79 |
|
Object o = null; |
80 |
|
synchronized(this) { |
81 |
|
try { |
82 |
< |
o = _lists[queue].removeFirst(); |
82 |
> |
o = ((LinkedList) _lists.get(queue)).removeFirst(); |
83 |
|
} |
84 |
|
catch (NoSuchElementException e) { |
85 |
|
// This should never happen ! |
87 |
|
} |
88 |
|
return o; |
89 |
|
} |
90 |
< |
|
90 |
> |
|
91 |
|
/** |
110 |
– |
* This method is intended for an environment where there is |
111 |
– |
* only a single consumer. It simply gets the item from the |
112 |
– |
* first (and presumably only) queue. |
113 |
– |
* |
114 |
– |
* @return The object from the front of the queue. |
115 |
– |
*/ |
116 |
– |
public Object get() { |
117 |
– |
return get(0); |
118 |
– |
} |
119 |
– |
|
120 |
– |
/** |
92 |
|
* This method returns a textual status of the queues. It |
93 |
|
* is merely for observation, and would most likely be used |
94 |
|
* by a larger "monitoring" component. Information returned |
99 |
|
*/ |
100 |
|
public String status() { |
101 |
|
String status = ""; |
102 |
< |
for(int i=0; i < _lists.length; i++) { |
103 |
< |
status += "Queue number "+i+" contains "+_lists[i].size()+" elements"; |
104 |
< |
status += "\n"; |
102 |
> |
for(int i=0; i < _lists.size(); i++) { |
103 |
> |
// check for null entries |
104 |
> |
if(_lists.get(i) != null) { |
105 |
> |
status += "Queue number "+i+" contains "+((LinkedList) _lists.get(i)).size()+" elements"; |
106 |
> |
status += "\n"; |
107 |
> |
} |
108 |
> |
else { |
109 |
> |
status += "Slot number "+i+" is currently empty"; |
110 |
> |
status += "\n"; |
111 |
> |
} |
112 |
|
} |
113 |
|
status += "A total of "+_count+" elements have been added to the queues"; |
114 |
|
return status; |
118 |
|
* This method assigns a queue to a consumer. The idea behind |
119 |
|
* this is to ensure that only 1 consumer can be associated with |
120 |
|
* a given queue, otherwise the whole "blocking" thing fails |
121 |
< |
* miserably. |
121 |
> |
* miserably. Queues are created upon requested. |
122 |
|
* |
123 |
+ |
* It is IMPORTANT that removeQueue() is used when the queue is |
124 |
+ |
* no longer required. |
125 |
+ |
* |
126 |
|
* @return An integer to be passed to the get() method. |
146 |
– |
* @throws NoQueueException if there are no un-assigned queue's. |
127 |
|
*/ |
128 |
< |
public int getQueue() throws NoQueueException { |
129 |
< |
if(_index < _lists.length) { |
130 |
< |
return _index++; |
128 |
> |
public int getQueue() { |
129 |
> |
int pos = -1; |
130 |
> |
for(int i=0; i < _lists.size(); i++) { |
131 |
> |
if(_lists.get(i) == null) { |
132 |
> |
// found a gap, re-use it |
133 |
> |
pos = i; |
134 |
> |
_lists.set(i, new LinkedList()); |
135 |
> |
} |
136 |
|
} |
137 |
< |
else { |
138 |
< |
throw new NoQueueException("Too many consumers, there are already "+_lists.length+" running"); |
137 |
> |
if(pos == -1) { |
138 |
> |
//we didn't find a gap, add at end |
139 |
> |
pos = _lists.size(); |
140 |
> |
_lists.add(pos, new LinkedList()); |
141 |
|
} |
142 |
+ |
return pos; |
143 |
|
} |
144 |
< |
|
144 |
> |
|
145 |
|
/** |
146 |
+ |
* This method sets a entry to null in the list. This ensures |
147 |
+ |
* that it will no longer be added to after it is no longer |
148 |
+ |
* required be a consumer. |
149 |
+ |
* |
150 |
+ |
* @param queue The integer identifier for the queue, given by getQueue(). |
151 |
+ |
*/ |
152 |
+ |
public void removeQueue(int queue) { |
153 |
+ |
_lists.set(queue, null); |
154 |
+ |
} |
155 |
+ |
|
156 |
+ |
/** |
157 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
158 |
|
* method to provide clean logging (every class should have this). |
159 |
|
* |
176 |
|
//---ATTRIBUTES--- |
177 |
|
|
178 |
|
/** |
179 |
< |
* The array of lists, which the underlying queue data |
181 |
< |
* is stored in. |
179 |
> |
* The LinkedLists of queues. |
180 |
|
*/ |
181 |
< |
private LinkedList[] _lists; |
181 |
> |
private LinkedList _lists = new LinkedList(); |
182 |
|
|
183 |
|
/** |
184 |
|
* A counter so we know how many data items have been |
186 |
|
*/ |
187 |
|
private int _count = 0; |
188 |
|
|
191 |
– |
/** |
192 |
– |
* An index of the next available queue. Used by the |
193 |
– |
* getQueue() method. |
194 |
– |
*/ |
195 |
– |
private int _index = 0; |
196 |
– |
|
189 |
|
/** |
190 |
|
* This is the friendly identifier of the |
191 |
|
* component this class is running in. |