ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/util/uk/org/iscream/cms/util/Queue.java
(Generate patch)

Comparing projects/cms/source/util/uk/org/iscream/cms/util/Queue.java (file contents):
Revision 1.14 by tdb, Thu Mar 1 01:05:49 2001 UTC vs.
Revision 1.22 by tdb, Tue May 29 17:02:35 2001 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 < package uk.ac.ukc.iscream.util;
2 > package uk.org.iscream.cms.server.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.cms.server.util.*;
9  
10   /**
11   * A Queue class designed to operate in a multi-threaded environment, with
# Line 49 | Line 49 | public class Queue {
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---  
# Line 89 | Line 94 | public class Queue {
94          for(int i=0; i < _lists.size(); i++) {
95              // skip over any gaps left in the list
96              if(_lists.get(i) != null) {
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) {
101 <                    // we need to remove an item
102 <                    removeQueueItem((LinkedList) _lists.get(i));
103 <                }
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);
97 >                synchronized(((LinkedList) _lists.get(i))) {
98 >                    // get size before adding to the Queue
99 >                    int s = ((LinkedList) _lists.get(i)).size();
100 >                    // check whether we need to remove an item from the current Queue
101 >                    if(_maxSize!=-1 && s>=_maxSize && _removeAlgorithm!=DROP) {
102 >                        // we need to remove an item
103 >                        removeQueueItem((LinkedList) _lists.get(i));
104                      }
105 <                }
106 <                // if the queue was empty before the add it is possible
107 <                // that a consumer is waiting... so we notify them
108 <                if (s == 0) {
109 <                    synchronized(((LinkedList) _lists.get(i))) {
110 <                        ((LinkedList) _lists.get(i)).notifyAll();
105 >                    // check if we should add (not if Queue full, and using DROP algorithm)
106 >                    if(!(s>=_maxSize && _removeAlgorithm==DROP)) {
107 >                        // add the next item, ensuring we lock
108 >                        synchronized(this) {
109 >                            // LinkedList.add() does the same thing, but this ensures behaviour
110 >                            ((LinkedList) _lists.get(i)).addLast(o);
111 >                        }
112 >                        // if the queue was empty before the add it is possible
113 >                        // that a consumer is waiting... so we notify them
114 >                        //synchronized(((LinkedList) _lists.get(i))) {
115 >                            ((LinkedList) _lists.get(i)).notifyAll();
116 >                        //}
117                      }
118                  }
119              }
# Line 131 | Line 136 | public class Queue {
136              throw new InvalidQueueException("Requested queue "+queue+" does not exist");
137          }
138          // block if the queue is empty
139 <        if (((LinkedList) _lists.get(queue)).size() == 0) {
140 <            synchronized(((LinkedList) _lists.get(queue))) {
139 >        synchronized(((LinkedList) _lists.get(queue))) {
140 >            if (((LinkedList) _lists.get(queue)).size() == 0) {
141                  try { ((LinkedList) _lists.get(queue)).wait(); } catch(Exception e) {}
142              }
143          }
# Line 201 | Line 206 | public class Queue {
206          if(_maxSize != -1) {
207              status += " maxSize=\""+_maxSize+"\"";
208          }
209 <        status += "</queue>";
209 >        status += "></queue>";
210          return status;
211      }
212      
# Line 242 | Line 247 | public class Queue {
247       *
248       * @return An integer to be passed to the get() method.
249       */
250 <    public int getQueue() {
250 >    public synchronized int getQueue() {
251          int pos = -1;
252          for(int i=0; i < _lists.size(); i++) {
253              if(_lists.get(i) == null) {
# Line 266 | Line 271 | public class Queue {
271       *
272       * @param queue The integer identifier for the queue, given by getQueue().
273       */
274 <    public void removeQueue(int queue) {
274 >    public synchronized void removeQueue(int queue) {
275          _lists.set(queue, null);
276      }
277      
# Line 327 | Line 332 | public class Queue {
332       * Overrides the {@link java.lang.Object#toString() Object.toString()}
333       * method to provide clean logging (every class should have this).
334       *
335 <     * This uses the uk.ac.ukc.iscream.util.FormatName class
335 >     * This uses the uk.org.iscream.cms.server.util.FormatName class
336       * to format the toString()
337       *
338       * @return the name of this class and its CVS revision.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines