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.8 by tdb, Mon Feb 26 01:05:16 2001 UTC vs.
Revision 1.9 by tdb, Thu Mar 1 03:04:28 2001 UTC

# Line 4 | Line 4
4   //---IMPORTS---
5   import java.util.LinkedList;
6   import java.util.NoSuchElementException;
7 + import java.util.Random;
8   //import uk.ac.ukc.iscream.util.*;
9  
10   /**
# Line 24 | Line 25 | public class Queue {
25       */
26      public static final String REVISION = "$Revision$";
27      
28 +    /**
29 +     * Pass to constructor to remove a RANDOM item from
30 +     * the Queue upon reaching the maximum limit.
31 +     */
32 +    public static final int RANDOM = 0;
33 +    
34 +    /**
35 +     * Pass to constructor to remove the FIRST item from
36 +     * the Queue upon reaching the maximum limit.
37 +     */
38 +    public static final int FIRST = 1;
39 +    
40 +    /**
41 +     * Pass to constructor to remove the LAST item from
42 +     * the Queue upon reaching the maximum limit.
43 +     */
44 +    public static final int LAST = 2;
45 +    
46 +    /**
47 +     * Pass to constructor to drop the new item upon reaching
48 +     * the maximum Queue limit.
49 +     */
50 +    public static final int DROP = 3;
51 +    
52   //---STATIC METHODS---
53  
54   //---CONSTRUCTORS---  
55 +    
56 +    /**
57 +     * Constructs a new Queue with a maximum size limit on
58 +     * any individual queue. This should be used to stop
59 +     * conditions where the Queue cannot be guaranteed to
60 +     * be emptied as quick as it's filled.
61 +     *
62 +     * An algorithm will be used to remove data when new data
63 +     * arrives. There may be choices of algorithms later on.
64 +     *
65 +     * @param maxSize the upper limit for a queue
66 +     * @param removeAlgorithm the remove algorithm to use upon reaching the maxSize
67 +     */
68 +    public Queue(int maxSize, int removeAlgorithm) {
69 +        _maxSize = maxSize;
70 +        _removeAlgorithm = removeAlgorithm;
71 +    }
72 +    
73 +    /**
74 +     * Constructs a Queue with no maximum size.
75 +     */
76 +    public Queue() {
77 +        _maxSize = -1;
78 +    }
79  
80   //---PUBLIC METHODS---
81      
# Line 40 | Line 89 | public class Queue {
89          for(int i=0; i < _lists.size(); i++) {
90              // skip over any gaps left in the list
91              if(_lists.get(i) != null) {
92 +                // get size before adding to the Queue
93                  int s = ((LinkedList) _lists.get(i)).size();
94 <                synchronized(this) {
95 <                    // add() does the same thing, but this ensures behaviour
96 <                    ((LinkedList) _lists.get(i)).addLast(o);
94 >                // check whether we need to remove an item from the current Queue
95 >                if(_maxSize!=-1 && s==_maxSize && _removeAlgorithm!=DROP) {
96 >                    // we need to remove an item
97 >                    removeQueueItem((LinkedList) _lists.get(i));
98                  }
99 +                // check if we should add (not if Queue full, and using DROP algorithm)
100 +                if(!(s==_maxSize && _removeAlgorithm==DROP)) {
101 +                    // add the next item, ensuring we lock
102 +                    synchronized(this) {
103 +                        // LinkedList.add() does the same thing, but this ensures behaviour
104 +                        ((LinkedList) _lists.get(i)).addLast(o);
105 +                    }
106 +                }
107                  // if the queue was empty before the add it is possible
108                  // that a consumer is waiting... so we notify them
109                  if (s == 0) {
# Line 138 | Line 197 | public class Queue {
197                  status += "queue"+i+"=\"[deleted]\" ";
198              }
199          }
200 <        status += "total=\""+_count+"\"></queue>";
200 >        status += "total=\""+_count+"\"";
201 >        if(_maxSize != -1) {
202 >            status += " maxSize=\""+_maxSize+"\"";
203 >        }
204 >        status += "></queue>";
205          return status;
206      }
207      
# Line 277 | Line 340 | public class Queue {
340      }
341  
342   //---PRIVATE METHODS---
343 <
343 >    
344 >    /**
345 >     * This method removes an item from a Queue, using a method
346 >     * requested at construction.
347 >     *
348 >     * @param list The LinkedList from which to remove an item.
349 >     */
350 >    private void removeQueueItem(LinkedList list) {
351 >        // look at our algorithm
352 >        // remove a random item from the list
353 >        if(_removeAlgorithm==RANDOM) {
354 >            // new Random, with a good seed
355 >            Random rand = new Random(System.currentTimeMillis());
356 >            int i = rand.nextInt(_maxSize);
357 >            synchronized(this) {
358 >                list.remove(i);
359 >            }
360 >        }
361 >        // remove the first item from the list
362 >        else if(_removeAlgorithm==FIRST) {
363 >            synchronized(this) {
364 >                list.removeFirst();
365 >            }
366 >        }
367 >        // remove the last item from the list
368 >        else if(_removeAlgorithm==LAST) {
369 >            synchronized(this) {
370 >                list.removeLast();
371 >            }
372 >        }
373 >    }
374 >    
375   //---ACCESSOR/MUTATOR METHODS---
376  
377   //---ATTRIBUTES---
# Line 297 | Line 391 | public class Queue {
391       * A reference to our QueueMonitor, if we have one.
392       */
393      private QueueMonitor _queueMon = null;
394 +    
395 +    /**
396 +     * The maximum size of any Queue.
397 +     */
398 +    private int _maxSize = -1;
399 +    
400 +    /**
401 +     * The remove algorithm to use upon a Queue reaching
402 +     * it's maximum size.
403 +     */
404 +    private int _removeAlgorithm = -1;
405      
406      /**
407       * This is the friendly identifier of the

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines