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.4 by tdb, Thu Jan 18 22:08:07 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 85 | Line 63 | class Queue {
63       * It will block until data exists in the queue if required.
64       *
65       * @return The object from the front of the queue.
66 +     * @throws InvalidQueueException if the queue does not exist.
67       */
68 <    public Object get(int queue) {
68 >    public Object get(int queue) throws InvalidQueueException {
69 >        // make sure queue exists
70 >        if (queue >= _lists.size() || _lists.get(queue) == null) {
71 >            throw new InvalidQueueException("Requested queue "+queue+" does not exist");
72 >        }
73          // block if the queue is empty
74 <        if (_lists[queue].size() == 0) {
75 <            synchronized(_lists[queue]) {
76 <                try { _lists[queue].wait(); } catch(Exception e) {}
74 >        if (((LinkedList) _lists.get(queue)).size() == 0) {
75 >            synchronized(((LinkedList) _lists.get(queue))) {
76 >                try { ((LinkedList) _lists.get(queue)).wait(); } catch(Exception e) {}
77              }
78          }
79          // get an item, it should never be null due to the blocking above
80          Object o = null;
81          synchronized(this) {
82              try {
83 <                o = _lists[queue].removeFirst();
83 >                o = ((LinkedList) _lists.get(queue)).removeFirst();
84              }
85              catch (NoSuchElementException e) {
86                  // This should never happen !
# Line 105 | Line 88 | class Queue {
88          }
89          return o;
90      }
91 <    
91 >      
92      /**
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    /**
93       * This method returns a textual status of the queues. It
94       * is merely for observation, and would most likely be used
95       * by a larger "monitoring" component. Information returned
# Line 128 | Line 100 | class Queue {
100       */
101      public String status() {
102          String status = "";
103 <        for(int i=0; i < _lists.length; i++) {
104 <            status += "Queue number "+i+" contains "+_lists[i].size()+" elements";
105 <            status += "\n";
103 >        for(int i=0; i < _lists.size(); i++) {
104 >            // check for null entries
105 >            if(_lists.get(i) != null) {
106 >                status += "Queue number "+i+" contains "+((LinkedList) _lists.get(i)).size()+" elements";
107 >                status += "\n";
108 >            }
109 >            else {
110 >                status += "Slot number "+i+" is currently empty";
111 >                status += "\n";
112 >            }
113          }
114          status += "A total of "+_count+" elements have been added to the queues";
115          return status;
116      }
117      
118      /**
119 +     * Returns the size of a given queue. A consumer can use
120 +     * this to see how big their queue is at any given time.
121 +     * they should use their queue number as the parameter.
122 +     *
123 +     * @param queue The queue number to query.
124 +     * @return the current size of the queue.
125 +     */
126 +    public int queueSize(int queue) throws InvalidQueueException {
127 +        if (queue >= _lists.size() || _lists.get(queue) == null) {
128 +            throw new InvalidQueueException("Requested queue "+queue+" does not exist");
129 +        }
130 +        return ((LinkedList) _lists.get(queue)).size();
131 +    }
132 +    
133 +    /**
134 +     * Returns the total numer of elements to have passed
135 +     * through this queue (ie. a counter on the add method).
136 +     *
137 +     * @return the element-ometer
138 +     */
139 +    public int elementCount() {
140 +        return _count;
141 +    }
142 +    
143 +    /**
144       * This method assigns a queue to a consumer. The idea behind
145       * this is to ensure that only 1 consumer can be associated with
146       * a given queue, otherwise the whole "blocking" thing fails
147 <     * miserably.
147 >     * miserably. Queues are created upon requested.
148       *
149 +     * It is IMPORTANT that removeQueue() is used when the queue is
150 +     * no longer required.
151 +     *
152       * @return An integer to be passed to the get() method.
146     * @throws NoQueueException if there are no un-assigned queue's.
153       */
154 <    public int getQueue() throws NoQueueException {
155 <        if(_index < _lists.length) {
156 <            return _index++;
154 >    public int getQueue() {
155 >        int pos = -1;
156 >        for(int i=0; i < _lists.size(); i++) {
157 >            if(_lists.get(i) == null) {
158 >                // found a gap, re-use it
159 >                pos = i;
160 >                _lists.set(i, new LinkedList());
161 >            }
162          }
163 <        else {
164 <            throw new NoQueueException("Too many consumers, there are already "+_lists.length+" running");
163 >        if(pos == -1) {
164 >            //we didn't find a gap, add at end
165 >            pos = _lists.size();
166 >            _lists.add(pos, new LinkedList());
167          }
168 +        return pos;
169      }
170 <
170 >    
171      /**
172 +     * This method sets a entry to null in the list. This ensures
173 +     * that it will no longer be added to after it is no longer
174 +     * required be a consumer.
175 +     *
176 +     * @param queue The integer identifier for the queue, given by getQueue().
177 +     */
178 +    public void removeQueue(int queue) {
179 +        _lists.set(queue, null);
180 +    }
181 +    
182 +    /**
183       * Overrides the {@link java.lang.Object#toString() Object.toString()}
184       * method to provide clean logging (every class should have this).
185       *
# Line 163 | Line 188 | class Queue {
188       *
189       * @return the name of this class and its CVS revision
190       */
191 <    /*public String toString() {
191 >    public String toString() {
192          return FormatName.getName(
193              _name,
194              getClass().getName(),
195              REVISION);
196 <    }*/
196 >    }
197  
198   //---PRIVATE METHODS---
199  
# Line 177 | Line 202 | class Queue {
202   //---ATTRIBUTES---
203      
204      /**
205 <     * The array of lists, which the underlying queue data
181 <     * is stored in.
205 >     * The LinkedLists of queues.
206       */
207 <    private LinkedList[] _lists;
207 >    private LinkedList _lists = new LinkedList();
208      
209      /**
210       * A counter so we know how many data items have been
# Line 189 | Line 213 | class Queue {
213      private int _count = 0;
214      
215      /**
192     * An index of the next available queue. Used by the
193     * getQueue() method.
194     */
195    private int _index = 0;
196
197    /**
216       * This is the friendly identifier of the
217       * component this class is running in.
218       * eg, a Filter may be called "filter1",
# Line 203 | Line 221 | class Queue {
221       * can be placed here.  This name could also
222       * be changed to null for utility classes.
223       */
224 <    //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();
224 >    private String _name = null;
225  
226   //---STATIC ATTRIBUTES---
227  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines