KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > justobjects > pushlet > core > EventQueue


1 // Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
2
// Distributable under LGPL license. See terms of license at gnu.org.
3

4 package nl.justobjects.pushlet.core;
5
6 /**
7  * FIFO queue with guarded suspension.
8  * <b>Purpose</b><br>
9  * <p>
10  * <b>Implementation</b><br>
11  * FIFO queue class implemented with circular array. The enQueue() and
12  * deQueue() methods use guarded suspension according to a readers/writers
13  * pattern, implemented with java.lang.Object.wait()/notify().
14  *
15  * <b>Examples</b><br>
16  * <p>
17  * <br>
18  *
19  * @version $Id: EventQueue.java,v 1.2 2005/02/21 11:50:46 justb Exp $
20  * @author Just van den Broecke - Just Objects &copy;
21  */

22 public class EventQueue {
23     /** Defines maximum queue size */
24     private int capacity = 8;
25     private Event[] queue = null;
26     private int front, rear;
27
28     /** Construct queue with default (8) capacity. */
29     public EventQueue() {
30         this(8);
31     }
32
33     /** Construct queue with specified capacity. */
34     public EventQueue(int capacity) {
35         this.capacity = capacity;
36         queue = new Event[capacity];
37         front = rear = 0;
38     }
39
40     /** Put item in queue; waits() indefinitely if queue is full. */
41     public synchronized boolean enQueue(Event item) throws InterruptedException JavaDoc {
42         return enQueue(item, -1);
43     }
44
45     /** Put item in queue; if full wait maxtime. */
46     public synchronized boolean enQueue(Event item, long maxWaitTime) throws InterruptedException JavaDoc {
47
48         // Wait (optional maxtime) as long as the queue is full
49
while (isFull()) {
50             if (maxWaitTime > 0) {
51                 // Wait at most maximum time
52
wait(maxWaitTime);
53
54                 // Timed out or woken; if still full we
55
// had bad luck and return failure.
56
if (isFull()) {
57                     return false;
58                 }
59             } else {
60                 wait();
61             }
62         }
63
64         // Put item in queue
65
queue[rear] = item;
66         rear = next(rear);
67
68         // Wake up waiters; NOTE: first waiter will eat item
69
notifyAll();
70         return true;
71     }
72
73     /** Get head; if empty wait until something in queue. */
74     public synchronized Event deQueue() throws InterruptedException JavaDoc {
75         return deQueue(-1);
76     }
77
78     /** Get head; if empty wait for specified time at max. */
79     public synchronized Event deQueue(long maxWaitTime) throws InterruptedException JavaDoc {
80         while (isEmpty()) {
81             if (maxWaitTime >= 0) {
82                 wait(maxWaitTime);
83
84                 // Timed out or woken; if still empty we
85
// had bad luck and return failure.
86
if (isEmpty()) {
87                     return null;
88                 }
89             } else {
90                 // Wait indefinitely for something in queue.
91
wait();
92             }
93         }
94
95         // Dequeue item
96
Event result = fetchNext();
97
98         // Notify possible wait()-ing enQueue()-ers
99
notifyAll();
100
101         // Return dequeued item
102
return result;
103     }
104
105     /** Get all queued Events. */
106     public synchronized Event[] deQueueAll(long maxWaitTime) throws InterruptedException JavaDoc {
107         while (isEmpty()) {
108             if (maxWaitTime >= 0) {
109                 wait(maxWaitTime);
110
111                 // Timed out or woken; if still empty we
112
// had bad luck and return failure.
113
if (isEmpty()) {
114                     return null;
115                 }
116             } else {
117                 // Wait indefinitely for something in queue.
118
wait();
119             }
120         }
121
122         // Dequeue all items item
123
Event[] events = new Event[getSize()];
124         for (int i = 0; i < events.length; i++) {
125             events[i] = fetchNext();
126         }
127
128         // Notify possible wait()-ing enQueue()-ers
129
notifyAll();
130
131         // Return dequeued item
132
return events;
133     }
134
135     public synchronized int getSize() {
136         return (rear >= front) ? (rear - front) : (capacity - front + rear);
137     }
138
139     /** Is the queue empty ? */
140     public synchronized boolean isEmpty() {
141         return front == rear;
142     }
143
144     /** Is the queue full ? */
145     public synchronized boolean isFull() {
146         return (next(rear) == front);
147     }
148
149     /** Circular counter. */
150     private int next(int index) {
151         return (index + 1 < capacity ? index + 1 : 0);
152     }
153
154     /** Circular counter. */
155     private Event fetchNext() {
156         Event temp = queue[front];
157         queue[front] = null;
158         front = next(front);
159         return temp;
160     }
161
162     public static void p(String JavaDoc s) {
163         System.out.println(s);
164     }
165
166     public static void main(String JavaDoc[] args) {
167         EventQueue q = new EventQueue(8);
168         Event event = new Event("t");
169         try {
170             q.enQueue(event);
171             p("(1) size = " + q.getSize());
172             q.enQueue(event);
173             p("(2) size = " + q.getSize());
174             q.deQueue();
175             p("(1) size = " + q.getSize());
176             q.deQueue();
177             p("(0) size = " + q.getSize());
178
179             q.enQueue(event);
180             q.enQueue(event);
181             q.enQueue(event);
182             p("(3) size = " + q.getSize());
183             q.deQueue();
184             p("(2) size = " + q.getSize());
185             q.enQueue(event);
186             q.enQueue(event);
187             q.enQueue(event);
188             p("(5) size = " + q.getSize());
189             q.enQueue(event);
190             q.enQueue(event);
191             p("(7) size = " + q.getSize());
192             q.deQueue();
193             q.deQueue();
194             q.deQueue();
195             p("(4) size = " + q.getSize());
196             q.deQueue();
197             q.deQueue();
198             q.deQueue();
199             ;
200             q.deQueue();
201             p("(0) size = " + q.getSize());
202
203             q.enQueue(event);
204             q.enQueue(event);
205             q.enQueue(event);
206             q.enQueue(event);
207             q.enQueue(event);
208             p("(5) size = " + q.getSize());
209
210             q.deQueue();
211             q.deQueue();
212             q.deQueue();
213             ;
214             q.deQueue();
215             p("(1) size = " + q.getSize());
216         } catch (InterruptedException JavaDoc ie) {
217         }
218     }
219 }
220
221 /*
222 * $Log: EventQueue.java,v $
223 * Revision 1.2 2005/02/21 11:50:46 justb
224 * ohase1 of refactoring Subscriber into Session/Controller/Subscriber
225 *
226 * Revision 1.1 2005/02/18 10:07:23 justb
227 * many renamings of classes (make names compact)
228 *
229 * Revision 1.6 2005/02/16 12:16:16 justb
230 * added support for "poll" mode
231 *
232 * Revision 1.5 2005/01/13 14:47:15 justb
233 * control evt: send response on same (control) connection
234 *
235 * Revision 1.4 2004/09/03 22:35:37 justb
236 * Almost complete rewrite, just checking in now
237 *
238 * Revision 1.3 2003/08/15 08:37:40 justb
239 * fix/add Copyright+LGPL file headers and footers
240 *
241 *
242 */

243
Popular Tags