KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > Queue


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util;
11
12 import java.util.*;
13
14 /**
15  * A list object that allows firstin-firstout retrieval of data.
16  * When querying for data that is not (yet) available, the retrieve-process sleeps
17  * until it is notified of a change.
18  * In 1.5, this class need to be replaced with the java.util.concurrent.BlockingQueue<E> interface.
19  *
20  * @author vpro
21  * @version $Id: Queue.java,v 1.9 2006/07/18 12:10:25 michiel Exp $
22  * @deprecated Use edu.emory.mathcs.backport.java.util.concurrent.BlockingQueue, or some other Queue implementation.
23  */

24 public class Queue {
25
26     /**
27      * Default size of 32 for the queue if none is specified.
28      */

29     public static int DEFAULT_QUEUE_SIZE = 32;
30
31     /**
32      * Default timeout of 0 for a blocking append call.
33      * Not used
34      */

35     public static int DEFAULT_APPEND_TIMEOUT = 0;
36
37     /**
38      * Default timeout of 0 for a blocking get call.
39      * Not used
40      */

41     public static int DEFAULT_GET_TIMEOUT = 0;
42
43     /**
44      * The time to wait until an attempt to append an item times out.
45      * Not used
46      */

47     public int appendTimeoutTime;
48     /**
49      * The time to wait until an attempt to get an item times out.
50      * Not used
51      */

52     public int getTimeoutTime;
53
54     // the fields below should be private
55

56     /**
57      * The head element of the queue.
58      * This is the element that will be returned first by a {@link #get}.
59      */

60     QueueElement head;
61     /**
62      * The tail element of the queue.
63      * This is the element that was last added using {@link #append}.
64      */

65     QueueElement tail;
66
67     /**
68      * The real number of items in the queue.
69      */

70     int len = 0;
71
72     /**
73      * Max # of items to be allowed in queue.
74      * Not really used.
75      */

76     private int queuesize;
77
78     // fields below are not used any more, should be removed
79

80     public Vector items;
81     int flip = 0;
82     long get1,get2;
83
84     //private Object[] items;
85
//public Object[] items;
86
//private Object isNotFull = new Object();
87

88     private int first; // pointer to last item in queue
89
private int count; // current # of items in queue
90
private int maxcount; // max # of items to be allowed in queue
91

92     /**
93      * Constructs the queue with the default queue size set to
94      * DEFAULT_QUEUE_SIZE, and the append timeout set to
95      * DEFAULT_APPEND_TIMEOUT
96      *
97      * @see Queue#DEFAULT_QUEUE_SIZE
98      * @see Queue#DEFAULT_APPEND_TIMEOUT
99      */

100     public Queue() {
101         this(DEFAULT_QUEUE_SIZE, DEFAULT_APPEND_TIMEOUT, DEFAULT_GET_TIMEOUT);
102     }
103
104     /**
105      * Constructs the queue, sets the max number of queueable
106      * items to the given size, sets the append timeout
107      * to DEFAULT_APPEND_TIMEOUT, and sets the get timeout to
108      * DEFAULT_GET_TIMEOUT
109      *
110      * @param size The maximum size of the queue
111      * @see Queue#DEFAULT_APPEND_TIMEOUT
112      */

113     public Queue(int size) {
114         this(size, DEFAULT_APPEND_TIMEOUT, DEFAULT_GET_TIMEOUT);
115     }
116
117     /**
118      * Constructs the queue, sets the max number of queueable
119      * items to the given size, and sets the append() and get() timeouts
120      * to the given values.
121      *
122      * @param size The maximum size of the queue
123      * @param appendTimeout If we can't append() within this many milliseconds,
124      * the appendTimeout() method is called before retrying.
125      * @param getTimeout If we can't get() something within this many
126      * milliseconds, the getTimeout() method is called.
127      * @see Queue#appendTimeout
128      * @see Queue#getTimeout
129      */

130     public Queue(int size, int appendTimeout, int getTimeout) {
131
132         // better call newQueue...
133
appendTimeoutTime = appendTimeout;
134         getTimeoutTime = getTimeout;
135
136         queuesize = size; // initial size of the queue
137
items = new Vector(queuesize);
138
139         first = count = 0;
140         maxcount = size; // max elements in queue
141
}
142
143     /**
144      * Re-uinitializes the queue, sets the max number of queueable
145      * items to the given size, and sets the append() and get() timeouts
146      * to the given values.
147      *
148      * @param size The maximum size of the queue
149      * @param appendTimeout If we can't append() within this many milliseconds,
150      * the appendTimeout() method is called before retrying.
151      * @param getTimeout If we can't get() something within this many
152      * milliseconds, the getTimeout() method is called.
153      */

154     public void newQueue(int size, int appendTimeout, int getTimeout) {
155
156         appendTimeoutTime = appendTimeout;
157         getTimeoutTime = getTimeout;
158
159         queuesize = size; // initial size of the queue
160
items = new Vector(queuesize);
161
162         first = count = 0;
163         maxcount = size; // max elements in queue
164
}
165
166     /**
167      * Returns the size of the queue
168      */

169     public int queueSize() {
170         return queuesize;
171     }
172
173     /**
174      * Returns the number of items currently in the queue.
175      */

176     public int count() {
177         //return items.size();
178
return len;
179     }
180
181     /**
182      * Appends the given item to the queue. This method calls a
183      * synchronized append method, so that it won't interfere with a get
184      * call. The method will block if the queue is full, and it won't
185      * block otherwise.
186      *
187      * @todo rename to put(), similar to java's BlockingQueue
188      * @param item The item to be appended to the queue */

189     public synchronized void append(Object JavaDoc item) {
190         // put a object in the vector and wait on it
191
// it should be able to block if full
192
QueueElement p = new QueueElement();
193         p.obj = item;
194
195         if (tail == null) {
196             head = p;
197         } else {
198             tail.next = p;
199         }
200         p.next = null;
201         tail = p;
202         len++;
203         flip++;
204         if (flip > 99) {
205             flip = 0;
206         }
207         notify(); // scream that a new one has reached us.
208
}
209
210     /**
211      * Pulls an item off of the queue. This method will block until
212      * something is found. This method is synchronized so it doesn't
213      * interfere with the append call.
214      *
215      * @todo rename to take(), similar to java's BlockingQueue
216      * @return The bottom object of the queue.
217      */

218     public synchronized Object JavaDoc get() throws InterruptedException JavaDoc {
219 // try {
220
while(head == null) {
221             wait();
222         }
223 // } catch(InterruptedException e) {
224
// return null;
225
// }
226
QueueElement p = head;
227         head = head.next;
228         if (head == null) {
229             tail = null;
230         }
231         len--;
232         return p.obj;
233     }
234
235     /**
236      * This is called every time we timeout while waiting to append
237      * something to the queue. You can use this to figure out if
238      * you want to increase the queue size. A real hacker could override
239      * this to keep statistics, and use the resize() function to increase
240      * the size of the queue after a bunch of timeouts. NOTE - <b> DON'T </b>
241      * use the resize() method from within this method - the code will
242      * hang forever. You should instead flag another thread to do the
243      * resize.
244      *
245      * @see Queue#appendTimeoutTime
246      */

247     public void appendTimeout() {
248         // no default behavior
249
}
250
251     /**
252      * Pretty much the same thing as the getTimeout() method, but for
253      * blocking get() timeouts. REMEMBER: DON'T call resize() from within
254      * this method.
255      *
256      * @see Queue#getTimeoutTime
257      */

258     public void getTimeout() {
259         // no default behavior
260
}
261
262     /**
263      * Resizes the queue so that it can contain at most the given
264      * number of items in it. Note - the queue may already have more
265      * than the given number of items in it, if so, nothing will be
266      * allowed in the queue until it shrinks to contain fewer than the
267      * new maximum number of elements in it.
268      *
269      * @param newsize The new maximum size of the queue
270      */

271
272     public synchronized void resize(int newsize) {
273         /*
274         synchronized (isNotFull) {
275             if (newsize < maxcount) { // shrinking the queue
276                 maxcount = newsize;
277
278             } else if (newsize > maxcount) { // growing the queue
279
280                 if (newsize <= queuesize) { // queue size is still bigger
281                     maxcount = newsize; // than the new size
282
283                 } else { // (newsize > queuesize)
284                     Object[] newItems = new Object[newsize];
285
286                     for (int x = 1; x <= count; x++) {
287                         newItems[x - 1] = items[(first + x - 1) % queuesize];
288                     }
289
290                     items = newItems;
291                     queuesize = newsize;
292                     maxcount = newsize;
293                     first = 0;
294                 }
295
296                 isNotFull.notify(); // let any stuck appends proceed
297             }
298         }
299         */

300         queuesize = newsize;
301         maxcount = newsize;
302     }
303
304 }
305
Popular Tags