KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > util > BlockingQueue


1 // ========================================================================
2
// $Id: BlockingQueue.java,v 1.5 2004/05/09 20:32:49 gregwilkins Exp $
3
// Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.util;
17
18 /* ------------------------------------------------------------ */
19 /** Blocking queue.
20  *
21  * Implemented as circular buffer in a Vector. Synchronization is on the
22  * vector to avoid double synchronization.
23  *
24  * @version $Id: BlockingQueue.java,v 1.5 2004/05/09 20:32:49 gregwilkins Exp $
25  * @author Greg Wilkins (gregw)
26  */

27 public class BlockingQueue
28 {
29     // TODO temp implementation. Should use java2 containers.
30

31     Object JavaDoc[] elements;
32     Object JavaDoc lock;
33     int maxSize;
34     int size=0;
35     int head=0;
36     int tail=0;
37
38     /* ------------------------------------------------------------ */
39     /** Constructor.
40      */

41     public BlockingQueue(int maxSize)
42     {
43         this(null,maxSize);
44     }
45
46     /* ------------------------------------------------------------ */
47     /** Constructor.
48      */

49     public BlockingQueue(Object JavaDoc lock, int maxSize)
50     {
51         this.maxSize=maxSize;
52         if (maxSize==0)
53             this.maxSize=255;
54         elements = new Object JavaDoc[this.maxSize];
55         this.lock=lock==null?elements:lock;
56     }
57     
58     /* ------------------------------------------------------------ */
59     public void clear()
60     {
61         synchronized(lock)
62         {
63             size=0;
64             head=0;
65             tail=0;
66         }
67     }
68     
69     /* ------------------------------------------------------------ */
70     public int size()
71     {
72         return size;
73     }
74     
75     /* ------------------------------------------------------------ */
76     public int maxSize()
77     {
78         return maxSize;
79     }
80     
81   
82     /* ------------------------------------------------------------ */
83     /** Put object in queue.
84      * @param o Object
85      */

86     public void put(Object JavaDoc o)
87         throws InterruptedException JavaDoc
88     {
89         synchronized(lock)
90         {
91             while (size==maxSize)
92                 lock.wait();
93
94             elements[tail]=o;
95             if(++tail==maxSize)
96                 tail=0;
97             size++;
98             lock.notify();
99         }
100     }
101     
102     /* ------------------------------------------------------------ */
103     /** Put object in queue.
104      * @param timeout If timeout expires, throw InterruptedException
105      * @param o Object
106      * @exception InterruptedException Timeout expired or otherwise interrupted
107      */

108     public void put(Object JavaDoc o, int timeout)
109         throws InterruptedException JavaDoc
110     {
111         synchronized(lock)
112         {
113             if (size==maxSize)
114             {
115                 lock.wait(timeout);
116                 if (size==maxSize)
117                     throw new InterruptedException JavaDoc("Timed out");
118             }
119             
120             elements[tail]=o;
121             if(++tail==maxSize)
122                 tail=0;
123             size++;
124             lock.notify();
125         }
126     }
127
128     /* ------------------------------------------------------------ */
129     /** Get object from queue.
130      * Block if there are no objects to get.
131      * @return The next object in the queue.
132      */

133     public Object JavaDoc get()
134         throws InterruptedException JavaDoc
135     {
136         synchronized(lock)
137         {
138             while (size==0)
139                 lock.wait();
140             
141             Object JavaDoc o = elements[head];
142             elements[head]=null;
143             if(++head==maxSize)
144                 head=0;
145             if (size==maxSize)
146                 lock.notifyAll();
147             size--;
148             return o;
149         }
150     }
151     
152         
153     /* ------------------------------------------------------------ */
154     /** Get from queue.
155      * Block for timeout if there are no objects to get.
156      * @param timeoutMs the time to wait for a job
157      * @return The next object in the queue, or null if timedout.
158      */

159     public Object JavaDoc get(int timeoutMs)
160         throws InterruptedException JavaDoc
161     {
162         synchronized(lock)
163         {
164             if (size==0 && timeoutMs!=0)
165                 lock.wait((long)timeoutMs);
166             
167             if (size==0)
168                 return null;
169             
170             Object JavaDoc o = elements[head];
171             elements[head]=null;
172             if(++head==maxSize)
173                 head=0;
174
175             if (size==maxSize)
176                 lock.notifyAll();
177             size--;
178             
179             return o;
180         }
181     }
182     
183     /* ------------------------------------------------------------ */
184     /** Peek at the queue.
185      * Block if there are no objects to peek.
186      * @return The next object in the queue, or null if timedout.
187      */

188     public Object JavaDoc peek()
189         throws InterruptedException JavaDoc
190     {
191         synchronized(lock)
192         {
193             if (size==0)
194                 lock.wait();
195             
196             if (size==0)
197                 return null;
198             
199             Object JavaDoc o = elements[head];
200             return o;
201         }
202     }
203     
204     /* ------------------------------------------------------------ */
205     /** Peek at the queue.
206      * Block for timeout if there are no objects to peek.
207      * @param timeoutMs the time to wait for a job
208      * @return The next object in the queue, or null if timedout.
209      */

210     public Object JavaDoc peek(int timeoutMs)
211         throws InterruptedException JavaDoc
212     {
213         synchronized(lock)
214         {
215             if (size==0)
216                 lock.wait((long)timeoutMs);
217             
218             if (size==0)
219                 return null;
220             
221             Object JavaDoc o = elements[head];
222             return o;
223         }
224     }
225 }
226
227
228
229
230
231
232
233
234
Popular Tags