ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/server/Queue/Queue.java
(Generate patch)

Comparing experimental/server/Queue/Queue.java (file contents):
Revision 1.2 by tdb, Thu Dec 28 03:49:15 2000 UTC vs.
Revision 1.6 by tdb, Tue Jan 30 02:13:09 2001 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 + package uk.ac.ukc.iscream.util;
3  
4   //---IMPORTS---
5   import java.util.LinkedList;
6   import java.util.NoSuchElementException;
7 < //import uk.ac.ukc.iscream.util.*;
7 > import uk.ac.ukc.iscream.util.*;
8  
9   /**
10   * A Queue class designed to operate in a multi-threaded environment, with
# Line 14 | Line 15 | import java.util.NoSuchElementException;
15   * @author  $Author$
16   * @version $Id$
17   */
18 < class Queue {
18 > public class Queue {
19  
20   //---FINAL ATTRIBUTES---
21  
# Line 25 | Line 26 | class Queue {
26      
27   //---STATIC METHODS---
28  
29 < //---CONSTRUCTORS---
30 <      
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 <    
29 > //---CONSTRUCTORS---  
30 >
31   //---PUBLIC METHODS---
32      
33      /**
# Line 62 | Line 37 | class Queue {
37       * @param o An Object to be added to the queues.
38       */
39      public void add(Object o) {
40 <        for(int i=0; i < _lists.length; i++) {
41 <            int s = _lists[i].size();
42 <            synchronized(this) {
43 <                // add() does the same thing, but this ensures behaviour
44 <                _lists[i].addLast(o);
45 <            }
46 <            // 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();
40 >        for(int i=0; i < _lists.size(); i++) {
41 >            // skip over any gaps left in the list
42 >            if(_lists.get(i) != null) {
43 >                int s = ((LinkedList) _lists.get(i)).size();
44 >                synchronized(this) {
45 >                    // add() does the same thing, but this ensures behaviour
46 >                    ((LinkedList) _lists.get(i)).addLast(o);
47                  }
48 +                // if the queue was empty before the add it is possible
49 +                // 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();
53 +                    }
54 +                }
55              }
56          }
57          // we keep track of the total additions for the status() method
# Line 84 | Line 62 | class Queue {
62       * This method returns an object from the front of a given queue.
63       * It will block until data exists in the queue if required.
64       *
65 +     * @param The queue to retrieve data from.
66       * @return The object from the front of the queue.
67 +     * @throws InvalidQueueException if the queue does not exist.
68       */
69 <    public Object get(int queue) {
69 >    public Object get(int queue) throws InvalidQueueException {
70 >        // make sure queue exists
71 >        if (queue >= _lists.size() || _lists.get(queue) == null) {
72 >            throw new InvalidQueueException("Requested queue "+queue+" does not exist");
73 >        }
74          // block if the queue is empty
75 <        if (_lists[queue].size() == 0) {
76 <            synchronized(_lists[queue]) {
77 <                try { _lists[queue].wait(); } catch(Exception e) {}
75 >        if (((LinkedList) _lists.get(queue)).size() == 0) {
76 >            synchronized(((LinkedList) _lists.get(queue))) {
77 >                try { ((LinkedList) _lists.get(queue)).wait(); } catch(Exception e) {}
78              }
79          }
80          // get an item, it should never be null due to the blocking above
81          Object o = null;
82          synchronized(this) {
83              try {
84 <                o = _lists[queue].removeFirst();
84 >                o = ((LinkedList) _lists.get(queue)).removeFirst();
85              }
86              catch (NoSuchElementException e) {
87                  // This should never happen !
# Line 107 | Line 91 | class Queue {
91      }
92      
93      /**
94 <     * This method is intended for an environment where there is
95 <     * only a single consumer. It simply gets the item from the
96 <     * first (and presumably only) queue.
94 >     * This method releases a get() method that's currently
95 >     * waiting on an empty queue. This was designed for
96 >     * shutdown() type methods that may have problems closing
97 >     * if the thread of control is waiting on a queue.
98       *
99 <     * @return The object from the front of the queue.
99 >     * @param queue the queue to release.
100       */
101 <    public Object get() {
102 <        return get(0);
101 >    public void releaseQueue(int queue) {
102 >        synchronized(((LinkedList) _lists.get(queue))) {
103 >                ((LinkedList) _lists.get(queue)).notifyAll();
104 >        }
105      }
106 +
107 +    /**
108 +     * This method erases the contents of a given queue. This
109 +     * method should be used with care. It can only empty one
110 +     * internal queue, not all of them. This must be called
111 +     * multiple times to empty all queues.
112 +     *
113 +     * @param queue the queue to empty.
114 +     */
115 +    public void clearQueue(int queue) {
116 +        synchronized(this) {
117 +            ((LinkedList) _lists.get(queue)).clear();
118 +        }
119 +    }
120      
121      /**
122       * This method returns a textual status of the queues. It
# Line 128 | Line 129 | class Queue {
129       */
130      public String status() {
131          String status = "";
132 <        for(int i=0; i < _lists.length; i++) {
133 <            status += "Queue number "+i+" contains "+_lists[i].size()+" elements";
134 <            status += "\n";
132 >        for(int i=0; i < _lists.size(); i++) {
133 >            // check for null entries
134 >            if(_lists.get(i) != null) {
135 >                status += "Queue number "+i+" contains "+((LinkedList) _lists.get(i)).size()+" elements";
136 >                status += "\n";
137 >            }
138 >            else {
139 >                status += "Slot number "+i+" is currently empty";
140 >                status += "\n";
141 >            }
142          }
143          status += "A total of "+_count+" elements have been added to the queues";
144          return status;
145      }
146      
147      /**
148 +     * Returns the size of a given queue. A consumer can use
149 +     * this to see how big their queue is at any given time.
150 +     * they should use their queue number as the parameter.
151 +     *
152 +     * @param queue The queue number to query.
153 +     * @return the current size of the queue.
154 +     * @throws InvalidQueueException if the queue does not exist.
155 +     */
156 +    public int queueSize(int queue) throws InvalidQueueException {
157 +        if (queue >= _lists.size() || _lists.get(queue) == null) {
158 +            throw new InvalidQueueException("Requested queue "+queue+" does not exist");
159 +        }
160 +        return ((LinkedList) _lists.get(queue)).size();
161 +    }
162 +    
163 +    /**
164 +     * Returns the total numer of elements to have passed
165 +     * through this queue (ie. a counter on the add method).
166 +     *
167 +     * @return the element-ometer.
168 +     */
169 +    public int elementCount() {
170 +        return _count;
171 +    }
172 +    
173 +    /**
174       * This method assigns a queue to a consumer. The idea behind
175       * this is to ensure that only 1 consumer can be associated with
176       * a given queue, otherwise the whole "blocking" thing fails
177 <     * miserably.
177 >     * miserably. Queues are created upon requested.
178       *
179 +     * It is IMPORTANT that removeQueue() is used when the queue is
180 +     * no longer required.
181 +     *
182       * @return An integer to be passed to the get() method.
146     * @throws NoQueueException if there are no un-assigned queue's.
183       */
184 <    public int getQueue() throws NoQueueException {
185 <        if(_index < _lists.length) {
186 <            return _index++;
184 >    public int getQueue() {
185 >        int pos = -1;
186 >        for(int i=0; i < _lists.size(); i++) {
187 >            if(_lists.get(i) == null) {
188 >                // found a gap, re-use it
189 >                pos = i;
190 >                _lists.set(i, new LinkedList());
191 >            }
192          }
193 <        else {
194 <            throw new NoQueueException("Too many consumers, there are already "+_lists.length+" running");
193 >        if(pos == -1) {
194 >            //we didn't find a gap, add at end
195 >            pos = _lists.size();
196 >            _lists.add(pos, new LinkedList());
197          }
198 +        return pos;
199      }
200 <
200 >    
201      /**
202 +     * This method sets a entry to null in the list. This ensures
203 +     * that it will no longer be added to after it is no longer
204 +     * required be a consumer.
205 +     *
206 +     * @param queue The integer identifier for the queue, given by getQueue().
207 +     */
208 +    public void removeQueue(int queue) {
209 +        _lists.set(queue, null);
210 +    }
211 +    
212 +    /**
213       * Overrides the {@link java.lang.Object#toString() Object.toString()}
214       * method to provide clean logging (every class should have this).
215       *
216       * This uses the uk.ac.ukc.iscream.util.FormatName class
217       * to format the toString()
218       *
219 <     * @return the name of this class and its CVS revision
219 >     * @return the name of this class and its CVS revision.
220       */
221 <    /*public String toString() {
221 >    public String toString() {
222          return FormatName.getName(
223              _name,
224              getClass().getName(),
225              REVISION);
226 <    }*/
226 >    }
227  
228   //---PRIVATE METHODS---
229  
# Line 177 | Line 232 | class Queue {
232   //---ATTRIBUTES---
233      
234      /**
235 <     * The array of lists, which the underlying queue data
181 <     * is stored in.
235 >     * The LinkedLists of queues.
236       */
237 <    private LinkedList[] _lists;
237 >    private LinkedList _lists = new LinkedList();
238      
239      /**
240       * A counter so we know how many data items have been
# Line 189 | Line 243 | class Queue {
243      private int _count = 0;
244      
245      /**
192     * An index of the next available queue. Used by the
193     * getQueue() method.
194     */
195    private int _index = 0;
196
197    /**
246       * This is the friendly identifier of the
247       * component this class is running in.
248       * eg, a Filter may be called "filter1",
# Line 203 | Line 251 | class Queue {
251       * can be placed here.  This name could also
252       * be changed to null for utility classes.
253       */
254 <    //private String _name = <!THIS SHOULD CALL A STATIC NAME IN THE COMPONENT CLASS FOR THIS OBJECT!>;
207 <
208 <    /**
209 <     * This holds a reference to the
210 <     * system logger that is being used.
211 <     */
212 <    //private Logger _logger = ReferenceManager.getInstance().getLogger();
254 >    private String _name = null;
255  
256   //---STATIC ATTRIBUTES---
257  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines